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 8 of 8
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts

    is it ok to pass strings with spaces through url

    hi!

    the title says it:
    is it ok to pass strings with spaces trough url?

    i tried it and it seems to work, but it also seems to me that more correct way to do it is to use '+' instead of space since that's what php get method does.
    i'm using window.location to redirect to another page.

  • #2
    New Coder
    Join Date
    Nov 2008
    Posts
    59
    Thanks
    5
    Thanked 4 Times in 4 Posts
    As far as I'm aware, the space is not a legal character in URLs. Some browsers may accept it and others not.

  • #3
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    depending on how you generate and load the url, spaces are generally converted into "%20" in the actual url string and then converted back into spaces when read by JS or server-side query string requests.

    though i would recommend avoiding spaces, as long as you encode and decode your url querystrings correctly, there's no problem with them.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,277
    Thanks
    10
    Thanked 581 Times in 562 Posts
    DOM properties like window.location, a.href, script.src, etc, will handle this automatically, just like they fully qualify URLs.

    note that "+" is not the same as "%20", so make sure you are using the proper encoding for your context. they are not always interchangeable...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #5
    New Coder
    Join Date
    May 2009
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts
    thank you for a reply. Could you explain more about:

    Quote Originally Posted by rnd me View Post
    DOM properties like window.location, a.href, script.src, etc, will handle this automatically, just like they fully qualify URLs.
    Does it mean that i can do window.location = string with spaces and it will be ok?

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Code:
    var str = "something with spaces";
    str = str.replace(/\s+/g,"%20");
    window.location = str;

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,277
    Thanks
    10
    Thanked 581 Times in 562 Posts
    Quote Originally Posted by helavissa View Post
    thank you for a reply. Could you explain more about:



    Does it mean that i can do window.location = string with spaces and it will be ok?
    probably
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #8
    New Coder
    Join Date
    May 2009
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts
    thank you


  •  

    Posting Permissions

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