www.webdeveloper.com
Results 1 to 14 of 14

Thread: document.getElementById('xxx').src is not working

Hybrid View

  1. #1
    Join Date
    Jun 2010
    Posts
    8

    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>

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    A collection is returned
    Code:
    var u = document.getElementsByName("url")[0]
    // or use
    var u = document.getElementById("url");

  3. #3
    Join Date
    Dec 2005
    Posts
    2,984
    no need for the attribute language="JavaScript" - that's taken care of by the type attribute.

    Also, it's document.getElementsByName - 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.

  4. #4
    Join Date
    Jun 2010
    Posts
    8
    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>

  5. #5
    Join Date
    Dec 2005
    Posts
    2,984
    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.

  6. #6
    Join Date
    Jun 2010
    Posts
    8
    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?

  7. #7
    Join Date
    Jun 2010
    Posts
    8
    Yes, the url:

    http://localhost/ClickToCallNew.NetE.../g==&location=

    is working in all browsers.

  8. #8
    Join Date
    Dec 2005
    Posts
    2,984
    Quote Originally Posted by nocuser
    And how should I know what part is not working?
    Quote Originally Posted by aj_nsc View Post
    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.

  9. #9
    Join Date
    Jun 2010
    Posts
    8
    Yes, it works for all browsers.

  10. #10
    Join Date
    Dec 2005
    Posts
    2,984
    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.

  11. #11
    Join Date
    Jun 2010
    Posts
    8
    Thanks, it's a good answer... but it is not working for IExplorer (for me). For the others it is working very well.

  12. #12
    Join Date
    Dec 2005
    Posts
    2,984
    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)?

  13. #13
    Join Date
    Jun 2010
    Posts
    8
    I have not been able to get the JS Error, because the IExplorer does not responde, I need to close and restart it...

  14. #14
    Join Date
    Jun 2010
    Posts
    8
    any ideas?

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
  •  
HTML5 Development Center



Recent Articles