要将变量附加到散列(如Matthew建议的那样),您可以在纯JavaScript中执行以下操作:
window.location.hash = 'varA=some_value;varB=some_value';
Run Code Online (Sandbox Code Playgroud)
这将添加#varA=some_value;varB=some_value到您的URL.除非哈希值等于文档中的锚名称或元素id,否则它不会刷新页面.
然后检查是否存在哈希值,只需执行以下操作:
var i, variables = window.location.hash.split(';');
if (variables.length > 0) {
// Variables present in hash
for (i = 0; i < variables.length; i++) {
keyValuePair = variables.split('=');
// keyValuePair[0] would be the key (variable name)
// keyValuePair[1] would be the value
}
}
else {
// No variables in the hash
}
Run Code Online (Sandbox Code Playgroud)
您可能还想查看以下Stack Overflow帖子,了解与不同浏览器中哈希部分的URL编码相关的问题: