Javascript 新 URL(location.href) 在 IE 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48447629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
new URL(location.href) doesn't work in IE
提问by Reddy SK
I am facing to problem with method new URL('address') in IE.
我在 IE 中遇到方法 new URL('address') 的问题。
I have this code:
我有这个代码:
var href = location.href;
var hrefParams = new URL(href);
var api = hrefParams.searchParams.get("api");
In Firefox and Chrome it works at should and I will get the value of attribute "api".
在 Firefox 和 Chrome 中,它适用于 should,我将获得属性“api”的值。
But in IE I am getting error on console:
但在 IE 中,我在控制台上收到错误:
SCRIPT445: Object doesn't support this action
SCRIPT445:对象不支持此操作
Console error debugger points to the problem with line
控制台错误调试器指向 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.
但它不能解决这个问题。
Any idea how to fix it in IE?
知道如何在 IE 中修复它吗?
采纳答案by Reddy SK
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.
我忘记了在哪里找到的,但它正在按我的需要工作。
回答by Daniel A. White
IE does not support URL. You will have to add a polyfill for it.
IE 不支持URL。你必须为它添加一个 polyfill。
回答by trev
Another solution I've been using if anyone is interested
如果有人感兴趣,我一直在使用的另一个解决方案
function getParameterByName(name) {
name = name.replace(/[\[]/, "\[").replace(/[\]]/, "\]");
var regex = new RegExp("[\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
getParameterByName('api');
回答by Alex83690
This method is not supported by IE
IE 不支持此方法
See https://developer.mozilla.org/en-US/docs/Web/API/URL#AutoCompatibilityTable
请参阅https://developer.mozilla.org/en-US/docs/Web/API/URL#AutoCompatibilityTable
you should use a lib like jquery deparam or retrieve the parameters with String.split() method or use this function that I made:
您应该使用像 jquery deparam 这样的库或使用 String.split() 方法检索参数或使用我制作的这个函数:
function decodeUriComponentWithSpace (component) {
return decodeURIComponent(component.replace(/\+/g, '%20'))
}
// type : 'hash', 'search' or 'both'
function getLocationParameters (location, type) {
if (type !== 'hash' && type !== 'search' && type !== 'both') {
throw 'getLocationParameters expect argument 2 "type" to be "hash", "search" or "both"'
}
let searchString = typeof location.search === 'undefined' ? '' : location.search.substr(1)
let hashString = typeof location.hash === 'undefined' ? '' : location.hash.substr(1)
let queries = []
if (type === 'search' || type === 'both') {
queries = queries.concat(searchString.split('&'))
}
if (type === 'hash' || type === 'both') {
queries = queries.concat(hashString.split('&'))
}
let params = {}
let pair
for (let i = 0; i < queries.length; i++) {
if (queries[i] !== '') {
pair = queries[i].split('=')
params[this.decodeUriComponentWithSpace(pair[0])] = this.decodeUriComponentWithSpace(pair[1])
}
}
return params
}
// TEST:
window.location.hash = 'test=a&test2=b'
console.log(getLocationParameters(window.location, 'both'))
回答by Bashirpour
Add polyfillcdn script
添加polyfillcdn脚本
<script src="https://cdn.polyfill.io/v2/polyfill.min.js"></script>
回答by Ales
Pure Javascript solution, so you can run it in IE as well without bothering with polyfills:
纯 Javascript 解决方案,因此您也可以在 IE 中运行它,而无需担心 polyfill:
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
Comes from this page: https://html-online.com/articles/get-url-parameters-javascript/
来自这个页面:https: //html-online.com/articles/get-url-parameters-javascript/

