views:

663

answers:

6

I have this in a test.html:

<script>
    function h(){
       g();
    }
<script>

k.js has:

g();

In test.html I successfully entered h() but not g(). I added k.js as a script tag: scriptTag.src="k.js"

It is not finding g() just the same.

What am I doing wrong?

A: 

Have you ensured that k.js is loaded before calling h()?

Is it successfully finding k.js and the path resolves correctly?

Albert
A: 

Make sure the function you are calling is higher up in the DOM than the line calling it.

Chris Ballance
Im sure the script with the k.js source is higher up in the dom then 'h()' because i checked with firebug. im also sure that k.js is loaded before calling h() because im invoking h() when im clicking a link (anchor tag) from the .html
A: 

I added k.js as a script tag [ scriptTag.src="k.js" ]

Sounds like you're writing your script tag dynamically? If so, make sure you're injecting it into the DOM then waiting for the browser to load it before you try and access anything from it. Just creating the script node won't do anything until you inject it somewhere.

bck
i edit the script like this: function h(){ alert('1'); g(); alert('2'); } and it is only doing the first alert!
A: 

Does your code look someyhing like this?

var scriptTag = document.createElement('script');
scriptTag.src = 'k.js';

Really, you ought to have this line:

scriptTag.type = 'text/javascript';

And as previously mentioned, the script has to be inserted into the DOM. These two lines should solve the problem:

var head = document.getElementsByTagName('head')[0];
head.appendChild(scriptTag);

resulting in:

var scriptTag = document.createElement('script');
scriptTag.src = 'k.js';
scriptTag.type = 'text/javascript';

var head = document.getElementsByTagName('head')[0];
head.appendChild(scriptTag);

Now why aren't you using this?

<script type="text/javascript" src="k.js" />
Eric
i did it exactly like this:var scriptTag = document.createElement('script');scriptTag.src = 'k.js';scriptTag.type = 'text/javascript';var head = document.getElementsByTagName('head')[0];head.appendChild(scriptTag);since im adding it dynamically...
And did it work?
Eric
sadly no. then i tried calling g() from a script in a lower level in the dom in a try and catch and the message was "Refernce error: g is not defined"
+2  A: 

You said that k.js has this:

g();

This isn't a valid function to call, it's just trying to call g(); which still isn't defined.

What k.js should have is something like this:

function g() { alert('you called g()'); }
Nick Craver
+1  A: 

It sounds like you are trying to add the script when the document loads or whenever an event fires. If you try to call a function immediately after the external file that contains it is added to the dom, you will get an error. The external file will not have been downloaded. There is latencey in this process. The computer cycles through the client side script faster than it can dowload the .js file. Ad a set timeout and you should see it working. I could be wrong but that is my thoughs...

John