Welcome to WebmasterWorld Guest from 23.20.221.93

Forum Moderators: open

Message Too Old, No Replies

JavaScript

Calling external JavaScript from within a JavaScript

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

New User

10+ Year Member

joined:Apr 15, 2004
posts:2
votes: 0


Hi,
Is there a way to call an external JavaScript from within a JavaScript?
12:58 pm on July 10, 2001 (gmt 0)

Administrator from US 

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

joined:Sept 21, 1999
posts:38061
votes: 13


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

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

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

New User

10+ Year Member

joined:Apr 15, 2004
posts:2
votes: 0


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?

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

New User

10+ Year Member

joined:Apr 15, 2004
posts:2
votes: 0


duh..... why can't it work?
am i missing something or did something not right?
10:21 am on July 11, 2001 (gmt 0)

Senior Member

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

joined:May 26, 2000
posts:37301
votes: 0


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

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

New User

10+ Year Member

joined:Apr 15, 2004
posts:2
votes: 0


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). :-)
10:39 pm on July 24, 2001 (gmt 0)

Junior Member

10+ Year Member

joined:July 20, 2001
posts:137
votes: 0


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>
8:05 am on Aug 6, 2001 (gmt 0)

New User

10+ Year Member

joined:Apr 15, 2004
posts:2
votes: 0


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

phzzz

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

Inactive Member
Account Expired

 
 


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