...

View Full Version : Mount a .js in JavaScript



Omega9850
08-20-2011, 03:11 AM
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!

jmrker
08-20-2011, 03:34 AM
If I understand the problem, see if this works for you: http://javascript.internet.com/snippets/a-modular-toolbox.html

Philip M
08-20-2011, 08:41 AM
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/javatutors/loadjavascriptcss.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.

jmrker
08-20-2011, 01:26 PM
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. :confused:
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. :eek:

Philip M
08-20-2011, 03:24 PM
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.

venegal
08-20-2011, 06:36 PM
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.

rnd me
08-20-2011, 06:43 PM
use a dom adder (document.createElement("script")). you shouldn't need to reload thew page for anything ever.

Philip M
08-20-2011, 07:50 PM
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?

Omega9850
08-20-2011, 08:58 PM
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.

Old Pedant
08-20-2011, 11:22 PM
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???

xelawho
08-20-2011, 11:22 PM
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?

Old Pedant
08-21-2011, 02:45 AM
He doesn't know *which* script to load until he asks the user with that prompt() call.

xelawho
08-21-2011, 04:22 AM
... 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...

rnd me
08-21-2011, 07:05 AM
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:



<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.

Philip M
08-21-2011, 08:18 AM
... 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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum