javascript 查询字符串 > window.location.search.substring > 使用 # 而不是 ? 开始查询字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12082540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript query string > window.location.search.substring > using # instead of ? to start query string
提问by Rowe Morehouse
I'm trying to extract hash substring values from window.locationand write them into HTML for the user to see, with an URL syntax that disallows use of ?
as query string delimiter.
我正在尝试从 window.location 中提取哈希子字符串值并将它们写入 HTML 以供用户查看,其 URL 语法不允许?
用作查询字符串分隔符。
Ok, so I have this great code example, thanks to @Gabe:
好的,感谢@Gabe,我有了这个很棒的代码示例:
<html>
<input type="button" id="test" value="Test" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script>
$(function() {
$('#test').click(function() {
window.location = (window.location);
GetURLParameter('source');
});
});
function GetURLParameter(sParam) {
var sPageURL = window.location.hash.substring(1);
console.log(sPageURL);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++) {
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam) {
alert(sParameterName[1]);
return sParameterName[1];
}
}
}
</script>
</html>
I made one modifcatin to his example, so we're using the actual window location from the client ::
我对他的例子做了一个修改,所以我们使用客户端的实际窗口位置::
window.location = (window.location);
When I hit the "test" submit button on a page containing the above code (the URL is formed like this):
当我在包含上述代码的页面上点击“测试”提交按钮时(URL 是这样形成的):
http://example.com/pagename.html#source=FOO&medium=BAR&campaign=FRED
http://example.com/pagename.html#source=FOO&medium=BAR&campaign=FRED
(notice no ?
as query string delimiter, just #
)
(注意没有?
作为查询字符串分隔符,只是#
)
... then I get a successful alert, that shows the source "FOO" from the URL. Awesome.
...然后我收到一个成功的警报,显示来自 URL 的源“FOO”。惊人的。
But how to I get the other two SParamaterNames, and then how do I write them into the page as HTMLrather than return an alert.
但是如何获取其他两个 SParamaterNames,然后如何将它们作为 HTML 写入页面而不是返回警报。
I know this is a basic question for you guys. I'm here because I have been trying to solve this problem for days now, looking at many diff solutions. Any help is greatly appreciated!! thank you very much to all you wizards.
我知道这对你们来说是一个基本问题。我来这里是因为我已经尝试解决这个问题好几天了,查看了许多不同的解决方案。任何帮助是极大的赞赏!!非常感谢你们所有的巫师。
回答by Gabe
Just get the hash
value instead of the search
value from the location
object
只需获取hash
值而不是search
从location
object
Hereis a working example.
这是一个工作示例。
function GetURLParameter(sParam)
{
var sPageURL = window.location.hash.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}