VDone Demo VDone Demo
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)

Nikolay Tuzov

Backend Developer
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)
  • JavaScript文章

    • 33 Extremely Useful JavaScript One-Liners
    • How the new Operator Works
    • ES5 Object-Oriented Programming
    • ES6 Object-Oriented Programming
    • Performance Comparison of Multiple Array Deduplication Methods
    • JS Randomly Shuffle an Array
    • Detect Whether the Browser Is on a Mobile Device
    • Convert a 1D Array into a 2D Array by Specified Length
    • Debounce and Throttle Functions
    • JS Get and Modify URL Parameters
      • Get URL Parameters
      • Modify URL Parameters
      • Modify URL Parameters Without Refreshing the Page
      • URL Encoding and Decoding
    • More Accurate Type Checking Than the typeof Operator
    • 三级目录

  • 学习笔记

  • 前端
  • JavaScript文章
xugaoyi
2020-03-05
Contents

JS Get and Modify URL Parameters

# JS Get and Modify URL Parameters

# Get URL Parameters

/**
 * Get a parameter from the URL
 * @param arg Parameter name
 * @returns
 */
function getURLString(arg) {
    var reg = new RegExp("(^|&)" + arg + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}
1
2
3
4
5
6
7
8
9
10
11
12

# Modify URL Parameters

/**
 * Modify a URL parameter
 * @param url The URL to modify
 * @param arg The parameter name to modify
 * @param arg_val The new value
 * @returns {String}
 */
function changeURLArg(url, arg, arg_val) {
    var pattern = arg + '=([^&]*)';
    var replaceText = arg + '=' + arg_val;
    if (url.match(pattern)) {
        var tmp = '/(' + arg + '=)([^&]*)/gi';
        tmp = url.replace(eval(tmp), replaceText);
        return tmp;
    } else {
        if (url.match('[\?]')) {
            return url + '&' + replaceText;
        } else {
            return url + '?' + replaceText;
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# Modify URL Parameters Without Refreshing the Page

https://www.cnblogs.com/wuting/p/8946927.html (opens new window)

# URL Encoding and Decoding

For example, on a system using UTF-8, in the URL http://www.example.com/q=hello, non-ASCII characters are not valid URL characters, so the browser automatically encodes them. For instance, the Chinese word for "Spring Festival" would be converted to http://www.example.com/q=%E6%98%A5%E8%8A%82. The first character becomes %E6%98%A5, and the second character becomes %E8%8A%82. This is because their UTF-8 encodings are E6 98 A5 and E8 8A 82 respectively, and each byte is prefixed with a percent sign to form the URL encoding.

JavaScript provides four methods for URL encoding/decoding:

  • encodeURI()
  • encodeURIComponent()
  • decodeURI()
  • decodeURIComponent()

View documentation (opens new window)

Edit (opens new window)
Last Updated: 2026/03/21, 12:14:36
Debounce and Throttle Functions
More Accurate Type Checking Than the typeof Operator

← Debounce and Throttle Functions More Accurate Type Checking Than the typeof Operator→

Recent Updates
01
How I Discovered Disposable Email — A True Story
06-12
02
Animations in Grid Layout
09-15
03
Renaming a Git Branch
08-11
More Articles >
Theme by VDone | Copyright © 2026-2026 Nikolay Tuzov | MIT License | Telegram
  • Auto
  • Light Mode
  • Dark Mode
  • Reading Mode