Jarda
Jarda

Reputation: 596

Javascript can't find the script tag's id

I am facing problem with javascript document.getElementByID function. The HTML file is:

...
<script 
   id="scriptID"
   type="text/javascript"
   src="http://external.script.com/file.js">
</script>
...

When the page is loaded, the script is successfully included, but when executing expression from that file (the script is executed automaticaly after loading it):

... = document.getElementById('scriptID').src

The script fails with message saying that "document.getElementById('scriptID') is null". Can anybody tell me, why it is null if the tag is the script tag itself?

Thx for any response.

EDIT:
I don't know if that is relevant, but the page is built in a bit more complicated way. There is page of some product. When the customer orders that product, there is a div loaded by AJAX with some "Thanks for order" and that contains the script. Then the script is executed.

Upvotes: 0

Views: 2393

Answers (1)

Voonic
Voonic

Reputation: 4785

May be your DOM is not ready when you are try to get src of script,

<script id="scriptID" type="text/javascript" src="http://external.script.com/file.js">
</script>

window.onload=function()
{
 alert( document.getElementById('scriptID').src);   
}

Its workinfg fine SEE

Upvotes: 1

Related Questions