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
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy URI that UnfortunatelyHasBrackets[]=thisalsohas:acolon in JS

    My HTML page loads with a sent visitor. The request looks like this:

    http://mydomain.com/index.html?success_url[]=http://www.google.com

    (sometimes browsers seem to request this instead

    http://mydomain.com/index.html?success_url[]=http%3A%2F%2Fwww.google.com

    And I need to do some differing Javascript stuff depending on what URL is passed as an URI string. I got it working in my own installation of IE, Chrome & Firefox, but when I pushed it live it apparently did not work for a lot of people. I have not been able to replicate any problems but I know it didn't work for one person running Chrome on a Mac.

    Here is the code I was running:

    Code:
    <script language="JavaScript">
    var qsParm = new Array();
    function qs() {
    var query = window.location.search.substring(1);
    query = query.replace('[]', '');
    var parms = query.split('&');
    for (var i=0; i<parms.length; i++) {
    var pos = parms[i].indexOf('=');
    if (pos > 0) {
    var key = parms[i].substring(0,pos);
    var val = parms[i].substring(pos+1);
    qsParm[key] = val;
    }
    }
    }
    qsParm['success_url'] = null;
    qs();
    
    var suxess;
    
    suxess = qsParm['success_url'];
    suxess = decodeURIComponent(suxess);
    
    switch(suxess)
    {
    case 'http://google.com':   
    sitename = 'Google';
    break;
    case 'http://facebook.com': 
    sitename = 'Facebook';
    break;
    }
    </script>
    I'm not sure how to proceed with this. My google powers failed to yield anything beyond producing the code above. I can't find anything specific regarding brackets in URI string names, and colons, slashes in URIs.. And I've learned everything backwards so I don't know what I should read up on.

    Can anyone see any reason as to why this would fail for anyone?

    PS. I have no influence over the URI content.

    PPS. Is it possible it fails because the brackets gets encoded for some and is therefore not removed in line 5 of the posted code?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Well, seeing brackets in a URL like that is common: PHP requires those if the <form> field in question can have multiple values, as is the case with checkboxes. That is, a PHP <form> might look like this:
    Code:
    <form action="whatever.php" method="get">
    <input type="checkbox" name="food[]" value="apple" />
    <input type="checkbox" name="food[]" value="banana" />
    <input type="checkbox" name="food[]" value="canteloupe" />
    ...
    </form>
    So clearly browsers need to be able to handle field names with brackets.

    (Usually, PHP forms would be method="post", but there is certainly no requirement that they be.)

    My question to you: Why do you try to remove the []? Are you saying that sometimes they are *NOT* present?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,189
    Thanks
    10
    Thanked 569 Times in 550 Posts
    i recommend a true parser:

    Code:
    function parseQS(str){
      var ob={}, float="", key="", dc=decodeURIComponent;
    
    for( var i=0, mx=str.length; i<mx;i++){
      var it=str[i];
        if(it==="="){ key=float; float=""; continue;}
        if(!it.search(/^[?&]/)){ 
            if(it==="&" && str.slice(i+1,i+5)==="amp;"){ i=(i+4);float+="&"; continue;}
            if(key){ob[key]=dc(float);} key=""; float="";  continue;
        }
        float+=it;
    }
    ob[key]=dc(float);
    return ob;
    }
    
    var u="http://mydomain.com/index.html?success_url[]=http://www.google.com"
    var ob=parseQS( u.split("[]").join("") );
    
    alert(ob.success_url);
    which shows the same for http://mydomain.com/index.html?success_url[]=http://www.google.com as for http://mydomain.com/index.html?success_url[]=http%3A%2F%2Fwww.google.com
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    This version has the advantage that it leaves the key value alone. I still don't see why you want to convert success_url[] to success_url. It also handles multiple occurrences of the key value, whether PHP style (with the brackets) or JSP/ASP/ASP.NET style (without brackets).
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <style type="text/css">
    * { font-size: x-large; }
    span { color: red; font-weight: bold; }
    </style>
    </head>
    <body>
    <div>
    <a href="junk2.html?success_url[]=http%3A%2F%2Fwww.google.com">test 1</a><br/>
    <a href="junk2.html?success_url[]=http://www.google.com">test 2</a><br/>
    <a href="junk2.html?success_url[]=http://www.google.com&other=fun%20stuff">test 3</a><br/>
    <a href="junk2.html?success_url[]=http://www.google.com&other=fun%20stuff&other=and%20more">test 4</a><br/>
    <a href="junk2.html?success_url[]=http://www.google.com&success_url[]=http://www.yahoo.com">test 5</a><br/>
    <hr/>
    success_url[] is <span id="found"></span><br/>
    other is <span id="other"></span><br/>
    </div>
    
    <script type="text/javascript">
    (
      function( )
      {
          var qs = [];
          if ( location.search.length > 1 )
          {
              var pairs = location.search.substring(1).split("&");
              for ( var p = 0; p < pairs.length; ++p )
              {
                  var pair = pairs[p].split("=");
                  var name = pair[0];
                  if ( qs[name] == null ) { qs[name] = []; }
                  qs[name].push( decodeURIComponent(pair[1]) );
              }
          }
          document.getElementById("found").innerHTML = qs["success_url[]"];
          document.getElementById("other").innerHTML = qs["other"];
      }
    )();
    </script>
    </body>
    </html>
    Yes, it always returns an array, just in case the name is multiply-defined. But if you convert the one-element array to a string, then you get just the one value.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for your answers OP and rnd me!

    Quote Originally Posted by Old Pedant View Post
    Well, seeing brackets in a URL like that is common: (..) So clearly browsers need to be able to handle field names with brackets. (..)

    My question to you: Why do you try to remove the []? Are you saying that sometimes they are *NOT* present?
    Good lesson. The reason I am removing them is because this didn't work for me:
    Code:
    qsParm['success_url[]'] = null;
    but this did
    Code:
    qsParm['success_url'] = null;
    not sure why this would occur, I might have made a mistake here, looking at the code you wrote above it looks like you wrote something similar so I guess I mistook something.

    And also, I noticed that on Mac, the brackets become %-encoded, so earlier today I added another row to the code and pushed to production, but still no improvement:

    Code:
    query = query.replace('[]', '');
    query = query.replace('%5B%5D', '');
    I hoped that that was the problem, but it still didn't work.

    Quote Originally Posted by rnd me View Post
    i recommend a true parser
    which shows the same for http://mydomain.com/index.html?success_url[]=http://www.google.com as for http://mydomain.com/index.html?success_url[]=http%3A%2F%2Fwww.google.com

    Quote Originally Posted by Old Pedant View Post
    This version has the advantage that it leaves the key value alone. I still don't see why you want to convert success_url[] to success_url. It also handles multiple occurrences of the key value, whether PHP style (with the brackets) or JSP/ASP/ASP.NET style (without brackets).
    Yes, it always returns an array, just in case the name is multiply-defined. But if you convert the one-element array to a string, then you get just the one value.
    Thank you so much for the code suggestions you have written, I will try these with some visitors and see if they remedy my problem. If it does not I must be missing something elsewhere.


  •  

    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
    •