www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] Question: Can you delay load of an external JS file?

  1. #1
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,357

    resolved [RESOLVED] Question: Can you delay load of an external JS file?

    I would like to retrieve the contents of different external JS files with a textbox or drop-down selection.
    Is this possible?

    Code:
    <select id="SBox" onChange="LoadJSinfo(this.value)">
    <option value="">Choose Month</option>
    <option value="Jan.js">Jan</option>
    // ...
    <option value="Dec.js">Dec<option>
    </select>
    Where 'Jan.js' ... 'Dec.js' would be stand alone external files.

    I am stuck on how to create the function 'LoadJSinfo(filename)'.
    My initial thoughts were:
    Code:
    function LoadJSinfo(filename) {
      <script type="text/javascript" src=filename></script>
    }
    But obviously, that doesn't work.

    Is there a way to load the external file contents after the page has been displayed?

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Use DOM methods:
    Code:
    function addScript(JSfileName) {
    var js = document.createElement('script');
    js.setAttribute('type', 'text/javascript');
    js.src = JSfileName;
    document.body.appendChild(js);
    }
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,357
    Thank you 'Fang'. I'll give it a try.
    Appreciate the information.

  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by Fang View Post
    Use DOM methods:
    Code:
    function addScript(JSfileName) {
    var js = document.createElement('script');
    js.setAttribute('type', 'text/javascript');
    js.src = JSfileName;
    document.body.appendChild(js);
    }
    shouldntt the script tag go in the head instead of the body?
    I thought some browsers wouldn't fire that...
    good news if i'm wrong, that's shorter code.

  5. #5
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    The usual place to put JavaScript tags these days is at the bottom of the body. That way any content that needs to run as soon as possible and which needs to interact with the page can run straight away because the HTML has already loaded before the JavaScript. Also since JavaScript files single threads for download if hard coded in the HTML placing them at the bottom of the body doesn't hold up the download of anything else in the page. For script tags added dynamically from JavaScript it doesn't make any difference whether you add them to the end of the head or end of the body since in either case the page is already downloaded before the JavaScript.
    Stephen

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles