
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
HTML DOM Location replace() method
The HTML DOM Location replace() method is used for rendering a new document replacing the current document. It also removes the current document URL from document history disabling navigation to old document using ‘back’ button.
Syntax
Following is the syntax −
location.replace(URLString)
Example
Let us see an example for Location replace() property −
<!DOCTYPE html> <html> <head> <title>Location replace()</title> <style> form { width:70%; margin: 0 auto; text-align: center; } * { padding: 2px; margin:5px; } input[type="button"] { border-radius: 10px; } </style> </head> <body> <form> <fieldset> <legend>Location-replace( )</legend> <label for="urlSelect">Current URL: </label> <input type="url" id="urlSelect" value="https://www.example.com"><br> <label for="newUrlSelect">New URL: </label> <input type="url" id="newUrlSelect" placeholder="Give new url..."><br> <input type="button" onclick="doReplace()" value="Go to new URL"> <div id="divDisplay"></div> </fieldset> </form> <script> var divDisplay = document.getElementById("divDisplay"); var urlSelect = document.getElementById("urlSelect"); var newUrlSelect = document.getElementById("newUrlSelect"); function doReplace(){ if(newUrlSelect.value === '') divDisplay.textContent = 'Provide new URL'; else{ location.replace(newUrlSelect.value); divDisplay.textContent = 'Redirecting to new URL'; } } </script> </body> </html>
Output
This will produce the following output −
After clicking ‘Go to new URL’ button with no input −
Clicking ‘Go to new URL’ button after fillings details −
- Related Articles
- HTML DOM Location reload() method
- HTML DOM KeyboardEvent location Property
- HTML DOM Location hash Property
- HTML DOM Location host Property
- HTML DOM Location hostname Property
- HTML DOM Location href Property
- HTML DOM Location origin Property
- HTML DOM Location pathname Property
- HTML DOM Location port Property
- HTML DOM Location protocol Property
- HTML DOM Location search Property
- HTML Location assign( ) Method
- HTML DOM addEventListener() Method
- HTML DOM normalize( ) Method
- HTML DOM write() Method

Advertisements