无法从查询字符串 cordova windows phone 中获取值
Can't get value from query string cordova windows phone
如何从查询字符串中获取值 index.html#id=1&name=Lionel
下面的代码在设置时有效吗?而不是上面的 # link.
示例:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\[").replace(/[\]]/, "\]");
var regex = new RegExp("[\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
好吧,您的问题有一个简单的答案。假设 url 是 www.awsomepage.dev?foo=bar&baz#foobar
命令 location.search
将 return ?foo=bar&baz
因为那是 search
部分。要访问主题标签数据,您必须使用 location.hash
,这将 return #foobar
.
为了让您的代码与 #
一起工作,您应该替换
var regex = new RegExp("[\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
来自
var regex = new RegExp("[\#&]" + name + "=([^&#]*)"),
results = regex.exec(location.hash);
我改变了什么:
首先:在您的正则表达式中,您检查 ?
和 &
作为开始。您显然必须将其更改为 #
和 &
.
第二:将 location.search
更改为 location.hash
因为您的目标是哈希。
希望对您有所帮助。如需进一步阅读,我建议您在此处查看:https://developer.mozilla.org/en-US/docs/Web/API/Window/location
如何从查询字符串中获取值 index.html#id=1&name=Lionel
下面的代码在设置时有效吗?而不是上面的 # link.
示例:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\[").replace(/[\]]/, "\]");
var regex = new RegExp("[\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
好吧,您的问题有一个简单的答案。假设 url 是 www.awsomepage.dev?foo=bar&baz#foobar
命令 location.search
将 return ?foo=bar&baz
因为那是 search
部分。要访问主题标签数据,您必须使用 location.hash
,这将 return #foobar
.
为了让您的代码与 #
一起工作,您应该替换
var regex = new RegExp("[\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
来自
var regex = new RegExp("[\#&]" + name + "=([^&#]*)"),
results = regex.exec(location.hash);
我改变了什么:
首先:在您的正则表达式中,您检查 ?
和 &
作为开始。您显然必须将其更改为 #
和 &
.
第二:将 location.search
更改为 location.hash
因为您的目标是哈希。
希望对您有所帮助。如需进一步阅读,我建议您在此处查看:https://developer.mozilla.org/en-US/docs/Web/API/Window/location