Methods of the DOM Location Object - Replace()
(Page 4 of 4 )
The replace() method is used to replace the current document or page with a new one. I was hoping I could get this code to loop through fifty web pages (without using a loop) but it did not work in that manner. If you use document.write and use replace, it automatically loads a new page; if you put say…ten of those pointing to different URLs, it doesn't display one after another (or if it does, you don’t see that). Instead, it takes a really long time to load the last page.
Here is how you use it in a document.write, instantly loading the page without giving the user a choice (which, by the way, I don’t really recommend):
<html>
<head>
<script type="text/javascript">
document.write(window.location.replace("http://www.devhardware.com"));
</script>
</head>
</html>
Now let’s give the user a choice, shall we?
<html>
<head>
<script type="text/javascript">
function replacer()
{
window.location.replace("http://www.devhardware.com");
}
</script>
</head>
<body>
<input type="button" value="Use the replace() Method" onclick="replacer()" />
</body>
</html>
Again, when the user clicks the button, it triggers the replacer() function, which initiates the replace() method, loading the new page. Here is the result:

Conclusion
As you can see, we’ve run out of time, so while we have finished our conversation regarding the Dom Location Objects and Methods, we will have to save the History Objects for a later time. Before we go, however, I would like to leave you with one last review of the tables summarizing the purposes of the Object method and properties we have covered in these two articles. Feel free to print it out and use it as a reference:
Name | What It Does | Type |
hash | Used to Set or retrieve the URL starting from the hash (#) symbol | Property |
host | Used when you want to set or retrieve the port number and hostname of the URL | Property |
hostname | Used to set or retrieve the hostname of the present URL | Property |
href | Used to retrieve or set the entire URL | Property |
pathname | Used to retrieve or set the pathname of the present URL | Property |
port | Used to retrieve or set the port number of the present URL | Property |
protocol | Used to retrieve or set the protocol of the present URL | Property |
search | Used to retrieve or set the URL from the question (?) mark | Property |
assign() | Used when you wish to load a new document/page | Method |
reload() | Used when you want to reload the current page/document | Method |
replace() | Used when you wish to replace the current page/document with a new one | Method |
Till then…
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |