JavaScript Window.Location

The window.location() is used to know the current page name (current url) and also used to redirect to new url. If you want to redirect all your visitors to site when they arrived at your site, you would just need the script below:

<script type=”text/javascript”>
alert(‘Current Location/url = ‘+window.location)
window.location = ‘http://’;

In the below example, the event execute/fires when user clicks button – the button event is onClick

<script type=”text/javascript”>
function currLocation()
function newLocation()
<input type=”button” onclick=”currLocation()” value=”Show current URL”>
<input type=”button” onclick=”newLocation()” value=”Change URL”>

When you click the first button ‘Show current URL’ it just shows current page name/url with alert box. And clicking second button will redirect you to this url


Posted in Javascript Tagged with: