Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

createElement( "script" ) in Mozilla 1

Status
Not open for further replies.

theboyhope

IS-IT--Management
Jul 11, 2001
772
GB
Say you have this in an html page ...
Code:
<head>

<script type="text/javascript">

window.onload = function () {

   var el = document.createElement( "script" );
   el.setAttribute( "src", "new_js.js" );
   el.setAttribute( "type", "text/javascript" );
   document.getElementsByTagName( "head" )[ 0 ].appendChild( el ); 

   testing();
}

</script>

</head>

And the new_js.js file contains ...
Code:
alert ( "New stuff loaded." );

testing();

function testing() {

	alert( "Testing function." );
}

The alert will run and testing() will run when called from new_js.js but I get a "not defined" error when calling testing() from the original page.

I suppose this could be caused by one of a number of extensions I have installed for Mozilla. Can anyone reproduce the fault?

Thanks.
 

function testing() {

alert( "Testing function." );
}
testing();


it may be because of the order, the function defenition comes first then the call to the function...

Known is handfull, Unknown is worldfull
 
No, that call from the .js file works, mate. It's when I call it from the HTML page that I get "not defined", even though I've already just called it from the .js file.

Weird, eh.
 
theboyhope,

Must be some delay in compile of the append??

Setting a timeout made this work.

Code:
<script type="text/javascript">

window.onload = function () {

   var el = document.createElement( "script" );
   el.setAttribute( "src", "js.js" );
   el.setAttribute( "type", "text/javascript" );
   document.getElementsByTagName( "head" )[ 0 ].appendChild( el );

   setTimeout("testing('in Append')",10);
}

</script>
 
It does indeed! Good one, Lrnmore.

It's very strange to have to do that when I've already called it from somewhere else without a problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top