最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Query String in JavaScript - Stack Overflow

programmeradmin6浏览0评论

By using document.referrer we will get all the reference of URL in JavaScript, such as the following:

http://localhost/testwordpress/wp-admin/admin.php?page=thesis-options&upgraded=true

From this output how can we extract the query string part only:

?page=thesis-options&upgraded=true

Is there any method in JavaScript?

By using document.referrer we will get all the reference of URL in JavaScript, such as the following:

http://localhost/testwordpress/wp-admin/admin.php?page=thesis-options&upgraded=true

From this output how can we extract the query string part only:

?page=thesis-options&upgraded=true

Is there any method in JavaScript?

Share Improve this question edited Apr 23, 2010 at 21:17 Daniel Vassallo 345k72 gold badges512 silver badges446 bronze badges asked Apr 21, 2010 at 4:28 AadiAadi 7,10928 gold badges102 silver badges148 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

To get the query string from document.referrer, you can use the split() method:

var qs = document.referrer.split('?')[1];

if (typeof qs !== 'undefined') {
    // qs contains the query string.
    // this would be "page=thesis-options&upgraded=true" in your case.
}
else {
    // there was no query string in document.referrer.
}

If you are just looking to get the values from the query string I use the following function:

function getQuerystring(key)
{
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}

Simply pass in the key you are looking for and get the value back. IE: getQueryString('upgraded') would return true

There are some functions around to do that. See this for example.

发布评论

评论列表(0)

  1. 暂无评论