Two ways to get URL parameters for native javascript

Source: Internet
Author: User

Two native methods of obtaining parameters via URLs (typically provided using a framework):

Method One (recommended):

Urlinfo=window.location.href; Gets the URL of the current page

len=urlinfo.length;//gets the length of the URL

Offset=urlinfo.indexof ("?"); /Set the start position of the parameter string

NEWSIDINFO=URLINFO.SUBSTR (Offset,len)//Take out the parameter string and get a string like "id=1"

Newsids=newsidinfo.split ("=");//The obtained parameter string is split by "="

newsid=newsids[1];//Get parameter values

Method Two:

function Getquerystringargs () {

Get the query string and remove the question mark from the beginning

var qs = (location.search.length > 0? location.search.substring (1): ""),

args = (),

Items = qs.length? Qs.spilt ("&"): [],

Items = NULL,

name = NULL,

Value = NULL,

i = 0,

len = items.length;

For (I=0;i < Len; i++) {

Item = item[i].split ("=");

Name = decodeURIComponent (item[0]);

Value = decodeURIComponent (item[1]);

if (name.length) {

Args[name] = value;

}

}

return args;

}

Two ways to get URL parameters for native javascript

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.