In interactive web applications, users can navigate from one page to another. Such navigation are possible by the use of hyperlinks, functions of client and server side scripts. For example, in PHP, the header() function is used to redirect by specifying location URL.
This property of window object indicates current page URL. And we can switch over to another page by setting this property with required URL. For example,
By setting this windows navigate property, we can perform page redirect. But, this property works only for Internet Explorer. And, a URL can be assigned to this property, as like as we have seen to set it with window’s location property above.
For example, we can replace the following line,
This property works as same as windows.location.href except for the pages that include frames. When we use this property within frame to set location, then the content of the frame will be changed with the specified location’s content. For example, save the following html content as redirect.html.
<input type="button" name="redirect" value="Redirect Now" onclick="self.location='http://phppot.com'" />
When we open this file with the browser, the redirect button click event will navigate us to the URL, http://phppot.com. In other case, if we use this file as a source of a frame like,
<frameset rows="300,*"> <frame src="redirect.html" name="redirect"> <frame src="footer.html" name="footer"> </frameset>
then, then the button click event will replace the frame content with the content of the page http://phppot.com.
Like the location properties we have seen above, we can use top.location.href, parent.location.href based on the place from where the redirection is going to take place. When we use simply location.href, then, by default this will be set to the window object.
If we want to go back to the previous page after redirect, we can refer history by using back() function. For example,
<input type="button" name="back" value="Back" onclick="window.history.back();" />