Document.getElementByID and IE

HI people, the following code is not working for IE.
Could you help me, please?
Thanks,

<script id=url language=javascript ></script>
<script type=“text/javascript”>document.getElementById(“url”).src = “http://localhost/aref.aspx?&key=C763&refnum=yY9&location=” + location;</script>

It is done so as an example. You know…show how to use the function?

Why add a script when you’ve already got one that’s usable?
You don’t need to bother loading jQuery. It’s not needed in this instance. The changeScript function will work. Works on all my browsers, my friends Nokia 5800, all iPhones that have tested it :slight_smile:

BTW, you don’t need the extra script anymore, you can use the existing one provided you have finished with it

also, you can’t use

window.onload=function(){
changeScript("src1.js");
changeScript("src2.js");
}

as it will not load src1.js (just found that out, trying to minimize the number of file being used)
changeScript(“src2.js”) can however be put in src1.js, and both will be called

This will work, even though the script you’re changing is the one you’re using :slight_smile:

<body>
<script type="text/javascript">
function changeScript(livescript_equation) {
oldScript=document.getElementsByTagName("script")[0];
var newEl=document.createElement('script');
newEl.setAttribute('type', 'text/javascript');
newEl.setAttribute('src',livescript_equation);
oldScript.parentNode.replaceChild(newEl,oldScript);
}
window.onload=function(){
changeScript("src1.js");
}
</script>

Unlike CSS stylesheet changers, the javascript will be kept in memory, so you can keep using this method to build layers of scripting. Look in to cache headers and expiry dates as well, really useful when using this type of script

Try this:


function addScript ( uri, callback )
{
  var script = document.createElement( "script" );
      script.src = uri;
  
  // Callback for when the script fully loads.
  if ( typeof callback === "function" ) {
    script.onload = script.onreadystatechange = function () {
      if ( !( r = this.readyState ) || r === "loaded" || r === "complete" )
        callback( uri );
    };
  }

  document.documentElement.childNodes[0].appendChild( script );
}

addScript( "http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js",
  function () { alert( jQuery.fn.jquery ); }
);

Thanks, but I need that the script, which is returned by my aspx, is in some place of the body… not head…

I suggest you try this:

var hh = document.getElementsByTagName(‘head’)[0];

a.htm


<head>

<script type="text/javascript">

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

var s = document.createElement('script');

s.setAttribute("type","text/javascript");

s.setAttribute("src","a.js");

h.appendChild(s);

alert(h.innerHTML);

</script>

</head>
<body>

a.js


var n="The Time Through Ages. In the Name of Allah, Most Gracious, Most Merciful. 1. By the Time, 2. Verily Man is in loss, 3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.";

alert(n);

Hi, this code is creating the script element dynamically, but it doesn’t work for IE…

<script name="t1">
</script>
<script type='text/javascript'>
var ss = document.createElement('script');
var scr = 'http://localhost/aref.aspx?&key=634&ref=xGa&location=' + document.location;
ss.src = scr;
var hh = document.getElementsByName('t1')[0];
hh.appendChild(ss);
</script>

Any ideas?

Thanks, but it doesn’t work for IE…

I’m assuming the variable “location” is defined elsewhere.

Btw your original markup is kind of a mess (no offense.) You should always wrap attribute values in double quotes whenever possible.

Anyway, see if that works.


<script id="url" type="text/javascript" src=""></script>
<script type="text/javascript">
var scriptTag = document.getElementById("url");
scriptTag.setAttribute("src","http://localhost/aref.aspx?&key=C763&refnum=yY9&location="+ location);
</script>

Create the script element, do not use an existing one.