Welcome to WebmasterWorld Guest from 54.226.194.180

Forum Moderators: open

Message Too Old, No Replies

JavaScript

Calling external JavaScript from within a JavaScript

     

medic

12:16 pm on Jul 10, 2001 (gmt 0)

10+ Year Member



Hi,
Is there a way to call an external JavaScript from within a JavaScript?

Brett_Tabke

12:58 pm on Jul 10, 2001 (gmt 0)

WebmasterWorld Administrator brett_tabke is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Hi Medic and welcome to the board. Sure you can:

document.write('<script src="http://foo.com/myjs.js">');
document.write('</script>');

medic

1:16 am on Jul 11, 2001 (gmt 0)

10+ Year Member



Thanks for the reply Brett :-)
Does it mean I can write a code like this:

<script language="javascript">
function ValidateItem(obj){
var citem;
var validflag=false;
citem=obj.txtItemName.value;

document.write('<script src="myjs.js">');
document.write('</script>');

if (validflag) {
/* place my codes here */
}
else
{
alert("Invalid Item")
}
}
</script>

My purpose here is to execute the codes in MY.JS immediately after the "citem=obj.txtItemName.value;" then execute the "if (validflag)..." statement.

Do I get it right?

medic

1:48 am on Jul 11, 2001 (gmt 0)

10+ Year Member



duh..... why can't it work?
am i missing something or did something not right?

tedster

10:21 am on Jul 11, 2001 (gmt 0)

WebmasterWorld Senior Member tedster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



A couple of thoughts.

The problem with this may be that you end up with a <script> tag nested in a <script> tag. Have you tried three separate script tags:


<script language="javascript">
function ValidateItem(obj){
var citem;
var validflag=false;
citem=obj.txtItemName.value;
</script>


<script type="text/javascript" language="JavaScript" src="myjs.js"></script>


<script language="javascript">
if (validflag) {
/* place my codes here */
}
else
{
alert("Invalid Item")
}
}
</script>

Here's another angle you might try. When the HTML parser hits '</script>' in the second document.write statement, it stops the currently running script. You could test this by writing the second statment as:

document.write('</' + 'script>');

--but you may still have problems because of the nested script tags.

If all else fails, load the page in Netscape and see what their javascript console has to say (type javascript: in the location window).

medic

10:58 am on Jul 13, 2001 (gmt 0)

10+ Year Member



Thanks for the reply Ted,
I tried your advise but it seems that "calling an external Javascript from a Javascript" is really a dream for now (at least for me). :-)

MikeFoster

10:39 pm on Jul 24, 2001 (gmt 0)

10+ Year Member



Hi Tom!

medic, here's another idea:

Here's the file myjs.js:


function doSomething() {

...
}

Now, after myjs.js is included in the html file (as on line 4), all the functions defined in myjs.js will be available and can be called from within SCRIPT elements in the html file (as on line 10).


1: <html>
2: <head>
3:
4: <script type="text/javascript" src="myjs.js"> </script>
5:
6: <script type="text/javascript"><!--
7:
8: window.onload = function() {
9:
10: doSomething();
11: }
12:
13: //--></script>
14: </head>
15: <body>
16:
17: </body>
18: </html>

medic

8:05 am on Aug 6, 2001 (gmt 0)

10+ Year Member



Thanks for the help guys, especially to you Tom! That was a great tip!

phzzz

1:10 am on Aug 7, 2001 (gmt 0)

10+ Year Member



You are very welcome Medic. I knew the solution was the correct one, even before I posted. :)

Mike Foster is also very good as well. He co-moderates a "different" forum with me in Webmastering/HTML at another board, but that board will remain nameless here, due to proper "netiquette". LOL! :)

We respect this practice and abide by it at all times.

Take care!
...Tom

 

Featured Threads

Hot Threads This Week

Hot Threads This Month