0

I have an ajax call which gets a string contains script from the backend. Now I want to add this string to a script tag at runtime.

$.ajax({
    type: "GET",
    url: "Users/_getScript",
    contentType: "application/json; charset=utf-8",
    cache: false,
    success: function (data) {
        script = data;
        console.log(script);
    }
});

say script is a global variable which now contains code from backend.

Note: There is no URL from which I want to load external js. I want to add a string variable.

Muhammed Shevil KP
  • 1,404
  • 1
  • 16
  • 21
  • Is this what you are looking for? https://learn.jquery.com/using-jquery-core/document-ready/ – Thomas Johansen Jan 02 '18 at 06:15
  • What exactly do you mean by *"add string to script tag"*? Sounds like what you want is [`$.getScript()`](http://api.jquery.com/jQuery.getScript/) – charlietfl Jan 02 '18 at 06:15
  • I'm getting the js code through an ajax call in string now I want to add this string to a script tag. – Habib Ur Rehman Jan 02 '18 at 06:18
  • No need if you use `$.getScript`. If you are returning valid js it will get evaluated immediately – charlietfl Jan 02 '18 at 06:19
  • @charlietfl but ` $.getScript("demot.js");` takes an external js file as an argument, not a variable. Please correct me if I'm wrong. – Habib Ur Rehman Jan 02 '18 at 06:24
  • Does not have to be js file ...just has to be valid js returned – charlietfl Jan 02 '18 at 06:25
  • Example using `.txt` file http://plnkr.co/edit/tI6x43jMtcYH9DqnxBKf?p=preview – charlietfl Jan 02 '18 at 06:27
  • I think you return your script with script tag from c# method. And use `insertBefore()`. This way you can insert that script tag in your body tag. This is what you want.? – DhavalR Jan 02 '18 at 06:29
  • @charlietfl I want to use `ajax` for now, it seems that `$.getScript` also loads external js – Habib Ur Rehman Jan 02 '18 at 06:30
  • $.getScript is ajax...jQuery just evaluates the code for you and any functions or variables are available at the point the callback gets called and any time after that point. What exactly is it you want to do with this code? – charlietfl Jan 02 '18 at 06:31
  • @charlietfl my backend function is returning CS script say `` in a varibale through ajax call, all I want is to add this variable to my aspx page at runtime. – Habib Ur Rehman Jan 02 '18 at 06:36
  • You don't need the script tag. Once script is evaluated it is in memory and available any time after. The script tag does nothing after the code is compiled – charlietfl Jan 02 '18 at 06:37
  • so you are saying is that all I need to do is call `$.getScript(myvariable)` and the script will be appended there? – Habib Ur Rehman Jan 02 '18 at 06:39
  • No.. see my demo example above. I added a setInterval to show you that it is available for the life of the page – charlietfl Jan 02 '18 at 06:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/162315/discussion-between-habib-ur-rehman-and-charlietfl). – Habib Ur Rehman Jan 02 '18 at 06:46
  • Possible duplicate of [How to Append in javascript?](https://stackoverflow.com/questions/9413737/how-to-append-script-script-in-javascript) – Amr Elgarhy Jan 02 '18 at 10:58
  • @HabibUrRehman If your returned string contains `script` tag then simply use `$(data).appendTo('head');` – mmushtaq Jan 03 '18 at 11:35

1 Answers1

-1

You can use insertAfter or insertBefore to append your script. If you want to execute your script, use $.getScript().

$("<yourscript>").insertAfter("<any element you want your script to be added after>")

$("<yourscript>").insertBefore("<any element you want your script to be added before>")
DhavalR
  • 1,409
  • 3
  • 29
  • 57