开发者

Accessing CGI values with Prototype

开发者 https://www.devze.com 2022-12-08 14:58 出处:网络
Anyone know the quickest way to gra开发者_运维技巧b the value of a CGI variable in the current URL using Prototype?So if I get redirected to a page with the following URL:

Anyone know the quickest way to gra开发者_运维技巧b the value of a CGI variable in the current URL using Prototype? So if I get redirected to a page with the following URL:

http://mysite.com/content?x=foobar

I am interested in retrieving the value of "x" (should be "foobar") in a function called on page load like this:

Event.observe(window, "load", my_fxn ());

Thanks


You may want to look at the parseQuery method. This should do all the splitting you'd expect on a standard querystring such as document.location.search

http://api.prototypejs.org/language/string.html#parsequery-instance_method

For example:

document.location.search.parseQuery()["x"];

Will be undefined if it's not present, and should be the value otherwise.


I couldn't find any shortcuts here, so in the end I just parsed the URL with js like so:

function my_fxn () { 

  var varsFromUrl = document.location.search;

  // get rid of first char '?'                                                  
  varsFromUrl = varsFromUrl.substring(1);

  var pairsArray = varsFromUrl.split("&");

  for (i = 0; i < pairsArray.length; i++) {
    var pair = pairsArray[i].split("=");
    if (pair[0] == "x")
      alert(pair[1] + ' is what I want.');
  } 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消