Problem with dynamically added script element - attribute src is empty

Posted by Stazh on Stack Overflow See other posts from Stack Overflow or by Stazh
Published on 2010-05-19T10:59:21Z Indexed on 2010/05/19 11:10 UTC
Read the original article Hit count: 209

Filed under:
|
|
|

Hi there,

I have a problem with dynamically added script element (using jQuery). Code for adding new script element to DOM is this:

var pScript = document.createElement("script");
pScript.type = "text/javascript";
pScript.src = sFile;


// Add element to the end of head element
$("head").append(pScript);

The script is added with no problem, and the code runs perfectly.

But, the problem occurs when I try to find the newly added script. I use this code to iterate through all script elements:

var bAdd = true;
$("script").each(function()
{
  if(this.src == sFile)
    bAdd = false;
});

(I need this code to prevent adding script that is already loaded)

Problem is that all other script elements have src attribute set, but the newly added (dynamically) has not...

Any idea?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery