Script data type in jquery

The jquery site says:

"script": evaluates the response as JavaScript and evaluates it.

I get a block of js code (without script tags) around an ajax call and markup fragment. To work on the label, I need js code that will be executed as soon as it appears. Do I have to add JS code to dom to execute it, or as it says on the jquery site, it just starts by itself as soon as it appears?

Can someone help me with this please?

Thanks L

+3
source share
4 answers

jQuery.getScript(), , . , , , , script, script . DOM, , . , script , .js.

+2

, $.getScript(), .js, , , . API docs , .

- , , JavaScript, <script>, DOM. - ​​ , . JavaScript, , jQuery, .

, , JavaScript DOM:

var headID = document.getElementsByTagName("head")[0];         
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.onload=scriptLoaded;
newScript.src = 'http://api.flickr.com/services/feeds/photos_public.gne?tags=sunset&format=json';
headID.appendChild(newScript);

, .

0

, 2 Ajax:

  • one dataType: "script", ( ) script
  • dataType: "html" .

- script ( ) . , , DOM.

0

Another approach that Douglas Crockford frowned at (see the latest tip) would be to just evalthe JavaScript code that you get from the server.

$(document).ready(function(){
    var scriptString = "alert('hi');";  //assume I get this from the server 
                                        // as a string of JavaScript commands
    eval(scriptString); //executed immediately
});
0
source

Source: https://habr.com/ru/post/1796317/


All Articles