load js external file from javascript

Discussion in 'JavaScript' started by mark103, Mar 23, 2013.

  1. #1
    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!!!!
     
    mark103, Mar 23, 2013 IP
  2. Gemba

    Gemba Member

    Messages:
    36
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    25
    #2
    Do you have the JQuery library?
    If so you can do this.
    $.getScript("urltoscript.js");
    Code (markup):
     
    Gemba, Mar 23, 2013 IP
  3. mark103

    mark103 Active Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #3
    No I am only using javascript nothing else. I tried to read test1.js from key.js, but it won't letting me to read it.

    Here is the one i'm currently using it in my javascript:

    key.js

    function checkloadjscssfile(filename, filetype)
    {
      loadjscssfile(filename, filetype)
      filesadded+="["+filename+"]" //List of files added in the form "[filename1],[filename2],etc"
    }
    
    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");
             }
          }
       }
    Code (markup):

    test1.js

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

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

    any idea?
     
    mark103, Mar 23, 2013 IP
  4. Gemba

    Gemba Member

    Messages:
    36
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    25
    #4
    You didn't include the code for loadjscssfile
     
    Gemba, Mar 23, 2013 IP
  5. mark103

    mark103 Active Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #5
    sorry, I have added the wrong code when I posted it.

    I used this code to read my javascript code but it did not work.

    
    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");
            }
          }
      }
    Code (markup):

    any idea?
     
    mark103, Mar 23, 2013 IP
  6. xtmx

    xtmx Active Member

    Messages:
    359
    Likes Received:
    12
    Best Answers:
    4
    Trophy Points:
    88
    #6
    Errors:
    1. you didn't append the script to the body element
    2. test1.js had an extra quote.
    Bad Practice:
    1. Using setAttribute.

    key.js
    function loadjscssfile(filename, filetype)
    {
      var fileref=document.createElement('script');
      fileref.type = "text/javascript";
      fileref.src = filename;
      document.body.appendChild(fileref);
    }
    document.onkeydown = function(ev)
    {
      var key;
      ev = ev || event;
      key = ev.keyCode;
     
      if(key == 13)
      {
          if (img1.indexOf('image1_yellow.jpg') != -1)
          {
            if (img5.indexOf('picture1_yellow.jpg') != -1)
            {
                loadjscssfile("test1.js", "js");
            }
          }
      }
    }
    HTML:


    test1.js
    var teststring="hello! you are reading another javascript file";
    alert(teststring);
    HTML:
     
    Last edited: Mar 24, 2013
    xtmx, Mar 24, 2013 IP
  7. mark103

    mark103 Active Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #7
    thank you very much for your help, 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

    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");
             }
          }
       }
    Code (markup):
    test1.js

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

    do you know why and what is wrong?
     
    mark103, Mar 25, 2013 IP
  8. xtmx

    xtmx Active Member

    Messages:
    359
    Likes Received:
    12
    Best Answers:
    4
    Trophy Points:
    88
    #8
    1. You have to change "loadjscssfile" to "loadjsfile"
    2. Below the line with appendChild, insert a new line with:
    myFunction();
     
    xtmx, Mar 25, 2013 IP
  9. mark103

    mark103 Active Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #9
    I have already got it working. Now I want to work on to read the code from php using with javascript.

    do you know how i can read the code from my php using with javascript?
     
    mark103, Mar 25, 2013 IP