对不起,如果我重复了这个问题.
我试图在我的jQuery中执行URLSearchParams函数,但我在IE 11上遇到错误,而它在Chrome和Mozilla中完美运行.实际上它在IE11中不受支持.让我知道我们如何能够支持IE.我在Laravel 5.4中执行代码.
我做了研究,但无法得到解决方案.
这是我尝试执行的代码行.
var urlParams = new URLSearchParams(window.location.search);
错误:
SCRIPT5009:'URLSearchParams'未定义
Nar*_*ari 55
通过使用给定示例替换代码获得解决方案:
$.urlParam = function(name){
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (results == null){
return null;
}
else {
return decodeURI(results[1]) || 0;
}
}
Run Code Online (Sandbox Code Playgroud)
example.com?param1=namem2=&id=6
$.urlParam('param1'); // name
$.urlParam('id'); // 6
$.urlParam('param2'); // null
Run Code Online (Sandbox Code Playgroud)
小智 22
Odhikari作为(部分)polyfill的答案:
(function (w) {
w.URLSearchParams = w.URLSearchParams || function (searchString) {
var self = this;
self.searchString = searchString;
self.get = function (name) {
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(self.searchString);
if (results == null) {
return null;
}
else {
return decodeURI(results[1]) || 0;
}
};
}
})(window)
Run Code Online (Sandbox Code Playgroud)
小智 17
如果有人在 2019 年发现了这一点,那么 corejs 现在也支持 URLSearchParams 作为 polyfill,因此您可以继续使用 corejs,而不必自己编写一些东西。
import 'core-js/features/url-search-params';
Run Code Online (Sandbox Code Playgroud)
https://github.com/zloirock/core-js#url-and-urlsearchparams
小智 10
我能够通过使用url-search-params-polyfill来解决这个问题。只需导入到您使用 URLSearchParams 的文件中:
import 'url-search-params-polyfill';
Run Code Online (Sandbox Code Playgroud)
您对 URLSearchParams 的现有引用现在应该可以使用了!