admin 管理员组文章数量: 1086019
This is very odd, using DW CC 2018 (in case that is where the problem is), when i use URLSearchParams
inside a script tag in my HTML page, it does not get flagged as an "error".
I put URLSearchParams
in my external JS file, inside a function, it gets flagged as "not defined". DW flags it as an error, but it still works, so must be more of a "warning" than an error. This has me a bit concerned even if it is a warning, that it could break when going live.
Should I worry, or is it just once of those things to just ignore?
This is very odd, using DW CC 2018 (in case that is where the problem is), when i use URLSearchParams
inside a script tag in my HTML page, it does not get flagged as an "error".
I put URLSearchParams
in my external JS file, inside a function, it gets flagged as "not defined". DW flags it as an error, but it still works, so must be more of a "warning" than an error. This has me a bit concerned even if it is a warning, that it could break when going live.
Should I worry, or is it just once of those things to just ignore?
Share Improve this question edited Feb 22, 2018 at 17:59 Derek 朕會功夫 94.5k45 gold badges198 silver badges253 bronze badges asked Feb 22, 2018 at 17:57 MaxThrustMaxThrust 1372 gold badges2 silver badges9 bronze badges 1- URLSearchParams isn't supported in all browsers, so I would assume different developer platforms would have the same issue. – EliteTech Commented Feb 22, 2018 at 18:06
2 Answers
Reset to default 4Dreamweaver probably uses an outdated list of "expected globals" in its indexer that isn't updated to include the URLSearchParams
API, since it's relatively recent.
If you're not concerned with backwards patibility *cough* IE *cough*, just add this somewhere in the offending file to get Dreamweaver to shut up:
const URLSearchParams = window.URLSearchParams;
If Dreamweaver doesn't support ES6 syntax (I've never used it), then you must add this somewhere that's not at the top level:
(function () {
// must be in a closure
var URLSearchParams = window.URLSearchParams;
...
})();
The reason why is because top-level var
overwrites the global namespace in some browsers.
you can write custom URLSearchParams class
class UrlSearchParams {
constructor(query) {
this.query = query;
}
getSearchObject = () => {
const { query } = this;
return query
? (/^[?#]/.test(query) ? query.slice(1) : query)
.split("&")
.reduce((params, param) => {
let [key, value] = param.split("=");
params[key] = value
? decodeURIComponent(value.replace(/\+/g, " "))
: "";
return params;
}, {})
: {};
};
getAll = () => {
const searchParams = this.getSearchObject();
return searchParams;
}
get = param => {
const searchParams = this.getSearchObject();
return searchParams[param];
};
setUrl = (param, value) => {
const searchParams = this.getSearchObject();
searchParams[param] = value;
return Object.keys(searchParams)
.map(key => key + "=" + searchParams[key])
.join("&");
};
}
export default UrlSearchParams;
本文标签: javascriptURLSearchParams not defined error Inside A FunctionStack Overflow
版权声明:本文标题:javascript - URLSearchParams not defined error Inside A Function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744072702a2528781.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论