Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does javascript get the request parameters

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to get request parameters from javascript". In daily operation, I believe many people have doubts about how to obtain request parameters from javascript. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to get request parameters from javascript". Next, please follow the editor to study!

The method for javascript to obtain request parameters: 1. Obtain the URL;2 of the browser through JavaScript, and obtain the value of a parameter in url through Javascript; 3, extend a method for jQuery to obtain url parameters through jquery.

This article operating environment: windows7 system, javascript1.8.5 version, Dell G3 computer.

How does javascript get the request parameters?

JavaScript acquires the request parameter value of get mode

1. The implementation of obtaining browser's URL through JavaScript

_ window.location.href

In fact, it only uses the basic window object of JavaScript.

2. Obtain the value of a parameter in url through Javascript

Function getUrlParam (name) {/ / construct a regular expression object var reg = new RegExp ("(^ | &)" + name + "= ([^ &] *) (& | $)"); / / match target parameter var r = _ window.location.search.substr (1) .match (reg); / / return parameter if (r! = null) {return decodeURI (r [2]);} return null;}

You can get the value of the parameter by passing the parameter name in url through this function, for example, url is

Http://127.0.0.1:8020/JavaScriptFunction/index.html?bb=1&aa=' Zhang San'

To get the values of bb and aa, you can write:

Var bb = getUrlParam ('bb'); var aa = getUrlParam (' aa')

3. We can use this method to extend a method for jQuery to obtain url parameters through jquery. The following code extends a getUrlParam () method for jquery

(function ($) {$.getUrlParam = function (name) {var reg = new RegExp ("(^ | &)" + name + "= ([^ &] *) (& | $)"); var r = _ window.location.search.substr (1) .match (reg); if (r! = null) return decodeURI (r [2]); return null;}}) (jQuery)

4. With regard to the obtained parameters, the problem of preventing garbled codes in Chinese

When passing parameters, it is found that encodeURI is used for Chinese character coding, so the above method uses decodeURI when parsing parameter coding to ensure that Javascript is consistent with the parameter coding and decoding methods, which can prevent Chinese garbled.

In JavaScript, there are two commonly used encoding and decoding functions

EncodeURI () decodeURI () encodeURIComponent () decodeURIComponent () at this point, the study on "how to get request parameters from javascript" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report