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.
Related
I am trying to build a code that does a request when a cross-origin iframe is clicked and it directs the main window to a new page.
Since it's impossible to directly tap into Iframe click events, I thought of the following conditions as necessary:
page unload event occurs
the window is out of focus
Page unload (As far as I know) happens only when the current url is directed to some other url.
Now, this unload could happen by clicking any link. To restrict it to Iframes, I added the condition of window being out of focus.
addEventListener('beforeunload',(event) =>{
if(!(document.hasFocus())){
// Do Something
}
});
My question is, are their any limitations to this approach? If yes, then please suggest some more conditions to make it as close to reality.
For those of you, who are curious: I want to track clicks on Google AdSense Iframes on my website.
I am opening a popup after main window load.
Problem:
When user actually click on link the popup opens without complaining anything.
But when I am using Javascript call to click on href, I am getting popup blocker.
I am suspecting that, browser identifies that, popup is opening without any actual operation by user and that's why it is blocking popup.
In herf, I am calling a javascript method to open the popup.
I searched all the questions regarding opening popup and simulating the click like this, these works fine to simulate the click but still getting popup blocker.
Is there any workaround to fool browser?
You can't fool the browser per-se in this scenario. You could however, launch a div as an overlay on the main window if that's an option.
Trying to invalidate the session once click on the browser back button as well as browser close using jQuery.
How to capture the browser back button event and browser close event? Is there any advantage with respect to javascript?
Any help appreciated.
Hopefully this helps you:
http://api.jquery.com/unload/
The unload event is sent to the window element when the user navigates
away from the page. This could mean one of many things. The user could
have clicked on a link to leave the page, or typed in a new URL in the
address bar. The forward and back buttons will trigger the event.
Closing the browser window will cause the event to be triggered. Even
a page reload will first create an unload event.
I'm using a script to fade out the page content when user is actually leaving the page.
For that I tried using the beforeunload event.
It works fine when I navigate through my site, however I also found it triggering on application launch, such as skype button. Even though I never left the page yet...
What is the best way to validate onbeforeunload event against premature triggering?
I guess I should still use my:
$(window).one('beforeunload', function() {
// need to make some condition, if really leaving the page - execute
$('html.nojs').stop(true,false).css('overflow','hidden').animate({opacity:0},2000);
});
But I would need to use some condition... just cannot think of any...
EDIT:
Rmoved the link to the site
The animation does work, but if you go to any specific product and click skype button there you will see the it makes poo...
EDIT2:
The solution to this problem is to detect what triggered an before unload event. As I said, by writing some condition inside that call.
We must find a way to find out whether the event was triggered by external application call (such as skype button that tries to open application) or was it something else, like... for example:( link click, a button submit, script for location change, starting a search, hitting back/forward, or refreshing the page).
This the accepted answer for this question
Capture user response when using window.onbeforeunload
If you need to know what triggered the onbeforeunload event, you can have a global variable, then set it to true when you click on the Skype button. Then check it inside your onbeforeload event.
What I mean is - when user clicks on some browser page reload/refresh button we do not want to reload page - we want to capture that event and call some function (for simple example one with some alert). I need it to work in IE6 and up and Chrome and Firefox3+ of course. How to do such thing (not using jQuery and other libs)?
It's not possible.
You can detect when an unload occurs, but you can't detect what caused the unload and you can't cancel it without the user's consent. For instance, typing a new address in the address box, clicking a link, submitting a form, selecting a bookmark or refreshing the page will all fire an onbeforeunload event.
You cannot. You can't change the operating system functionality from within any browser except IE.