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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Won't execute my JS function?

Status
Not open for further replies.

milton747

Programmer
Apr 21, 2005
133
US
Hi,
I added a JS file to my HTML file programatically.
It appended just fine, but it wont execute the function inside the JS file. Can any see what I'm doing wrong? Thanks.
-----------------------------------

<html>
<head>
<script type="text/javascript">
function FindTotal(first,next,last)
{
var total = first + next + last;
document.write ("Your total is " + total + " . ");
}
</script>
</head>

<body>
<script type="text/javascript">
FindTotal(2,3,4);
document.write("T1");
var ns = document.createElement("script");
ns.type = 'text/javascript';
ns.src = './js8.js';
document.getElementsByTagName('head')[0].appendChild(ns);
document.write("T2");
var i;
var ip = document.getElementsByTagName("script");
for (i = 0; i < ip.length; i++)
{
var type = ip.src;
document.write(type);
}
document.write(j8());
document.write("END");
</SCRIPT>
</body>
</html>

--------------------
js8.js is:

function j8()
{ alert('88888888888888'); }

--- eom ------------
 
[&nbsp;}

I don't see anywhere that you 'told' the HTML page that your js8.js file even exists. Therefore, I don't see how your function 'j8()' will ever fire.

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
Jim,
He is dynamically creating the link to that JS file :

var ns = document.createElement("script");
ns.type = 'text/javascript';
ns.src = './js8.js';
document.getElementsByTagName('head')[0].appendChild(ns);

Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005 & 2006
 
Thanks Greg.
Even after I dynamically add js8.js to the HTML page,
it does not run j8() which is inside js8.js?
I can't see why not.

Best Regs,
Milt.
 
I'd check the resultant HTML to see what's exactly been added.

Maybe you should use quotes or maybe you're including the scripo but no call to it or maybe ./ is not pointing to the correct path



Cheers,
Dian
 
I'd check Greg's advice and make sure it works if you include the file normally.

If it does, then try waiting for the file to load (see thread216-1561626)

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top