www.webdeveloper.com
Results 1 to 7 of 7

Thread: load js external file from javascript

  1. #1
    Join Date
    Sep 2008
    Posts
    62

    load js external file from javascript

    does anyone know how to load the js external file from javascript?

    i tried it on google to find the working one but none of them are working.

    please help!!!!

  2. #2
    Join Date
    Mar 2009
    Posts
    492
    Be more specific about what you want. Your request, as is, doesn't make any sense.

  3. #3
    Join Date
    Sep 2008
    Posts
    62
    I tried to read the code from my external javascript to another but it won't let me to read it.

    I have tried to use this in my javascript:

    key.js

    Code:
    function loadjscssfile(filename, filetype)
    {
      var fileref=document.createElement('script');
      fileref.setAttribute("type","text/javascript");
      fileref.setAttribute("src", filename);
    }
    
    document.onkeydown = function(ev) 
    {	
       var key;
       ev = ev || event;
       key = ev.keyCode;
       var image1 = document.getElementById("image1").getElementsByTagName("img")[0];
    
       if(key == 13)
       {
          if (img1.indexOf('image1_yellow.jpg') != -1)
          {
             if (img5.indexOf('picture1_yellow.jpg') != -1)
             {
                loadjscssfile("test1.js", "js");
             }
          }
       }

    test1.js

    Code:
    var teststring="hello! you are reading another javascript file";
    alert("teststring);

    I can't read the test1.js from the key.js.

    any idea?

  4. #4
    Join Date
    Mar 2009
    Posts
    492
    I'm still unclear as to what you are attempting to do here, but I do have some observations:

    Code:
    function loadjscssfile(filename, filetype)
    {
      var fileref=document.createElement('script');
      fileref.setAttribute("type","text/javascript");
      fileref.setAttribute("src", filename);
    }
    (1) setAttribute doesn't work in IE8 or below.
    (2) you are not attaching the script to the document. You can create all
    the dynamic elements you want (scripts or otherwise) but unless they are
    attached to the document or something in it they will evaporate as soon as the function returns.
    (3) Even if you fix #2, you need to provide for the latency period that it will take to download the script after its dynamically inserted.

  5. #5
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,680
    Code:
    function loadjscssfile(filename, filetype)
    {
      var fileref=document.createElement('script');
      fileref.type="text/javascript";
      fileref.src="filename.js";
      document.getElementsByTagName('HEAD')[0].appendChild(fileref);
    }
    loadjscssfile('filename')
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  6. #6
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266

  7. #7
    Join Date
    Sep 2008
    Posts
    62
    thank you very much for your help guys, it is working now. I have a problem with the function I created. I tried to read them but nothing is going to happens and the alert message box did not display.

    key.js

    Code:
    function loadjsfile(filename)
    {
        var fileref=document.createElement('script');
        fileref.setAttribute("type","text/javascript")
        fileref.setAttribute("src", filename);
        document.body.appendChild(fileref);
    }
    
    document.onkeydown = function(ev)
    {   
       var key;
       ev = ev || event;
       key = ev.keyCode;
       var image1 = document.getElementById("image1").getElementsByTagName("img")[0];
    
       if(key == 13)
       {
          if (img1.indexOf('image1_yellow.jpg') != -1)
          {
             if (img5.indexOf('picture1_yellow.jpg') != -1)
             {
                loadjscssfile("test1.js", "js");
             }
          }
       }
    test1.js

    Code:
    function myFunction()
    {
      alert("hello, this is a test");
    }

    do you know why and what is wrong?

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