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=张三';
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;
var name = '张三'; var encodedName = encodeURIComponent(name); var url = 'http://example.com/page1.html?name=' + encodedName; window.location.href = url;
function redirectPage(name) { var encodedName = encodeURIComponent(name); var url = 'http://example.com/page1.html?name=' + encodedName; window.location.href = url; } redirectPage('张三');
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!