0

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?

4
  • 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 Commented May 8, 2009 at 18:37
  • i edit the script like this: function h(){ alert('1'); g(); alert('2'); } and it is only doing the first alert! Commented May 8, 2009 at 18:43
  • pls it very urgent and i already spent 5 hours on it... Commented May 8, 2009 at 19:23
  • you need to show more code, especially the part thats adding the k.js. have you checked via firebug that your k.js file reference returns the correct javascript file and not a 404 for instance? oh, and every question is urgent. Commented May 8, 2009 at 20:24

6 Answers 6

2

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()'); }
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

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

Comments

0

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

1 Comment

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
0

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.

1 Comment

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

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" />

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.