onbeforeunload window location change not working - javascript

I want to capture the page reload event using JavaScript and then redirect to a different URL (i.e. load the different page in the same browser tab) instead of the letting the existing page reload. My code is here -
<script type="text/javascript">
window.onbeforeunload = function(e) {
window.location = "https://www.google.com";
e.preventDefault();
};
</script>
The code gets executed when I right click in Chrome and then click reload but www.google.com does not load in the tab - instead the page reloads in the same tab.
What should be changed here?

You can only do certain, things inside this event handler, mostly ask for confirmation, redirection is too late.
However you can redirect after the user confirms. Use .returnValue
https://html.spec.whatwg.org/#the-beforeunloadevent-interface

Related

Javasccript or angularJS : is to possible to get Browser TAB click event

I am looking for a event like when when come back to a tab?
for example: I have opened four tab in the same browser with same url:
like: http://127.0.0.1:/blabla
http://127.0.0.1:/blabla http://127.0.0.1:/blabla http://127.0.0.1:/blabla
If i switched to a tab to another tab, i dont want that event. when i back to a tab, i want to get the event to refresh the page.
My objective is, when i back to the http://127.0.0.1:/blabla, the page should automatically reload, nothing else
You can capture onfocus event of window and try to reload the page.
window.onfocus and window.onblur
should work for you and then try to reload the page

Can I "catch" a redirect executed by a script, with javascript eventListeners?

I have an HTML page that includes only a script tag, I don't control the script and I can't change it (so I can't fire my custom event for example).
The script ends with a redirect (using window.location).
Is there a way to add a new script to the page that will listen to the page events and "catch" the redirect (actually it's better for me to catch the new loaded document)?
Something like:
window.addEventListener('redirected', function() {
// do staff
});
(I know there is no "redirected" event, it's just for the example).
It's very important to make it clear that the redirect isn't caused by an anchor click or back/forward button click, so I can't use events like click or popstate.
You might want to look at the onpagehide event or the onunload event, both of which occur when the user navigates away from the page.
However, if you wish to interfere or prevent the redirection itself, onbeforeunload is what you want.
Just take a look at :
unload function w3school or mozilla developper network
beacon function for sending a final XMLHttpRequest

JQuery URL change listener

How can I catch when the browser URL was changed?
I have the following scenario:
In my page I have an iframe when the user clicks inside the iframe the iframe content changes and the URL in the browser is changed via js.
Nothing else will change outside the iframe (not even the iframe src).
There is any possibility to check if the URL was changed?
This is not working:
jQuery(window).on("hashchange", () => {
}
EDIT: I fixed this problem using our internal event bus after the URL was changed, but I still think that there are better solutions for this using already built in events...
I am not sure about because the iframe is changing the url but you can try using the onbeforeunload event: it should be fire when a window is about to unload resources.
window.onbeforeunload = function(e) {
// ...
};
Keep in mind that the hashchange should only work when hash changes (not complete URL).
Set session variable when the visitor clicks certain button.

Check if the link results in reloading the page

I develope a chrome extension; with a content script I inject some code in the page. What I want is an event that triggers if the current tab is going to update.
I tried to catch the click event of an a-element and it does work in somehow 80% of all cases. I already check if it's an anchor, but there are still many links which don't reload the page or forward to another.
$('a', document).click(function (e) {
// ...
});
So, what I want is described in the following three steps:
Event: the page is going to reload
prevent it from reloading, execute some code
trigger reloading the page afterwards
Depending on your requirements, have you considered listening to beforeunload event? It's fired when the page is about to unload (also including refresh the current page).

How to capture browser window clsoing event?

Hi I want to capture event of windows closing and want to remove session values . After several search I found onBeforeUnload event which is triggered each time when you navigate to different page or unloads the page.even on reload this event will be triggered which is not my requirement. I want specific only windows closing event in all browsers to invalidate the session values.
Run JavaScript code on window close or page refresh?
/* The most acceptable answer seemed to be as follows in vanilla JS
and was addressed at the linked address already.*/
window.onbeforeunload = closingCode;
function closingCode(){
//I would capture the URL prior to unload here and if it does NOT
//exist or is not your url domain then you can unload variables here
return false;
}

Categories