How to get query parameters by es6
This article will explain in detail how to obtain query parameters for es6. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Get query parameters
For years, we wrote rough regular expressions to get query string values, but those days are gone; now we can get query parameters through URLSearchParams API
We obtain the query parameters in a regular way without using URLSearchParams, as follows:
Function getQueryString (name) {var reg = new RegExp ("(^ | &)" + name + "= ([^ &] *) (& | $)"); var r = _ window.location.search.substr (1) .match (reg); return r? R [2]: null;}
After using URLSearchParams:
/ / suppose the query parameters in the address bar are as follows: "? post=1234&action=edit" var urlParams = new URLSearchParams_ (window.location.search); console.log (urlParams.has ('post')); / / trueconsole.log (urlParams.get (' action')); / / "edit" console.log (urlParams.getAll ('action')); / / ["edit"] console.log (urlParams.toString ()) / / "? post=1234&action=edit" console.log (urlParams.append ('active',' 1')); / / "? post=1234&action=edit&active=1" this article on "how to get query parameters for es6" ends here. I hope the above content can be helpful to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.