Skip to content

Instantly share code, notes, and snippets.

@EricYue2012
Last active August 6, 2021 01:49
Show Gist options
  • Save EricYue2012/6a4973936dc17554433b94ece9b1b5f2 to your computer and use it in GitHub Desktop.
Save EricYue2012/6a4973936dc17554433b94ece9b1b5f2 to your computer and use it in GitHub Desktop.
javascript
// 1. 按名字取得网址中的query string
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
// example
// current URL: http: //www.test.com/index.php?name=John
getParameterByName('name') retrun 'John'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment