SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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>

  2. #2
    SitePoint Member
    Join Date
    May 2010
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    Code:
    <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>
    Last edited by Madisonjs; Jun 4, 2010 at 08:34. Reason: typo

  3. #3
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but it doesn't work for IE...

  4. #4
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Create the script element, do not use an existing one.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, this code is creating the script element dynamically, but it doesn't work for IE...

    Code:
    <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?

  6. #6
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suggest you try this:

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

    a.htm
    Code JavaScript:
    <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
    Code JavaScript:
    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);
    The Time Through Ages
    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.

  7. #7
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but I need that the script, which is returned by my aspx, is in some place of the body... not head...

  8. #8
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    http://www.sitepoint.com/forums/showthread.php?t=135179

    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
    Code:
    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
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  9. #9
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    This will work, even though the script you're changing is the one you're using
    Code:
    <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
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  10. #10
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    Code JavaScript:
    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 ); }
    );
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  11. #11
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  12. #12
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Markdidj View Post
    You don't need to bother loading jQuery. It's not needed in this instance.
    It is done so as an example. You know...show how to use the function?
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •