1

How can I load a JavaScript with this code

<script type="text/javascript" src="http://uads.ir/l.php?s=125125&w=5307cd5c027373e1773c9869"></script>

after page has loaded

$(document).ready(function() 
 {

 }); 

Is there another way?

Hidde
  • 9,683
  • 7
  • 38
  • 64
user3261715
  • 35
  • 1
  • 1
  • 8

5 Answers5

8

Try appending the script to the head section: (Working jsFiddle)

$(document).ready(function(){
    $('<script/>',{type:'text/javascript', src:'http://uads.ir/l.php?s=125125&w=5307cd5c027373e1773c9869'}).appendTo('head');
 }); 

EDIT: I like this syntax better..

Yotam Omer
  • 14,510
  • 11
  • 55
  • 61
2

I prefer to create the script tag, then its type and src attribs like this:

var fileref=document.createElement('script');
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", filename);

Straightforward and semantic.

Ben A. Hilleli
  • 582
  • 6
  • 20
1

Put your script tags after all of the HTML content.

tymeJV
  • 99,730
  • 13
  • 150
  • 152
1

By using the ajax() function as follows:

$(document).ready(function() {
    $.ajax({
        url: "http://uads.ir/l.php?s=125125&w=5307cd5c027373e1773c9869",
        dataType: "script",
        cache: true
    });
});
dana
  • 14,964
  • 4
  • 53
  • 82
1

You can use jQuery's getScript() method to achieve this. Documentation here. Usage:

$.getScript( "ajax/test.js", function( data, textStatus, jqxhr ) {
  console.log( data ); // Data returned
  console.log( textStatus ); // Success
  console.log( jqxhr.status ); // 200
  console.log( "Load was performed." );
});
Ananthan Unni
  • 1,274
  • 8
  • 19
  • 1
    This is what I originally had, but I did a double-check and remembered that this method adds a time-stamp to the URL preventing caching. As such, you need to use the low-level $.ajax() function in order to leverage browser caching. – dana Feb 21 '14 at 23:24