In interactive web applications, users can navigate from one page to another. Such navigation is 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 to required URL. For example,
By setting this windows to navigate property, we can perform page redirect. But, this property works only for Internet Explorer. And, an 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.half except for the pages that include frames. When we use this property within the 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='https://phppot.com'" />
When we open this file with the browser, the redirect button click event will navigate us to the URL, https://phppot.com. In another 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 https://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 the redirect, we can refer history by using back() function. For example,
<input type="button" name="back" value="Back" onclick="window.history.back();" />