We'd like to have a message popup when a visitor to specific webpages leave those webpages. So we could tie some Javascript to the links on those webpages, but then we can't control if the user exited the webpage by typing in a URL, using a bookmark or just closing the window...
I assume we have limited options if the user tries closing the browser window... but I do know it's possible because Google Docs' Documents offers the chance to cancel closing the window if you have unsaved work while closing the browser.
What are my options? Can I have Javascript called upon going to another webpage? Can I control the text in the popup when trying to close the window?
I'm using jQuery, so if there are good solutions implemented with jQuery that's perfectly fine.
Yes.
https://developer.mozilla.org/en/DOM/window.onbeforeunload
jQuery UI Dialog OnBeforeUnload
There is onunload event you can bind to, first example:
http://www.codetoad.com/javascript/miscellaneous/onunload_event.asp
Related
In my wordpress site, some hacker has embedded a script into it somewhere.
The problem I'm having now is, whenever the site is opened, the first time a user clicks anywhere on it, a spam popup appears, due to the hacker's script.
Is there any way I can prevent the popup window from being created with Javascript?
If I can't prevent it, is there any way that I can close the popup using JS? Unfortunately, since the other script is creating it, I don't have a reference to the opened window. I've seen other scripts calling .close() on an opened window, but I don't have a reference to the new window, so I don't know how to close it.
Here's my site.
The popup on your site is opened by something calling window.open. You can prevent it by assigning something else to window.open and running your script before the other script does:
window.open = () => undefined;
(This does work, if I run this code on your site before your site's Javascript runs, no windows get opened)
It's not uncommon for a client to want to implement this, but if you're the server, it'd be better to fix the server to remove the malicious code, rather than try to patch around it.
I'm developing a Firefox extension and I've been looking for a way to display it automatically (with JavaScript) under certain conditions, as if the user had clicked on the icon.
I know it is possible because some extensions already do it (like Wanteeed, see image below)
I have my javascript getting all the informations that I want, I know when my condition is okay the only thing that I need now is a way to make my little extension's 'popup' magically appear
I've looked for answers as I could, I hope that I didn't miss an already existing post, sorry if I did and thank you very much for your answers !
Are you using the latest WebExtensions format? If so, then you can't just open the popup page programmatically, this is for security reasons. From the MDN web docs:
When the user clicks the button, the popup is shown. When the user clicks anywhere outside the popup, the popup is closed. The popup can be closed programmatically by calling window.close() from a script running in the popup. However, you can't open the popup programmatically from an extension's JavaScript: it can only be opened in response to a user action.
An alternative is to use content scripts to append a position:fixed div to the current page, and then to style it with CSS to match the popup style. This is probably what the extension you referenced is doing.
How to make pop up blockers allow your popup windows?
In general, by popping them up from within the event handler of a user-generated event. For instance, if you have a link and the user explicitly clicks it and you raise a popup from the onclick handler on the link, most popup blockers will allow the popup because of the user's explicit action. In contrast, popups from the window.load event, or code executing as a result of a setTimeout or setInterval call, will typically be suppressed.
Somewhat OT, but: If you can avoid using a pop-up, I would. I'd say (unscientifically) that 95-99% or so of the use-cases where people think they need a pop-up, there's a better design solution. But the answer above is there for those 1-5% situations. :-)
You should use a jQuery UI Dialog, which the popup blocker will not affect.
Users have to set that manually. Imagine what would happen if web apps were allowed to override popup blockers.
You can't. It's up to the user to configure their software to allow pop ups. As a general rule, pop ups generated by user input (i.e. clicking on a button) is usually allowed by most pop up blockers. But this isn't a definitive rule and we can't change it programmatically. If we could it would make pop up blockers useless.
Display a message nicely asking the user to unblock your popups. Obviously the whole point to popup blockers is so you, the site developer, can't forcibly defeat them.
One solution is to make them appear on your page rather than as an actual pop-up (which you can do pretty easily with jquery). If that's not appropriate in your case, asking nicely is a good option.
In your own browser … it depends on the browser and/or third party popup blocker.
When you have no control over the client — open the popup in response to a user generated event (such as onclick).
I am noticing that my share popups are being blocked on our application but not others.
Here is the code execution:
1.) User enters web page.
2.) User clicks on facebook or twitter or googleplus share icon
3.) Onclick event passes the request to an internal controller that saves some information and then redirects back to the originating webpage. This time, however, there is a request parameter that invokes the usage of opening a new window.
The code I have that invokes opening a new window is (for this example we will use facebook):
var url = 'https://www.facebook.com/sharer/sharer.php?u='+copyLink;
window.open(url,'newwindow','width=600,height=600');
Now, if i enable popups it works fine. The problem is the user has to enable popups every time.
Is it a server issue? What is the reason why on other apps they don't have blocked popups but for OUR APP we cannot use popups without enabling popups
Any help would be greatly appreciated.
From what I understand, your application attempts to open a popup window following a page load. The nytimes.com popup appeared in response to a mouse click.
There is an important difference between these two ways to open a popup: the latter follows a user-initiated action but the former does not. As a result, popup blockers will generally block the former but not the latter. A lot of users wouldn't be too surprised if a popup opened when they clicked a button, but they would be more irritated if a popup appeared when a page loaded. The fact that your popup was ultimately triggered by an action on another page doesn't matter - what happens if you manually add to the URL the extra parameter that opens the popup?
Here's an old page on MSDN on popup blockers. It may describe the popup blocker in IE6 (of all browsers), but I think it still provides a reasonable explanation of when popup blockers typically permit or block popups.
Would it be possible to open a popup for your share dialog before calling back to the server? I would expect that opening a popup in the onclick handler would work without needing to explicitly allow popups.
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.