gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:35:06 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com