window.onbeforeunload doesn't triggered at once - javascript

I use a jQuery Dirty Forms plugin in my web-application like this:
$(document).ready(function(){
$('form.checkUnsavedData').dirtyForms();
});
$(window).on('beforeunload', function(){
if($.DirtyForms.isDirty()){
return($.DirtyForms.message);
}
});
But first click by any link doesn't trigger onbeforeunload event. Only the next clicks trigger it. This problem keeps in different browsers (google chrome, firefox, ie10). What's the matter?

I believe you will need to add the onBeforeUnload API As it says in the drupal Download & extend or else you should try not to bind the beforeunload through jQuery and do it with normal javascript like: window.beforeunload = function(){/*code*/};. Finally check what jQuery version are you using, what browsers and how they handle this event. Check out Catching the Javascript beforeunload event, the cross-browser way it's a little dated but it may help you.

What's the matter?
Dirty Forms automatically attaches to the beforeunload event. You are doing it again. This is likely causing your problematic behavior. You should change your code to just...
$(document).ready(function(){
$('form.checkUnsavedData').dirtyForms();
});

Related

how to detect browser print (ctrl+p) using jquery

I have a button on my page called "Print". clicking on which will trigger a jQuery function.
I need to call the same jQuery function when user hits Alt+f+p or ctrl+p
How do I do that?
I tried to do that with matchMedia but no luck
if (window.matchMedia('print').matches) {
alert("print using browser");
}
There is not a standard way to do that as far as I know.
IE and FF offer own function calls,
In IE there are the nonstandard window.onBeforePrint() and window.onAfterPrint() event listeners. There isn't a non-IE way to do it that I know of, however.
Firefox 6 now supports beforeprint and afterprint
https://developer.mozilla.org/en/Printing#Detecting_print_requests
A universally working solution might be to just listen for CMD+P.

Window binding not working in Firefox

my script reads something like this:
$(window).bind('popstate', function() {
//some code
//few more lines of code
});
This function works perfectly as intended in Chrome and Safari browsers. But Firefox for some reason ignores this function and does not work as intended.
Instead of using:
$(window).bind('popstate', function() {
//some code
//few more lines of code
});
You can use:
window.onpopstate = function() {
//some code
//few more lines of code
}
As firefox is using W3C defined rules for history API, so you have to use this for firefox and it works in chrome, safari and other browsers as well.
Note that just calling history.pushState() or history.replaceState()
won't trigger a popstate event. The popstate event is only triggered
by doing a browser action such as a click on the back button (or
calling history.back() in JavaScript).
Browsers tend to handle the popstate event differently on page load.
Chrome and Safari always emit a popstate event on page load, but
Firefox doesn't.
https://developer.mozilla.org/en-US/docs/Web/Reference/Events/popstate
Are you saying Chrome and Safari fire the event on page load or when the browser's back button is clicked? If the former, it's because Chrome/Safari are out of compliance with the HTML5 specs => the event should never be fired on page load. Up-vote https://code.google.com/p/chromium/issues/detail?id=63040 to get Google to fix this.
Please do Check that if you have coded window.load() more than once OR have called .onload() more than one time. This probably may work for IE but not for Chrome and fireFox.

Popup without user action?

How is this site triggering its popup while bypassing Chrome's pop-up blocker?
http://undertexter.se/
I thought pop-up blockers only allowed window.open if it was triggered by a user action but that's not the case here so how are they doing it?
OUTDATED
Origin resources and fiddles do not work anymore.
I figured out that there is an popup opening in chrome if you visit the Website first time (empty cache) and click somewhere on the document.
After a refresh and a click on it again nothing will happen if cache wasn't removed.
So lets start the game of reverse engineering...
Took the script from
http://undertexter.se/ and startet refuscation.
After a few steps I got the following code and I think this is not planned by browser manufactures to support something like this.
Now I wish you a lot of luck to use that for your own but I think it's evil.
Look on js fiddle for the result:
Its the reverse of:
http://www.wigetmedia.com/tags/undertexter.se.js
http://jsfiddle.net/W9BdS/
Tested on my own server, This opens up http://google.com in a new (standalone) window in Chromium 28 on Ubuntu 12.04. Adblock is active.
<script type="text/javascript">
document.addEventListener('click', function() {
window.open('http://google.com','...','width=1080 height=640')
})
</script>
try this using JQuery
$('#yourCotrolId').on('click', function() {
window.open('yourLink','...','width=1080 height=640')
});
window.open is blocked by modern browsers when called not in a click event handler.
I faced this myself while using Rails' format.js responses and wrote a small plugin that makes showing JS popups as simple as calling window.open():
Popup("<div>Hello world</div>").show('up')
You could use the .trigger method for simulating an user action:
Click
$('a').trigger('click');
.trigger() can not be used to mimc such native browser events, you can use .simulate() from simulate.js to mimic such events.
include simulate.js in your file and use,
$('a').simulate('click');

How to give focus to an iFrame in firefox just page is loaded?

How to give focus to an iFrame in firefox just page is loaded?
FYI: I tried focus() javascript & jQuery functions to do this, works properly for XP, but doesn't work for Windows 7?
Also, how does foucs() functionality differs from platform to another one?
try to trigger the focus or focusin event on page/dom ready.
$(document).ready(function(){
$('iframe').trigger('focusin');
});

ExtJS : handling browser exit event (click on cross-exit)

I would like to know if there is any way to handle the browser exit event.
For example, I would like to send a query when the user click on the cross-exit or simply close his browser.
I think I have found a solution. I haven't tested it on IE but it seems to be working on Firefox :
Ext.EventManager.on(window, 'beforeunload', function() {
alert('cross-exit tab click AND cross-exit browser click');
});
Ext.EventManager.on(window, 'unload', function() {
alert('cross-exit tab click');
});
PS : Thank you exhuma for having edited the question ;)
This thread might shed some insight:
http://forums.devarticles.com/javascript-development-22/how-to-stop-browser-from-closing-using-javascript-8458.html
I haven't given this solution a test-run yet. But it seems possible.
Here's a more in-depth discussion on the onbeforeunload event handler.
http://bytes.com/topic/javascript/insights/825556-using-onbeforeunload-javascript-event
And then there's always quirksmode. Unfortunately the load/unload events are not yet covered by quirksmode. As stated on the page:
I haven’t tested the load and unload events yet; they’ll be added to the table later on.

Categories