Home  >  Article  >  Web Front-end  >  javascript jump url parameters

javascript jump url parameters

王林
王林Original
2023-05-06 10:06:071151browse

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?

  1. Get the current URL

In JavaScript, you can get the URL of the current page through window.location.href. For example:

var currentUrl = window.location.href;
  1. Splicing URL parameters

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;
    Redirect page
Next, you can use

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('张三');
Summary

Through 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:birt javascript methodNext article:birt javascript method