0

I'm trying to pass java script code to the script tag and it is being displayed as plain text. How should i correctly pass the object so that the value is populated correctly

var script = $('<script>', {
    type: 'text/javascript',
    src: 'mcbz.com/models?type=c43&geo=true&languageselected='+ window.location.pathname.substring(1,6)
});

script[0].setAttribute("async", "");

$('script:first').before(script);

Expected result

 <script  async  src="mcbz.com/models?type=c43&geo=true&languageselected=en" ></script>
  • Possible duplicate of [How to dynamically insert a – frobinsonj Oct 17 '19 at 12:48
  • There is nothing wrong with how you insert that substring of the location pathname into the `src` attribute. // What gets displayed as text where? – 04FS Oct 17 '19 at 12:52

1 Answers1

0

You can try this out.

const script = document.createElement("script");
script.type = "text/javascript";
script.src = `mcbz.com/models?type=c43&geo=true&languageselected=${ window.location.pathname.substring(1,6)}`;
script.setAttribute("async", "");
$("head").append(script);