Change url / state to beforeunload with history.replaceState

I was looking for answers to this question, I could not find it, I wonder if this is an error, it seems that browsers do not support this HTML5 function well.

I only have chrome and firefox for testing. (latest stable versions).

$(function(){ if ( window.history.state != null ) $( "#content" ).html( window.history.state.value ); $( window ).bind( "beforeunload", function() { window.history.replaceState( { value:"remember me"}, "", document.location.href + "?param=rememberme" ); }); }); 

Just give it a try. I tried to change the current url to beforeunload, so I can store the data in the url so that when the user returns to the page, he will reload the page with the changed url in which there is information.

But this behaves strangely when reloading the page. At first, chrome window.history.state does not exist at all (not yet supported?), So the state cannot be loaded on reboot. And on page reload, the URL change is not repeated. But if you go to another site and come back, changing the URL will be remembered.

in firefox, window.history.state exists, and when you window.history.state the page or story forward / back, the state is executed correctly. But changing the URL does not work when the page reloads, the URL changes correctly every 2 reloads. If you go forward / backward, the URL change will be processed correctly.

Does anyone know how to properly configure this code to reload pages (F5), and if chrome really doesn't support history.state ? Is the behavior on page reloading correct, or do browsers cope poorly with this?

If anyone can test it on Safari, I will be interested to know how it works there.

Just check the code there: http://www.braikar.com/stuff/test.html

+4
source share
1 answer

Have you tried using cookies to store your variable? You can install it in beforeunload and read it at boot.

0
source

Source: https://habr.com/ru/post/1391041/


All Articles