Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Sep 2008
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by mark103 View Post
    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!!!!
    <script type = "text/javascript" src = "myjsfile.js></script>


    Or do you mean dynamically? If so see

    http://www.javascriptkit.com/javatut...criptcss.shtml
    Last edited by Philip M; 03-23-2013 at 10:22 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Regular Coder
    Join Date
    Sep 2008
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I tried to use that one in javascript but it won't let me to read another javascript file.

    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?
    Last edited by mark103; 03-24-2013 at 04:30 AM.

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    your code should
    look more like this ...

    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) 
    {
        ev = ev || event;
        key = ev.keyCode || ev.which;
        if(key == 13)
        {
            //do stuff ...
        }
    }
    Last edited by DaveyErwin; 03-24-2013 at 04:12 PM.

  • #5
    Regular Coder
    Join Date
    Sep 2008
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks, i have copied and paste the code but it is not working. It won't let me to read the external file from another javascript.

    Any idea?

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    Code:
     
    <!doctypt html>
    <html>
    <head>
    </HEAD>
    <body>
    press the enter key
    <script>
    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) 
    {
        ev = ev || event;
        key = ev.keyCode || ev.which;
        if(key == 13)
        {
            //do stuff ...
     loadjsfile("test1.js")
        }
    }
    </script>
    </body>
    </html>
    Last edited by DaveyErwin; 03-24-2013 at 10:24 PM.

  • #7
    Regular Coder
    Join Date
    Sep 2008
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks, 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?

  • #8
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    Code:
     
    <!doctypt html>
    <html>
    <head>
    </HEAD>
    <body>
    press the enter key
    <div id="image1">
    <img src="image1_yellow.jpg">
    <img src="image1_yellow.jpg">
    <img src="image1_yellow.jpg">
    <img src="image1_yellow.jpg">
    <img src="image1_yellow.jpg">
    <img src="picture1_yellow.jpg">
    </div>
    <script>
    function loadjscssfile(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 || ev.which;
       var img = document.getElementById("image1").getElementsByTagName("img");
       if(key == 13)
       {
          if (img[1].src.indexOf('image1_yellow.jpg') != -1)
          {
             if (img[5].src.indexOf('picture1_yellow.jpg') != -1)
             {
                loadjscssfile("test1.js", "js");
         myFunction();
             }
          }
       }
    }
    </script>
    </body>
    </html>

  • #9
    Regular Coder
    Join Date
    Sep 2008
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks david. It is working perfectly. Now I want to work on to read the code from php using with javascript. That is the last things I need to work on it.

    do you know how i can read the code from my php using with javascript?

  • #10
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    test1.php

    [CODE]
    <?php
    $a = "function myFunction(){alert('hello, this is a test');}";
    echo $a;
    ?>
    [/CODE]

    loadjscssfile("test1.php", "js");
    myFunction();
    Last edited by DaveyErwin; 03-26-2013 at 12:25 AM.

  • #11
    Regular Coder
    Join Date
    Sep 2008
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks david but it dont work. any idea??

  • #12
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,478
    Thanks
    0
    Thanked 635 Times in 625 Posts
    Code:
     
    <?php
    header('Content-type: text/javascript');
    $a = "function myFunction(){alert('hello, this is a test');}";
    echo $a;
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #13
    Regular Coder
    Join Date
    Sep 2008
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    Code:
     
    <?php
    header('Content-type: text/javascript');
    $a = "function myFunction(){alert('hello, this is a test');}";
    echo $a;
    it don't work.

    Edit: it didnt matter anyway, do you know how I can create the text in javascript?

    i tried to use this one:

    Code:
    <span style="font-family:Arial;font-size:24px;font-style:normal;font-weight:normal;text-decoration:none;text-transform:none;color:ded558;">this is a test</span>;
    it wont let me to input the text in the web browser. Any idea?
    Last edited by mark103; 03-26-2013 at 04:04 AM.


  •  

    Posting Permissions

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