Two methods to redirect URL parameters in JavaScript

Source: Internet
Author: User
Summary of Two Methods for redirecting URL parameters in JavaScript this article mainly introduces two methods for redirecting URL parameters in JavaScript. Let's not talk about them here. Let's look at the sample code directly.

I. Character concatenation

Function setUri (para, val) {var strNewUrl = new String (); var strUrl = new String (); var url = window. location. href; strUrl = window. location. href; if (strUrl. indexOf ("? ")! =-1) {strUrl = strUrl. substr (strUrl. indexOf ("? ") + 1); // obtain the parameter if (strUrl. toLowerCase (). indexOf (para. toLowerCase () =-1) {// if no parameter is found, assign strNewUrl = url + "&" + para + "=" + val; window. location. href = strNewUrl;} else {var aParam = strUrl. split ("&"); for (var I = 0; I <aParam. length; I ++) {if (aParam [I]. substr (0, aParam [I]. indexOf ("= ")). toLowerCase () = para. toLowerCase () {aParam [I] = aParam [I]. substr (0, aParam [I]. indexOf ("=") + "= "+ Val ;}} strNewUrl = url. substr (0, url. indexOf ("? ") + 1) + aParam. join (" & "); window. location. href = strNewUrl;} else {strUrl + = "? "+ Para +" = "+ val; window. location. href = strUrl ;}}

Ii. Use Regular Expressions

// Use the regular function setPara (para, val) {var newpar = ""; var url = window. location. href; var pars = location. search. substring (1); var reg = new RegExp ("(^ |)" + para + "= ([^ &] *) (| $)"); if (reg. test (pars) {// The required parameter para var p1 = pars. split (para) [0]; // productID = 100857 & count = 1 & var p2 = pars. split (para) [1]; // = 75825 & coupval = 1.5 & addressID = 358 & invoiceID = 1245 & invoiceName = jesse if (p2.indexOf ("&")>-1) {v Ar p3 = p2.split ("&") [0]; if (p3 = "=" + val + "") {return false ;} newpar = p1 + para + '=' + val + '&' + (p2.split (p3) [1];} else {if (p1) {newpar = p1 + para + '=' + val;} else {newpar = para + '=' + val ;}} else {if (url. indexOf ("? ") =-1) {newpar = pars +" & "+ para +" = "+ val ;} else {newpar = pars + "&" + para + "=" + val ;}} window. location. href = location. href. split ('? ') [0] + "? "+ Newpar ;}

Call:

Script window. onload = function () {var btn = document. getElementById ("btnClick"); btn. onclick = function () {// url: http: // localhost: 54714/testuri. aspx? ProductID = 100857 & count = 1 & coupresId = 12785 & coupval = 1.5 & openId = 12456 & addressID = 358 & invoiceID = & invoiceName = // setUri ("coupresId", 0 ); setPara ("coupresId", 0) ;}} script

Related Articles:

Three methods for php page redirection

PHP redirection and pseudo-static

Redirect to external links

Related Article

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.