新的URL(location.href)在IE中不起作用 [英] new URL(location.href) doesn't work in IE
问题描述
我在IE中遇到方法new URL('address')的问题.
I am facing to problem with method new URL('address') in IE.
我有此代码:
var href = location.href;
var hrefParams = new URL(href);
var api = hrefParams.searchParams.get("api");
在Firefox和Chrome中,它可以正常工作,我将获得属性"api"的值.
In Firefox and Chrome it works at should and I will get the value of attribute "api".
但是在IE中,控制台上出现错误:
But in IE I am getting error on console:
SCRIPT445:对象不支持此操作
SCRIPT445: Object doesn't support this action
控制台错误调试器指向第
Console error debugger points to the problem with line
var hrefParams = new URL(href);
为解决另一个问题,我已经在调用脚本
For solving of another problem I already invoking script
<script type="text/javascript" src="js/bluebird.min.js"></script>
但是它不能解决这个问题.
But it doesn't fix this problem.
有人知道如何在IE中修复它吗?
Any idea how to fix it in IE?
推荐答案
最后,我通过以下代码修复了该问题:
At the end I have fixed that by this code:
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');
我忘记了在哪里找到的,但是它可以按需工作.
I forgot where I found that but it is working as I needed.
这篇关于新的URL(location.href)在IE中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!