views:

2127

answers:

4

I'm looking for a jQuery plugin that can get url parameters, and support this search string without outputting Javascript error: malformed URI sequence. If there isn't a jQuery plugin that supports this, I need to know how to modify it to support this.

?search=%E6%F8%E5

The value of the url parameter, when decoded, should be:

æøå

(the characters are norwegian).

I don't have access to the server, so I can't modify anything on it.

+4  A: 

You can use the browser native location.search property:

function getParameter(paramName) {
  var searchString = window.location.search.substring(1),
      i, val, params = searchString.split("&");

  for (i=0;i<params.length;i++) {
    val = params[i].split("=");
    if (val[0] == paramName) {
      return unescape(val[1]);
    }
  }
  return null;
}

But there are some jQuery plugins that can help you:

CMS
+5  A: 
function getURLParameter(name) {
    return unescape(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
}
J-P
A: 

Rick Strahl just posted some handy routines for that: http://www.west-wind.com/Weblog/posts/884279.aspx

Dave Ward
A: 

Sorry for a complete nub question, I don't even know how to ask additional questions in the exsisting threads.

How do I assign the "return" of this function (see selected answer) to a var? Thanks!

Ted
as simple as var somevariable = getParameter('theparameteryouareinterestedin')
DDaviesBrackett
Thank you very much!
Ted