在Firefix(最新的)中,JS decodeuriccomponent返回空字符串



从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的"search"部分(查询字符串),但是您正在使用的页面没有任何查询字符串。您页面上的url看起来像http://www.atomz.host-ed.me/#?l=en。哈希(#)之后的所有内容都是URL 片段的一部分。

window.location.hash.substring(2)代替window.location.search.substring(1)

(或者去掉URL片段开头的问号,使用window.location.hash.substring(1))

尝试使用window.location.hrefdocument.URL获取当前页面的完整URL。关于他们的更多信息:

  1. https://developer.mozilla.org/en-US/docs/Web/API/Document/location
  2. https://developer.mozilla.org/en-US/docs/Web/API/Document/URL

相关内容

  • 没有找到相关文章

最新更新