// similar behavior as an HTTP redirect window.location.replace("http://www.example.com/new-home"); // similar behavior as clicking on a link window.location.href = "http://www.example.com/new-home";
In the first case, the redirect will not create an entry in browser’s history, while the second case will create one, since it is like clicking on another URL from your page.
- You can use full URL (e.g. http://www.example.com/new-home) or relative paths (e.g. /new-home). If you use relative path for redirection, the browser will redirect to the page, under same HTTP/HTTPS protocol without changing it. If you want to redirect from HTTP to HTTPS page, you need to specify the full URL
- Some web frameworks like Django require you to specify a trailing slash after URLs. If that is the case with your web framework, you need to specify the trailing backslash in the above statements. Otherwise, your framework will further redirect it to URL with trailing slash appended to it.
- If you add the redirection in the function handler for submit button, it may not work, since the browser will simply submit the form instead of redirecting the page. In such cases, you may submit form using AJAX and redirect based on button submission’s response.
Here are several other ways to redirect pages on browser. They produce the same result as the ones mentioned above.
// window.location window.location.replace('http://www.example.com/new-home') window.location.assign('http://www.example.com/new-home') window.location.href = 'http://www.example.com/new-home' document.location.href = '/new-home'
If you just want to go back to your previous page in your browser history, you can also use window.history to do this.
// window.history window.history.back() window.history.go(-1)
How to Show All Users in MySQL
How to Upgrade All Python Packages using pip
How to Create Python Function with Optional Arguments
How to Import Python Module Given Full Path
How to Automate Backup in Python