One of the most common uses of JavaScript is to redirect the user to or automatically open up another web page location. For example, when the user clicks a button, the JavaScript will redirect the user to the location selected in the drop down select menu. It’s just like the user has typed the web page location URL.
There are 2 ways to use JavaScript to navigate to another web page. The first is to rely on the window object:
<script type="text/javascript">
window.location="http://www.bing.com"
</script>
The location method mimics the user behavior of typing a URL address in the address bar of the browser window. The browser then forwards to that web site, leaving the last web page in the previous entry of browsing history.
The second approach is to use the replace method of location object:
location.replace("http://www.microsoft.com");
This is trickier in that it’s not like what users would do. Instead of creating a new history entry, this method replaces the old history entry by the new one. Essentially, users cannot go back to the previous page with this approach, the previous web page is replaced by the new one: http://www.microsoft.com.
Related Posts
- How to redirect the visitor to another page or website?
- PHP, JavaScript: Stop and prevent others from framing your site or web page
- A live demo of JavaScript injection example on a famous Webmaster forum
- PHP: setcookie() with HttpOnly Option to Reduce XSS (Cross Site Scripting) Attacks by Preventing JavaScript from Reading Cookies
- Use Relative Protocol URL Address to Automatically Determine Web Address Protocol (HTTP or HTTPS)
