How to fix: "Lost reference to element" in JavaScript

In HTML, when you use an element, you must include the document's  element in the opening tag. If you don't, you get an error message such as "Lost reference to element".

One way to fix this error is to use the history.pushState() method. This method lets you add elements to the history without including the  element in the opening tag.

The history.pushState() method takes three parameters:

  • The first parameter is the name of the state.
  • The second parameter is the data to be stored in the state.
  • The third parameter is the function to be called when the state is changed.

The following code snippet shows how to use the history.pushState() method:

<html>
    <head>
        <title>History.pushState()</title>
    </head>
    <body>
        <div id="main">
        Hello, world!
        </div>
        <script>
            function changeWindow() {
                history.pushState("Hello worldd!",null,changeWindow);
            }
        </script>
    </body>
</html>

In the code snippet above, the state is "Hello world!" and the function to be called when the state is changed is changeWindow() .

When you run the code snippet, the following happens:

  • The page loads and you see the text "Hello, world!"
  • You click on the link that says "Change to another page."
  • The page changes but the text "Hello, world!" remains the same.
  • You click on the link that says "Hello world!"
  • The page changes and the text "Hello world!" is replaced with the text "Hello worldd!"

The following code snippet shows how to use the history.pushState() method to add an element to the history:

<html>
    <head>
   	 <title>History.pushState()</title>
    </head>
    <body>
        <div id="main">
        Hello, world!
        </div>
        <script>
        function changeWindow() {
            var newdiv = document.createElement("div");
            newdiv.innerHTML = "Hello world!";
            document.body.appendChild(newdiv);
            history.pushState("Hello worldd!",null,changeWindow);
        }
        </script>
    </body>
</html>

In the code snippet above, the state is "Hello world!" and the function to be called when the state is changed is changeWindow() .

When you run the code snippet, the following happens:

  • The page loads and you see the text "Hello, world!"
  • You click on the link that says "Change to another page."
  • The page changes but the text "Hello, world!" remains the same.
  • You click on the link that says "Hello world!"
  • The page changes and the text "Hello world!" is replaced with the text "Hello worldd!"

The following code snippet shows how to use the history.pushState() method to add an element to the history and change the URL:

<html>
    <head>
    <title>History.pushState()</title>
    </head>
    <body>
        <div id="main">
        Hello, world!
        </div>
        <script>
            function changeWindow() {
                var newdiv = document.createElement("div");
                newdiv.innerHTML = "Hello world!";
                document.body.appendChild(newdiv);
                history.pushState("Hello world!",null,changeWindow);
                window.location.href = "https://www.google.com";
            }
        </script>
    </body>
</html>

In the code snippet above, the state is "Hello world!" and the function to be called when the state is changed is changeWindow() .

When you run the code snippet, the following happens:

  • The page loads and you see the text "Hello, world!"
  • You click on the link that says "Change to another page."
  • The page changes but the text "Hello, world!" remains the same.
  • You click on the link that says "Hello world!"
  • The page changes and the text "Hello world!" is replaced with the text "Hello world!"
  • The page changes to https://www.google.com .
💡
Need a writer? Want to work with me? I have a subscription copywriting service available at Zipply.io. Subscribe to a set number of words and split it into as many copy assets as you need. Scale your startup, small business, or MVP. Upgrade, downgrade and pause at any time. Start your subscription here.