SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2010
    Location
    ONS
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to load some javascript after Page load?

    How to load some javascript after Page load?

    I want some external javascripts to be loaded after 5 seconds the page loaded, like for Google Analytics and a few more etc. etc..
    Thanks.

  2. #2
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <html>
    <body onload="myfunction()">
    <p id="text"></p>
    </body>
    </html>

    //in your external js file:

    function myfunction(){
    var t=setTimeout("newfunction()",5000);
    }

    function newfunction(){
    alert("5 seconds later...");
    document.getElementById('text').innerHTML = "hello world!";
    }

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2010
    Location
    ONS
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But Sir, how the page knows where the external file is?

    I have


    This code is to be called after 5 seconds,

    <script src="http://abx.com/123.js" type="text/javascript"></script>
    <script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-5472515-3']);
    _gaq.push(['_setDomainName', 'none']);
    _gaq.push(['_setAllowLinker', true]);
    _gaq.push(['_trackPageview']);
    (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
    </script>


    How to do this?

  4. #4
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    replace this:

    function newfunction(){
    alert("5 seconds later...");
    document.getElementById('text').innerHTML = "hello world!";
    }

    with this:
    function newfunction(){
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-5472515-3']);
    _gaq.push(['_setDomainName', 'none']);
    _gaq.push(['_setAllowLinker', true]);
    _gaq.push(['_trackPageview']);
    (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
    }

    tell me if this works?

  5. #5
    SitePoint Zealot
    Join Date
    Nov 2010
    Location
    ONS
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks sir,

    But I am really sorry to disturb you again, because I am new to javascript.

    Sir, The above file from the code which I told (which is not controllable to me because it is on other server).

    <script src="http://abx.com/123.js" type="text/javascript"></script>

    What to do with this sir?
    I only want the delayed loading to load external JS files which are not controllable to me ( If it were on my server, then of course, I can put their text in a new JavaScript function.. But it is not)

  6. #6
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this should work:

    remove this line from your document-<script src="http://abx.com/123.js" type="text/javascript"></script>

    HTML Code:
    <html>
    <head>
    <script type="text/javascript">
    function myfunction(){
    var t=setTimeout("newfunction()",5000);
    }
    
    function newfunction(){
    var mysrc=document.createElement('script');
    mysrc.src="http://abx.com/123.js";
    document.getElementsByTagName('head')[0].appendChild(mysrc); 
    }
    </script>
    </head>
    <body onload="myfunction()">
    </body>
    </html>
    ^ when page loads, function will be called which will call another function after 5 seconds, this function will then add "<script src="http://abx.com/123.js" type="text/javascript"></script> " to the document. So this should work...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •