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 15 of 15
  1. #1
    New Coder
    Join Date
    Aug 2011
    Location
    U.S.A.
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Mount a .js in JavaScript

    I have been working on a site, and I am trying to make a simple database system for it. I know that there is MySQL, but I do not want to use that. The problem I am facing is pretty simple... This is the code...

    <html>
    <head>
    <script type="text/javascript">
    function show_prompt()
    {
    var sn=prompt("Please enter the Serial Number","");
    if (sn!=null && sn!="")
    {
    document.write(sn);
    }
    }
    </script>
    </head>
    <body>

    <input type="button" onclick="show_prompt()" value="Show prompt box" />

    </body>
    </html>

    So the problem that I am facing, I have "document.write(sn);" but I want to replace it with a script that mounts an external .js file. Basically, is there a JavaScript equivalent of... "<script type="text/javascript" src=""></script>"

    I would appreciate ALL help. Thanks!

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts
    If I understand the problem, see if this works for you: http://javascript.internet.com/snipp...r-toolbox.html

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    jmrker - I do not see how that will work when the file is specified by the user after page load.

    Have a look at
    http://www.javascriptkit.com/javatut...criptcss.shtml

    Is this not a case for AJAX?

    #Londonriots preventing you form going home? Stay a night at Flemings! - Tweet by Flemings Hotel, Mayfair, where a single room costs £250 per night.

    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.

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Arrow

    Quote Originally Posted by Philip M View Post
    jmrker - I do not see how that will work when the file is specified by the user after page load.

    ...
    As I indicated, I'm not sure I understood the problem.
    I thought he/she is trying to load an external JS file after the page had been loaded.
    I thought that is why they are trying to substitute the document.write statement.

    At this point, your guess is as good a mine.
    We'll have to wait for the OP to return.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Like jmrker I am not sure that I understand the question. But scripts are loaded at the same time as the HTML page, and there is no way that another script specified by the user can be loaded afterwards.

    What it is possible to do is redirect to a new page based on the user's selection, and load an appropriate script with that new page.

    Example - User enters serial number 230
    Then you can redirect to "mypage230.html" whch loads script "page230.js".

    Naturally you will have to ensure that the serial number entered by the user relates to a valid html file.

    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.

  • #6
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Quote Originally Posted by Philip M View Post
    But scripts are loaded at the same time as the HTML page, and there is no way that another script specified by the user can be loaded afterwards.
    I'm not sure I understand what you are saying here — there *is* a way using DOM manipulation, and, in fact, that's exactly what the scripts in both yours and jmrker's link do.

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    use a dom adder (document.createElement("script")). you shouldn't need to reload thew page for anything ever.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by venegal View Post
    I'm not sure I understand what you are saying here — there *is* a way using DOM manipulation, and, in fact, that's exactly what the scripts in both yours and jmrker's link do.
    Yes, sorry, that was poorly expressed. I guess I am confused by the references to a "database" and a script which relates to a "serial number" in some way. I do not really see what the script is intended to do. If it somehow contains descriptive text, surely AJAX is the better solution?

    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.

  • #9
    New Coder
    Join Date
    Aug 2011
    Location
    U.S.A.
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Further Explanation

    Sorry, It seems I didn't explain my problem properly... Here is the code...

    <html>
    <head>
    <script type="text/javascript">
    function show_prompt()
    {
    var sn=prompt("Please enter the Serial Number","");
    if (sn!=null && sn!="")
    {
    include(sn);
    document.write(systemlimit);
    }
    }
    </script>
    </head>
    <body>

    <input type="button" onclick="show_prompt()" value="Show prompt box" />

    </body>
    </html>

    In the code, "include(sn); is meant to be the code that mounts the external js file. "systemlimit is a variable in the js file that is to be mounted. The code should be useable after the page has loaded.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    If you have any control at all over the contents of those "included" files, then making a minor mod to them and using JMrker's suggestion looks like the way to go.

    Can you give an example of what is in one of the "included" files???
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,762
    Thanks
    55
    Thanked 517 Times in 514 Posts

    probably not helpful at all, but...

    but why is it so important that the code be *loaded* after the function has run? wouldn't it be simpler just to load the script with the page load and call it (or not) from within the function?

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    He doesn't know *which* script to load until he asks the user with that prompt() call.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,762
    Thanks
    55
    Thanked 517 Times in 514 Posts
    ... still seems weird to me, when you can just pass a variable from the html page to the external js and handle the rest with separate functions, switches, etc.

    ... unless we're talking about hundreds of wildly different .js files, of course...

  • #14
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    sync includes only work before the page is loaded.
    after that, you'll have to use async programming technique.

    this is not so hard, and you don't need to modify your files.
    use the sentinal pattern i came up with, it's like a one-time temporary event; perfect for async where it's not otherwise possible/easy:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    function include(u){ var sc2=document.createElement('script'); sc2.src=u;  document.getElementsByTagName('*')[1].appendChild(sc2) ;}
    
    function show_prompt(){
    
      var sn=prompt("Please enter the Serial Number","");
      if (sn){
    
      (function waiter(){ //sentinal pattern 
    
        if(undefined===window.systemlimit){return setTimeout(waiter, 200);}
    
        document.body.appendChild(
          document.createTextNode(
             "variable value: " + systemlimit
           )
        );
      }()); //end waiter()
    
      include( "file_"+ sn +".js");
    
      }//end if sn entered by user
    }//end show_prompt()
    </script>
    </head>
    <body>
    
    <input type="button" onclick="show_prompt()" value="Show prompt box" />
    
    </body>
    </html>
    your posted code had the user name the actual file, i added a prefix and extention onto the user-entered value, mod as needed.
    Last edited by rnd me; 08-21-2011 at 07:08 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #15
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by xelawho View Post
    ... still seems weird to me, when you can just pass a variable from the html page to the external js and handle the rest with separate functions, switches, etc.

    ... unless we're talking about hundreds of wildly different .js files, of course...
    Yes, that is the bit that I do not grasp. I do not see how there can be a need for numerous different .js files.

    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.


  •  

    Tags for this Thread

    Posting Permissions

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