Web Analytics Made Easy -
StatCounter Mount a .js in JavaScript - CodingForum

Announcement

Collapse
No announcement yet.

Mount a .js in JavaScript

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    If I understand the problem, see if this works for you: http://javascript.internet.com/snipp...r-toolbox.html

    Comment


    • #3
      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.

      Comment


      • #4
        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.

        Comment


        • #5
          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.

          Comment


          • #6
            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.
            .My new Javascript tutorial site: http://reallifejs.com/
            .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
            .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

            Comment


            • #7
              use a dom adder (document.createElement("script")). you shouldn't need to reload thew page for anything ever.
              Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

              Comment


              • #8
                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.

                Comment


                • #9
                  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.

                  Comment


                  • #10
                    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???
                    Be yourself. No one else is as qualified.

                    Comment


                    • #11
                      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?

                      Comment


                      • #12
                        He doesn't know *which* script to load until he asks the user with that prompt() call.
                        Be yourself. No one else is as qualified.

                        Comment


                        • #13
                          ... 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...

                          Comment


                          • #14
                            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; Aug 21, 2011, 02:08 AM.
                            Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

                            Comment


                            • #15
                              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.

                              Comment

                              Working...
                              X