homepage Welcome to WebmasterWorld Guest from 54.167.185.110
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
JavaScript
Calling external JavaScript from within a JavaScript
medic




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

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

 

Brett_Tabke




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

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

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

medic




msg:1473318
 1:16 am on Jul 11, 2001 (gmt 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?

medic




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

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

tedster




msg:1473320
 10:21 am on Jul 11, 2001 (gmt 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).

medic




msg:1473321
 10:58 am on Jul 13, 2001 (gmt 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). :-)

MikeFoster




msg:1473322
 10:39 pm on Jul 24, 2001 (gmt 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>

medic




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

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

phzzz




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved