javascript - 新 URL(location.href) 在 IE 中不起作用

标签 javascript internet-explorer

我在 IE 中遇到方法 new URL('address') 的问题。

我有这个代码:

var href =  location.href;
var hrefParams = new URL(href);
var api = hrefParams.searchParams.get("api");

在 Firefox 和 Chrome 中,它可以正常工作,我将获得属性“api”的值。

但在 IE 中,我在控制台上遇到错误:

SCRIPT445: Object doesn't support this action



控制台错误调试器指向行的问题
var hrefParams = new URL(href);

为了解决另一个问题,我已经调用了脚本
<script type="text/javascript" src="js/bluebird.min.js"></script>

但它不能解决这个问题。

知道如何在 IE 中修复它吗?

最佳答案

最后,我通过以下代码解决了这个问题:

function getQueryString() {
          var key = false, res = {}, itm = null;
          // get the query string without the ?
          var qs = location.search.substring(1);
          // check for the key as an argument
          if (arguments.length > 0 && arguments[0].length > 1)
            key = arguments[0];
          // make a regex pattern to grab key/value
          var pattern = /([^&=]+)=([^&]*)/g;
          // loop the items in the query string, either
          // find a match to the argument, or build an object
          // with key/value pairs
          while (itm = pattern.exec(qs)) {
            if (key !== false && decodeURIComponent(itm[1]) === key)
              return decodeURIComponent(itm[2]);
            else if (key === false)
              res[decodeURIComponent(itm[1])] = decodeURIComponent(itm[2]);
          }

          return key === false ? res : null;
}

...
        var api = getQueryString('api');

我忘记了我在哪里找到的,但它正在按我的需要工作。

关于javascript - 新 URL(location.href) 在 IE 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48447629/

相关文章:

javascript - IE Date.toLocaleTimeString().split 不返回字符串数组

javascript - Emacs - 用另一个字符串模式替换一些字符串模式

javascript - Chrome 显示网页来源而不是应用程序布局。

javascript - $喷油器 :modulerr error in IE 11 Using AngularJS

javascript - IE 无法正确循环 HtmlCollection

javascript - IE 的 toLocaleString 在结果中有奇怪的字符

javascript - 函数内javascript中的setTimeout

javascript - Node.js 如何将冒号分隔的键值对序列化为 JSON

javascript - Skype SDK 入口点登录用户错误 : NoFQDN

javascript - 下拉在 ie8 和 ie9 中不起作用