document.getElementById('xxx').src is not working
Hi people, I have the following code. It is not working for firefox, IExplorer and Opera. Could you help me? Which could be the error?
thanks,
<script id="url" name="url" language="JavaScript" type="text/javascript" src=""></script>
<script language="JavaScript" type="text/javascript">
var u = document.getElementByName("url");
var l = document.location;
u.src = 'http://localhost/ato.aspx?&key=6346&refnum=jpwAw&location=' + l;
</script>
A collection is returned
Code:
var u = document.getElements ByName("url")[0]
// or use
var u = document.getElementById("url");
At least 98% of internet users' DNA is identical to that of chimpanzees
no need for the attribute language="JavaScript" - that's taken care of by the type attribute.
Also, it's document.getElements ByName - and it returns a collection of HTML elements. So you need to use:
Code:
document.getElementsByName('url')[0];
Although, you could very easily, (even though you say it doesn't work) just do:
Code:
document.getElementById('url').src = 'http://localhost/ato.aspx?&key=6346&refnum=jpwAw&location='+document.location;
Here's the question - how do you know the JS doesn't work? Also why are you loading a .aspx script into a Javascript src? It's okay if the .aspx script returns a Javascript, I suppose.
I've switched careers...
I'm NO LONGER a scientist,
but now a web developer...
awesome.
Hi, thanks for your answers.
Yes, the aspx returns a javascript code.
But it doesn't work in firefox, iexplorer and opera.
The code is:
<script id="url" name="url" type="text/javascript" src=""></script>
<script type="text/javascript">
var l = document.location;
var u = document.getElementsByName('url')[0];
u.src = 'http://localhost/ClickToCallNew.NetEnvironment/arefto.aspx?&key=6346B5C26380D997A5B90D0FAEC1AA66&refnum=xGaGL+Z0XTjpwAww9NxF/g==&location=' + l;
</script>
or:
<script id="url" name="url" type="text/javascript" src=""></script>
<script type="text/javascript">
var l = document.location;
var u = document.getElementById('url');
u.src = 'http://localhost/ClickToCallNew.NetEnvironment/arefto.aspx?&key=6346B5C26380D997A5B90D0FAEC1AA66&refnum=xGaGL+Z0XTjpwAww9NxF/g==&location=' + l;
</script>
What part of the script doesn't work? The script that changes the src or the script that gets returned and set as the src?
To see if the returned script works, just embed that URL in your page.
From what you've shown, there's no reason why your script that dynamically changes the src of the script element shouldn't work.
I've switched careers...
I'm NO LONGER a scientist,
but now a web developer...
awesome.
And how should I know what part is not working?
The aspx returns a code which contains an image (with some javascript functions). That image is shown only for Chrome and Safari, I need to know why it is not working for Firefox, IExplorer and Opera.
Is it clear?
Originally Posted by
nocuser
And how should I know what part is not working?
Originally Posted by
aj_nsc
To see if the returned script works, just embed that URL in your page.
In your HTML, just put this:
HTML Code:
<script type="text/javascript" src="http://localhost/ClickToCallNew.NetEnvironment/arefto.aspx?&key=6346B5C26380D997A5B90D0FAEC1AA66&refnum=xGaGL+Z0XTjpwAww9NxF/g==&location=XXX" > </script>
But replace XXX with the actual URL in your browser (document.location) and see if the desired result is achieved. If not, then it's not the document.getElementById or document.getElementsByName script that wasn't working in the browsers, it's the script that you are loading that's causing the problem.
I've switched careers...
I'm NO LONGER a scientist,
but now a web developer...
awesome.
Yes, it works for all browsers.
Remove your src attribute initially so you just have this:
Code:
<script id="url" name="url" type="text/javascript"></script>
That made it work for me.
I've switched careers...
I'm NO LONGER a scientist,
but now a web developer...
awesome.
Thanks, it's a good answer... but it is not working for IExplorer (for me). For the others it is working very well.
Are you getting a JS error in IE? If so, where is it coming from (the details of the error). Are you loading this script in your head?
Are their functions in this script that need to be called or is it supposed to do something automatically (e.g. as soon as it is loaded)?
I've switched careers...
I'm NO LONGER a scientist,
but now a web developer...
awesome.
I have not been able to get the JS Error, because the IExplorer does not responde, I need to close and restart it...
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks