JS gets the URL to the number of two ways to explain the specific

Source: Internet
Author: User

There is a URL such as the following:

Http://passport.csdn.net/account/login?

From=http%3a%2f%2fwrite.blog.csdn.net%2fpostedit

How do we get the value from this parameter? Search the online method is very easy, such as the following, the first is through the regular. Another way to put the array into a string by slicing it:

Method One:

function getquerystring (name) {var reg = new RegExp ("(^|&)" + name + "= ([^&]*) (&|$)", "I"); var r = Window.loca TION.SEARCH.SUBSTR (1). Match (reg); if (r = null) return unescape (r[2]); return null;} var from = getquerystring (' from '); alert (from);


Method Two:

function Getrequest () {var url = location.search;//Get URL in "?" Character string var therequest = new Object (); if (Url.indexof ("?

")! =-1) {var str = URL.SUBSTR (1); STRs = Str.split ("&"); for (var i = 0; i < strs.length; i + +) {therequest[strs[i].split ("=") [0]]=unescape (Strs[i].split ("=") [1]);}} return therequest; } var req = Getrequest (); var from = req["from"];alert (from);









JS gets the URL to the number of two ways to explain the specific

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.