Get the App
SLTechnology News&Howtos  ›  Development  › 

How to get query parameters by es6

Shulou Source: shulou.com Published: 2022-06-01 04:10:07 10月01日 Update

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.

Tags: Queries parameters articles more regular good practical rough gone forever content can be passed address years years characters strings articles ways days knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Huawei macOS Linux Microsoft