JS get URL to pass the value and resolve get URL in the Chinese parameter garbled

Source: Internet
Author: User

Hello everyone, I'm a little C,

We in the project development sometimes need to pass the value between the page and the page, then we may choose to pass the parameter in the Address bar, then the other page needs to get the parameters in the Address bar, today to share the address bar how to pass parameters and get Chinese parameters garbled solution. Please look at the code:

1 /**2 * Take the parameters of the address bar3  * 4 * @param key5 * Key is the parameter name passed for example Http://localhost/test/test.html?id=1&name= Zhang San, key is ID and name6  *7 * @returns8  */9 functionGeturlparam (key) {Ten     //gets the parameter, taking a string with parameters, such as:? id=1&name= Zhang San One     varURL =Window.location.search;  A     //Filter the Address bar with regular -     varReg =NewRegExp ("(^|&)" + key + "= ([^&]*) (&|$)"); -     //Match target Parameters the     varresult = URL.SUBSTR (1). Match (reg); -     //Return parameter Values -     returnResult? decodeURIComponent (Result[2]):NULL; -}

Know the principle of the above code, it is very simple to use, when we need to get the address bar parameters, you can directly call the method Geturlparam (key) on it, and a good solution to the Chinese parameter garbled problem. You can try the results:

Geturlparam ("id")   //  result is 1geturlparam ("name")  //  result is Zhang San  

There is a shortage of places, please advise.

JS get URL to pass the value and resolve get URL in the Chinese parameter garbled

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.