JavaScript中的查询字符串 [英] Query String in JavaScript

查看:73
本文介绍了JavaScript中的查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过使用 document.referrer ,我们将获得JavaScript中URL的所有引用,例如:

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

JavaScript中是否有任何方法?

Is there any method in JavaScript?

推荐答案

如果您只想从查询字符串中获取值,请使用以下函数:

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];
}

只需传入您要查找的密钥并获取价值。
IE:getQueryString('升级')将返回true

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

这篇关于JavaScript中的查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆