Js modifies the value of a specified parameter in the url

Source: Internet
Author: User

When there are many search conditions on a page and joint get query is required, it is no problem to organize them into a correct url according to certain rules, but how do you organize your url when this joint query can proceed without limit? Is your query parameters appended to the current url unrestricted? Obviously, this is unscientific. The following describes how to directly modify the value of a parameter in the current url without causing multiple values in the same parameter in the url. To some extent, the url length is minimized. It is user-friendly and conducive to the optimization of search engines. The following describes how to use javascript to modify the value of a parameter in a url:

/** Url destination url (http://www.phpernote.com/) * parameter name to be replaced by arg * arg_val value of the parameter * return url parameter replaced by url */function changeURLArg (url, arg, arg_val) {var pattern = arg + '= ([^ &] *)'; var replaceText = arg + '=' + arg_val; if (url. match (pattern) {var tmp = '/(' + arg + '=) ([^ &] *)/G'; tmp = url. replace (eval (tmp), replaceText); return tmp;} else {if (url. match ('[\?] ') {Return url +' & '+ replaceText;} else {return url + '? '+ ReplaceText;} return url +' \ n' + arg + '\ n' + arg_val ;}

The usage is as follows:

changeURLArg('http://www.phpernote.com/article.php?class_id=3&id=2','class_id',4);

The result is: http://www.phpernote.com/article.php? Class_id = 4 & id = 2

Articles you may be interested in
  • What is the default parameter value for function settings in Javascript?
  • How to obtain the complete url address and parameters in javascript
  • Linux chmod (file or folder permission setting) command parameters and Usage Details
  • Js to determine whether an array contains repeated values
  • Install and use Sphinx in windows [Chinese full-text search is supported]
  • Using JavaScript to prohibit browsers from directly copying article content
  • Summary of the string truncation function implemented by javascript (including introduction to the use of Js to intercept Chinese characters)
  • When CuteFTP is connected to the ftp server, the "invalid Parameters" error is displayed.

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.