JavaScript imitates php $_get get URL pass value 2 ways

Source: Internet
Author: User

Example

The code is as follows Copy Code
<script>
var $_get = new Object ();
var _rep = document.location.search.replace (/(\?| &) ([\w\d_]+) = ([^&]+)/g, "_get[' $ ']= ' $");
Eval (' $ ' +unescape (_REP.SUBSTR (0,_rep.length-1)));
For (i in $_get) document.write (i+ ' => ' +$_get[i]+ ' <br> ');
</script>

Add an IE Firefox compatible

Character Read

The code is as follows Copy Code

<script language= "JavaScript" >
<!--
function Request (strName)
{
var strhref = Window.document.location.href;
var intpos = Strhref.indexof ("?");
var strright = strhref.substr (intpos + 1);

var arrtmp = Strright.split ("&");
for (var i = 0; i < arrtmp.length; i++)
{
var arrtemp = arrtmp[i].split ("=");

if (arrtemp[0].touppercase () = = Strname.touppercase ()) return arrtemp[1];
}
Return "";
}
Alert (Request ("id"))
-->
</SCRIPT>

Example Two

The code is as follows Copy Code

* * $_get[the Address bar get parameter like PHP's ' arg ']
function Getargs () {
var args = {};
var query = location.search.substring (1); Get query string
var pairs = Query.split ("&");
Break at Ampersand
for (var i = 0; i < pairs.length; i++) {
var pos = pairs[i].indexof (' = ');
Look for "Name=value"
if (pos = = 1) continue; If not found, skip
var argname = pairs[i].substring (0,pos); Extract the name
var value = pairs[i].substring (pos+1); Extract the value
Value = decodeuricomponent (value); Decode it, if needed
Args[argname] = value; Store as a property
}
return args; Return the object
}

/* Use method * *
* * Url:http://www.111cn.net user=funsion&age=26 * *
Alert (Getargs () [' User ']); Output funsion
Alert (Getargs () [' Age ']); Output 26

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.