-1

Today use this Code but this code some time not properly work

Problem : Page refresh then call unload() but some time user change URL directly at that time not call unload() because website change...

So, How to solve it in all Browser. Please Help Me

<html>
    <head>
        <script type="text/JavaScript">
            function unload(){
                alert("My Page unLoad - Destroy Variable");
            }

            function load() {
                alert("My Page Load");
            }
        </script>
    </head>

    <body onLoad="load()">
        <script type="text/javascript">
            window.onbeforeunload = unload();
        </script>
    </body>
</html>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

2

HTTP is stateless, so on every new page the variables will begin in their uninitialised state, and all code set to run on load (ie. not in an event handler) will run.

If you are using localstorage:

Check this question on how to detect changes in the url: How to detect URL changes with jQuery

Then use this to destroy it:

 window.localStorage.removeItem(key);
Community
  • 1
  • 1
Tristan
  • 2,000
  • 17
  • 32