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 www.learnwebdevelopment.info 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:// www.learnwebdevelopment.info’;
</script>

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

<html>
<head>
<script type=”text/javascript”>
function currLocation()
{
alert(window.location);
}
function newLocation()
{
window.location=”http://qualitypointtech.net”;
}
</script>
</head>
<body>
<input type=”button” onclick=”currLocation()” value=”Show current URL”>
<input type=”button” onclick=”newLocation()” value=”Change URL”>
</body>
</html>

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 www.qualitypointtech.net

Display:


Posted in Javascript Tagged with: