How to use JavaScript Location replace

In this article I have described about Dom Node Location replace() method in JavaScript
  • 2511

JavaScript Location replace() method

  • As we know that location object contains information about the current URL.
  • The location object has a method named replace() method that replace current document with another document.

Browser support

Following are the main browsers which support the replace() method of location object

  • Internet Explorer.
  • Firefox.
  • Opera.
  • Google Chrome.
  • Safari.

Syntax

The replace() method of location object has following syntax

location.replace(newurl)

Lets take an example

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript">

    function replaceDoc()

    {

        window.location.replace("https://dotnetheaven.com/category/javascript")

    }

</script>

</head>

<body style ="background-color:yellow">

<input type="button" value="Replace site" onclick="replaceDoc()" />

</body>

</html>

OUTPUT

replace1.jpg

When we click on button it repalce the current document with that document which we give in url as a parameter in method name.

Further Readings

You may also want to read these related articles :here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.