Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post

    Why does encodeURIComponent() not work when using innerHTML?

    I just fixed a problem where I had this small piece of javascript that is supposed to place something inside the innerHTML of an element.

    I was copying a few lines from one location to another. The encodeURIComponent() function went along for the ride to the new location in the script until I figured out it was preventing the script from working. I deleted it from that new location and the script worked.

    I learned doing it this way doesn't work:
    Code:
    request.onreadystatechange = function()
    {
            if(request.readyState == 4)
            {
                  if(request.status == 200)
                  {
                       var open = encodeURIComponent(document.getElementById('over_here');
                       open.innerHTML = "message"
                          
                  }
            }
    }
    This does work:
    Code:
    request.onreadystatechange = function()
    {
            if(request.readyState == 4)
            {
                  if(request.status == 200)
                  {
                       var open = document.getElementById('over_here');
                       open.innerHTML = "message"
                          
                  }
            }
    }
    What is the function encodeURIComponent()?

    Why doesn't it work with innerHTML?
    Last edited by milesdriven; 06-25-2012 at 10:54 PM.

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    http://www.w3schools.com/jsref/jsref...icomponent.asp

    Code:
    <div id="over_here"></div>
    <script type="text/javascript">
    var open = encodeURIComponent(document.getElementById('over_here'));
    alert(open)
    open = encodeURIComponent("$hello &what's up ?");
    alert(open)
    </script>

  • #3
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks very much.

    It would only apply to a get request(not a post request), because post does not put its key/value pairs in the url.

    Is this correct?
    Last edited by milesdriven; 06-25-2012 at 11:24 PM.

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts

  • #5
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thank you very much for your help.


  •  

    Posting Permissions

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