views:

494

answers:

4

Dear all

I am Extract the Data from the link. It works fine in firefox,But GoogleChrome didn't support.This functionality. Is there better way or alter procedure to extract the query string contents My Link http://safalra.com/web-design/javascript/parsing-query-strings/

   <script type="text/javascript">
     var _url =window.location
     alert(_url);
     var queryData = parseQueryString(_url.toString());
     var p=queryData['http://localhost/new_logic/test.php?a'];
     alert(queryData);
     var q=queryData['b'];
     </script>

Any one suggest the help

+6  A: 

The parseQueryString() function created by the author of the page you linked to obviously invokes some Javascript that does not run on Chrome.

This does not mean that Chrome does not support QueryStrings in general. It appears that you have assumed that the parseQueryString() function is global in nature and is inherently built into all browsers. On the contrary, it is a custom function built by the author.

You would best be advised to email the author using the contact form on the page, or drill into the JS file and find out exactly what is not supported.

Cerebrus
A: 

The script seem to expect only the search part as input but work for me on the last version of chrome

 <script type="text/javascript">
 var _url = window.location.search;
 alert(_url);
 var queryData = parseQueryString(_url.toString());
 var p=queryData['a'];
 alert(p);
 var q=queryData['b'];
 alert(q);
 </script>

Displays

?a=aval&b=bval
aval
bval
VirtualBlackFox
+1  A: 

In the code you used 'parseQuerySTring' wouldn't return anything for a in http://localhost/new_logic/test.php?a, because 'a' has no value. Perhaps you should try http://localhost/new_logic/test.php?a=1&amp;b=2&amp;c=3. Now queryData.a should return 1, queryData.b 2 etc.

KooiInc
+3  A: 

The parseQueryString at http://safalra.com/web-design/javascript/parsing-query-strings/ has some bugs:

  • Multiple + are not replaced with space.
  • Multiple unescaped equal-sign causes content to be lost.
  • Empty input results in an object with key "".
  • Multiple empty keys isn't discarded

In the function below the bugs above has been removed and has been tested with FF3, IE7, Opera9 and Chrome1.

function parseQueryString(input){
    var out={}, decode=decodeURIComponent, s=input||location.search||"",
        kv=("?"===s.charAt(0)?s.slice(1):s).replace(/\+/g," ").split(/[&;]/g),
        idx=-1,key,value;
    while(++idx<kv.length){
        if (kv[idx]==="") continue;
        value=kv[idx].split("=");
        key=decode(value.shift());
        (out[key]||(out[key]=[])).push(decode(value.join("=")));
    }
    return out;
}

The function above (and the original) expects only the query part of the url: From the question mark to the end of the string or to the first #. If no argument is supplied it will automatically extract the query part from the windows current url.

The result is an object with the keys from the query string, and the value of all keys is an array of all the values.

var data = parseQueryString("?a=test1&a=test2&b=test3");

//Result of data:
{
    a:["test1","test2"],
    b:["test3]
}

Some examples:

// Alert all values of the a key:
if (data.a && data.a.length) {
  for (var i=0;i<data.a.length;++i)
    alert(data.a[i]);
} 

// Get the first value of a-key:
var value_a = data.a && data.a[0];

// Get the first value of a-key or default:
var value_a = data.a && data.a[0] || "default";
some