views:

262

answers:

2

I'm trying to read a querystring parameter ("ssip") via jQuery and the query plugin, but it seems to return null instead of the actual value.

Here's is my code:

<script src="jquery-1.3.2.min.js" language="javascript"></script>
<script src="jquery.query-2.7.1.js" language="javascript"></script>

<script language="JavaScript" type="text/javascript">
  function getStreamingServerIP() {
    return $.query.get('ssip');
  }
</script>

I'm calling this method from Flex via ExternalInterface.

Does anyone spot any problems with the above code?

A: 

I reproduced this without any problem my url is :

http://localhost/test/test/Test.html?ssip=foo

getStreamingServerIP() launched from Firebug console return "foo"

Make sure you are making your request against a valid URL and not from a local file loaded in the browser.

Check in Firebug if you have something in > DOM Panel > then in the treeview :

window > location > search

sroucheray
Thanks. I'll try when I get back to work. I was trying this on a local file and not through http, but I wonder if that really was the problem. In the meantime I solved it by taking a substring of location.search.
Christophe Herreman
A: 

This is what I use.

<!doctype html>
<html>
<head>
<script src="jquery-1.4.js"></script>
<script>
/**
 * @return null if param not found
 * @return string if param found once
 * @return array if param found more than once or name ends with "[]"
 */
function getQueryStringParam (name) {
    var result = null;
    if (location.search !== "") {
        var querystring = location.search.substring(1).replace(/\+/g, " ");
        var pairs = querystring.split("&");
        var isFound = false;
        for (var i=0; i<pairs.length; i++) {
            var pair = pairs[i].split("=");
            var key = decodeURIComponent(pair[0]);
            if (key === name) {
                var val = (pair.length === 2 ? decodeURIComponent(pair[1]) : key);
                if (!isFound) {
                    isFound = true;
                    result = [val];
                }
                else {
                    result.push(val);
                }
            }
        }
        if (isFound && result.length == 1 && !/\[.*\]$/.test(name)){
            result = result[0];
        }
    }
    return result;
}
/**
 * helper function
 */
function printQueryStringParam (name) {
    var result = "";
    var val = getQueryStringParam(name);
    if (val === null) {
        result = "null";
    }
    else if (jQuery.isArray(val)) {
        result = "[\n\t" + val.join(",\n\t") + "\n]";
    }
    else {
        result = val;
    }
    return result;
}
</script>
</head>
<body>

<form action="test.html" method="get">
<p>
text <input type="text" name="text" 
value="space: , plus:+, slash:/, backslash:\, amp:&amp;, equals:=, less:<, greater:>"><br>
</p>
<p>
checkbox[]<br>
<input type="checkbox" name="checkbox[]" value="a" checked> a<br>
<input type="checkbox" name="checkbox[]" value="b" checked> b<br>
</p>
<p>
<input type="submit">
</p>
</form>

<hr>

<pre>
<script>
document.write("text: "+printQueryStringParam("text"));
document.write("\ncheckbox[]: "+printQueryStringParam("checkbox[]"));
</script>
</pre>

</body>
</html>
Kristof Neirynck