Consider this simple link:
Do Nothing
When this is in the main document in a window, clicking it does nothing, because the return false cancels the click.
But when this is in an iframe document (in Firefox and Chrome at least), it opens a new tab window.
This is very annoying because I want to be able to hook a click event (in actuality using jQuery's $(element).click(handler))), do some logic, cancel the navigation, and NOT have that extra tab.
What's going on here and how can I stop this behavior?
It turned out that I had an unrelated jQuery click event that was causing this behavior, so this was my user error. Dumb.
Related
I use the following to open a new window:
var win = window.open('URL HERE' , type ,'width=1100,height=500,left=200,top=200');
The reason I assign a variable to it is because it allows me to do other things with the variable such as actions on close (which I am not using in this case).
The window opens just fine. I can do everything I need to do inside the window. But the problem occurs when I close the window: The window closes but reopens automatically when i click anywhere on the parent page.
This happens if I click the browsers "x" on the new window, or even if I click a link that that includes onclick="window.close()", and it happens every single time I try this. It's not sporadic at all.
What am I missing?
Found it!
Avoid using onchange and onblur together - even though some people post that this fixes some onchange problems that occur on ios devices.
At some point in the past I was dealing with an issue with iphone and ipad not opening a window when running an onchange event. Everyone kept posting about using onblur events on those devices. That didn't fix the problem but I had left an onchange and an onblur event inside the select dropdown by accident not realizing that was what it was doing.
What was happening was that I was running the window.open when the selection changed, and then it was rerunning window.open when the window lost focus which resulted in another window opening when refocusing parent page.
I want to fire the onbeforeunload event only when the tab or browser is closed and not upon refresh, link click and so on. I found a site where that works: https://checkout.deindeal.ch/
Steps to fire the event in Chrome:
Put something into the cart
Click on "Zur Kasse"
Close tab
onbeforeunload Message is shown and a popup opens. onbeforeunload events like refresh will not fire. I debugged their code, but could not find the place where they exclude those events or whatsoever.
Anyone out there who can find the correct place in the code?
Thanks!
For readers that want to use onbeforeunload,
The event onbeforeunload should be used for preventing user's data loss, like filled in forms and other data that are not saved and will be lost upon page refresh.
Using onbeforeunload to prevent a user from leaving your page is annoying and will not stop the user from leaving.
You can not detect a browser tab refresh button press, unless the user use a shortcut that you can detect with key press (like Ctrl+F5).
More info here :
javascript beforeunload detect refresh versus close
Old answer was removed in order not to mislead anyone (it was not working on nowadays browsers).
More stack overflow links related on onbeforeunload use:
How can i get the destination url in javascript onbeforeunload event?
How to prevent calling onbeforeunload when page refresh
Fire onbeforeunload confirm alert for external sites only
My browser (firefox) prevents any popup from loading, and loads links that open new windows in the current tab, unless I explicitly say I want the link to load on a new tab or window, with the appropriate shortcuts (for example, middle click on the link, or left click with ctrl pressed causes the link to open on a new tab, and shift + left click on a new window).
I would like to create a javascript function f() that runs some code (meant to create the link address) when the link is pressed, and then loads the link that has been created, without removing the user experience described above.
Right now what I have is something like <a href="javascript:void(0)" onclick="f()"/>, but middle click doesn't work (it instead loads the url javascript:void(0)) and neither do the other features described above.
Do you have any idea as how to solve my problem ?
Thanks.
have you tried window.open('url')?
see: http://www.javascript-coder.com/window-popup/javascript-window-open.phtml
Also, as far as I know, you can't control whether or not the browser opens in a new tab or new window. That is a browser setting that is different for every user.
You might also try removing the onclick, and using
EDIT
There seems to be issues with using middle click with opening new tabs instead of executing the javascript: middle click (new tabs) and javascript links
As that site says, you can instead create an id for the element and bind it through javascript.
**Taken from that link:
...
And then in your JS, hook the link via it's ID to do the AJAX call.
Remember that you need to stop the click event from bubbling up. Most
frameworks have an event killer built in that you can call (just look
at its Event class).
Here's the event handling and event-killer in jquery:
$("#thisLink").click(function(ev, ob) {
alert("thisLink was clicked");
ev.stopPropagation();
});
Without jQuery, it might look like this:
document.getElementById('thisLink').onclick = function(e)
{
//do someting
e.stopPropagation();
}
Other browsers may vary, but by default Firefox doesn't tell the web page that it has been middle-clicked (unless you set the hidden preference to enable the feature). You might be able to create a workaround based on the focus and/or mouseover events instead.
I have a popup window and in that page I have the following code in the body.
<img src="...something"/>
The purpose is to have this popup window close when a user clicks on the image link, and to open a new page and be directed to http://www.example.com.
It works in IE and Chrome, but not in Firefox. The popup window closes but no new window is opened.
Any ideas?
Yes, I can repro this - interesting. setTimeout works around it:
onClick="javascript: setTimeout(window.close, 10);"
I can only guess that once the window closes (which happens before the hyperlink is followed) Firefox stops processing that page.
Edit: better make it 10ms delay - with 1ms Chrome doesn't close the window.
The question is actually solved for the opener but it didn't help my issue (not wished: the new windows under firefox keep the same size as the current popup).
So I find following solution:
function whenClicked()
{
window.close();
opener.location.href = "http://www.example.com";
}
or this if the ppage should open in a new tab:
function whenClicked()
{
window.close();
opener.open(http://www.example.com, '_blank');
}
When you add some functionality to an element's click event via javascript, that functionality is executed before the default click event (in this case, opening a new page), in order to allow for the possibility of intercepting and overriding the default event. The default behavior will only execute when and if the event returns a boolean value of true.
In this case, the additional functionality would be to close the window and my guess is that Firefox chooses to interpret this as "we're all done here", so the click event never returns true, and thus the new page never gets opened.
Evgeny's suggestion of using a short timeout would allow the click event to return true before the window is closed, thus allowing the new window to open.
Google toolbar is creating a serious problem for me in IE 6 when i try to open a window using window.open or if i set target="_blank" for anchor tag. It treats the window as pop up and dispaly pop up is blocked which i really don't want to dsiplay to my user. This problem only occurs if there is a extra code getting executed before window.open, e.g. calling another method at onclick then using window.open. Can somebody tell me how to solve this issue?
The toolbar and other devices like that are intended to protect users from unwanted popup windows. The only way for them to determine whether a window is "wanted" is to determine whether window.open is being called in an event handler for a user-initiated event, like a button click. Thus if you try to do something like call window.open on document load, or in an AJAX success handler, the toolbar (and other blockers) will assume that the popup is suspect.
There's nothing you can do about this other than, as noted by Mr. Buchan, tell your users what to expect. Wherever possible, have your popups launched directly from click handlers.
A more radical change would be to shift away from window.open and use simulated popup windows made from floating elements that cover up part of the page. Something, that is, like what jQuery UI dialogs give you.
Adding the site to your Trusted Sites will work.
Setting target="_blank" shouldn't be triggering a pop-up blocker.