从 StackOverflow 获取以下代码。它应该解析 URL 中的变量,但是当我在 for 循环中调试 sURLVariables 的值时,它的值始终为空。有什么想法吗?
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'), sParameterName, i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};
最佳答案
您使用的代码查找 URL 的“搜索”部分(查询字符串),但您使用的页面没有任何查询字符串。您页面上的 URL 类似于 http://www.atomz.host-ed.me/#?l=en
。哈希值 ( #
) 之后的所有内容都是 URL 片段的一部分。
而不是 window.location.search.substring(1)
,使用window.location.hash.substring(2)
.
(或者去掉 URL 片段开头的问号并使用 window.location.hash.substring(1)
。)
关于javascript - JS DecodeURIComponent 在 Firefox 中返回空字符串(最新),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39049726/