Search code examples
javascriptjqueryflickr

Loading simple JS code after page loads?


Is there a way to load the JavaScript code (without placing it in the very bottom line of HTML document) after the page has been loaded? Here's the script example:

<script type="text/javascript" src="http://www.flickr.com/badge_code_v2.gne?count=10&display=latest&size=t&layout=x&source=user&user=XXX"></script>

PS. the reason is that the above Flickr code loads slowly and hangs the site meanwhile.


Solution

  • you can try it

    <script type="text/javascript">
    // Use any event to append the code
    $(document).ready(function() 
    {
        var s = document.createElement("script");
        s.type = "text/javascript";
        s.src = "test.js";
        // Use any selector
        $("head").append(s);
    });
    </script>
    

    off course this will require jquery library or else you can paste this code on body's onload method

    well this is something useful

    It's not possible to synchronously execute a script at a URL. Note further that synchronous anything, when networks (or even file systems!) are involved is a Bad Idea. Someone, sometime, somewhere will be on a slow system, or a slow network, or both, and suddenly you've just hung their UI in the process.

    http://bugs.jquery.com/ticket/6557

    alternatively you can try

    http://yepnopejs.com/