Home >Web Front-end >Front-end Q&A >javascript jump url parameters
In web development, it is often necessary to jump to a page and pass parameters. The commonly used method is to add parameters after the URL and redirect. How to implement this process in JavaScript?
In JavaScript, you can get the URL of the current page through window.location.href
. For example:
var currentUrl = window.location.href;
Next, you need to splice the parameters to the end of the URL. A common method is to use question marks (?
) to separate URLs and parameters, use equal signs (=
) to separate parameter names and parameter values, and use between multiple parameters. ##connect. For example:
var url = 'http://example.com/page1.html?id=123&name=张三';When the processing parameter value is Chinese, url encoding needs to be performed, and the
encodeURIComponent() function is used for encoding. For example:
var name = '张三'; var encodedName = encodeURIComponent(name); var url = 'http://example.com/page1.html?name=' + encodedName;
window.location.href to redirect the page to the specified URL . For example:
window.location.href = url;The complete code is as follows:
var name = '张三'; var encodedName = encodeURIComponent(name); var url = 'http://example.com/page1.html?name=' + encodedName; window.location.href = url;Through the above method, we can realize the function of page jump and passing parameters in JavaScript. In the actual development process, we can encapsulate parameters into functions for reuse. For example:
function redirectPage(name) { var encodedName = encodeURIComponent(name); var url = 'http://example.com/page1.html?name=' + encodedName; window.location.href = url; } redirectPage('张三');SummaryThrough this article, we have learned how to implement page jumps and pass parameters in JavaScript. When we need to pass parameters, we can splice the parameters behind the URL and use
window.location.href for redirection. In actual development, we can encapsulate this method into a function for reuse.
The above is the detailed content of javascript jump url parameters. For more information, please follow other related articles on the PHP Chinese website!