How to track event on onbeforeunload - javascript

How can i track events if it is close tab, refresh page or button click on onbeforeunload?
I am clicking a button which is from iframe and it is redirecting me to some other page. It is cross domain call so can't set
$window.top.onbeforeunload = null;
I don't want to call onbeforeunload on button click. Please provide me any solution?

Related

JavaScript: How do I fire an event when a user has accessed the page via back/forward buttons?

There is a number of threads here on StackOverflow concerning how to catch the back/forward button, but that's not what I want to do. I want to fire a method each time a page is loaded by pressing the back/forward button. So not on the page, where the user clicked back/forward, but on the page that the user got to after clicking back/forward.
I also tried looking at events using Chrome and I haven't found any event that is firing when coming back or going forward to a page.
You can use https://developer.mozilla.org/en-US/docs/Web/API/Window/popstate_event
The popstate event will be triggered by doing a browser action such as a click on the back or forward button (or calling history.back() or history.forward() in JavaScript).
First you can save the info that user has navigated to localStorage or cookie, and then you can check if that localStorage entry or cookie exists on the next page load event.
If it exists, user has navigated to that page using back/forward buttons, you can do whatever you need to do, and clear the localStorage entry/cookie.

button in iframe should trigger event

I have a button in an iframe. I want that button to trigger an event outside the iframe. How can I do that?
I have the following button
<button class="btn">Back</button>
inside an iframe
I want when people click on that button, that it triggers an event in the view outside of the iframe. How can I do that?
No need to worry about cross domain because the iframe url is on the same domain as the view.
The button only shows up when the user navigates to the second page of the iframe.
It can't, at least not directly. Events stop bubbling when they hit the top of the document.
You could do something like:
button.addEventListener("click", passEventToParent);
function passEventToParent(event) {
parent.querySelector("iframe").dispatchEvent(event);
}
See dispatchEvent on MDN for more information about it.

How to stop page propagation and show a custom pop up in page unload event

When I have unsaved changes in a page, and user tries to navigate away from the page I want to show a custom pop up to the user instead of IE's default confirmation pop up in "unload" method of the page. But I am not able to stop the propagation of the page in the unload event.I have tried preventdefault() and stoppropagation(), cancelbubble() also, but nothing helped me.
$(window).bind('unload',function () {
//how to stop the page from unloading and show the custom pop up
}
Please suggest.Thank You.

Javascript fake click which triggers affiliate pop-up is being blocked

My page has an affiliate pop-up whenever someone clicks anywhere on the page. This part works fine.
So then I created a script where if they haven't clicked on their own after a while it will simulate a click, thus triggering the pop-up. However, the browser can somehow tell that this is a fake click & auto-blocks the pop-up (but it works fine if you actually click on the page, the browser won't block the pop-up then).
I've tried various ways of simulating a real-world click in javascript but nothing works. Any ideas of how to stop it from blocking the pop-up?
You cannot a trigger false click event so that your popup is not blocked by the browser. This is a not possible.
Popups will only work if they originate from a trusted event that is an event that the user initiated. In your first case the user clicks on the page causing a trusted event which allows it to open. Your second case however the user has made no such action, so no trusted event and no popup.

Can submit button clicked twice by user and script?

I've a timer set which will click a submit button automatically.
User can also press it.
Can both be triggered at the same time?
I can disable the button after click but if user clicks it and I disable it, it seems calling $("#btn").click() will still work.
I've googled this but could not find question like this.
Context: To prevent session expire, I want to submit a form programmatically after 5 minutes of a booking site which is heavily loaded. When I do could the user too by chance press the submit button?
Code
setTimeout(function (){ $("#btn").click()}, 5*60000); //click after 5 minutes
//this will cause page to reload
Can both be triggered at the same time?
Yes, disabling and triggering are two different things.
When you disable a button, that will only prevent the user from triggering a click by pressing the button. However, programmatic click triggers will still execute all related events.
To prevent subsequent click handler executions, the easiest path would be to use one() like
$(document).one('click', '#btn', function(){...});
This will undbind the click event after it has been executed at most once.

Categories

Resources