Can I call window.open from confirm dialog box using javascript?
My Requirement:
In case browser pop up blocker was enabled means ,i want to open pop up window using window.open(); but it was not happening so that i have to show the confirmation message which is "Browser popup blocker was enabled, now pop up has been open". now i click 'OK', on that time i want to call window.open() function.
<html>
<body onload="openPopup();">
<head>
onload Popup Window
</head>
</body>
<script>
function openPopup () {
var href= 'http://google.com';
popUp = window.open(href, "_blank");
if (popUp === null || typeof popUp === 'undefined') {
var ret = confirm("this alert is displayed by Blocker, Continue to Open ?");
if(ret){
window.open(href,"_new");
}
} else {
popUp.focus();
}
}
</script>
Is it possible or is there any other way to handle this scenario?
No.
Pop-ups are generally only allowed following a user's click, and as you've already seen in your code onload usually won't allow it.
However, you could use your own UI, to create a custom confirmation box that the user can click on to open the pop-up. This will work, unless the browser's pop-up blocker is overzealous enough to block even click-to-open popups without prior confirmation within the browser itself.
Related
Is there a way to detect if the current user agent support popup using native javascript?
for example in WEB, window.open will open a popup, and the parent will know when the popup is closed.
however in Iphone for example or Ipad it opens the popup in new tab and the parents will lose the context. so the parent's can't listen to the event when the new tab is closed.
Currently am using a work around, is by checking each device manually if it's supports popup and then in my javascript i check the user agent if it's included in the list i checked manually. but i was thinking if there are more intelligent way to implement this.
Thanks.
you can try this script:
<Script>
var windowName = 'userConsole';
var popUp = window.open('/popup-page.php', windowName, 'width=1000, height=700, left=24, top=24, scrollbars, resizable');
if (popUp == null || typeof(popUp)=='undefined') {
alert('Please disable your pop-up blocker and click the "Open" link again.');
}
else {
popUp.focus();// this will open popup if the browser allow it.you can do your implementation on popup support here
}
</script>
main source:here
What are you trying to accomplish, exactly? Are you trying to detect if the current window is a popup? If so, BornToCode's response in this thread might be your answer. He suggests to test if window.opener is defined:
if ( window.opener !== 'undefined' )
Below is my code for displaying a pop up window when the user clicks on an image link:
Javasript
function plusbutton(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
href=mylink;
else
href=mylink.href;
window.open(href, windowname, 'width=800,height=550,scrollbars=yes');
return false;
}
HTML
<a href="previousquestions.php" onclick="return plusbutton(this, 'previousquestions');">
<image src="Images/plussign.jpg" width="30" height="30" alt="Look Up Previous Question" class="plusimage"/>
</a>
My question is that when the user opens up the window, then when the user clicks away from the window, the window minimizes. I don't want this to happen. If the user clicks away from the window, then it should still display the window.
It is like when you click on "Save As" on Microsfot Word, when the "Save As" window appears, if you click of it, the window still appears, stating that you must either Save the document or Cancel the Save As before being able to do anything else on the document. I want the same this to happen with the pop up window above, how can this be achieved?
Thanks
What you are trying to do is called a "Modal Window", and it is not possible to achieve this goal with a browser window. In fact, you can not even guarantee that it's a window: some browsers would in some cases just open a new tab.
What you should do is open a window with pure HTML within you application. Some librarie allow you to do this easily: jQuery UI dialog for example. Check out http://jqueryui.com/demos/dialog/#modal
If all your architecture bounds your popup content to be delivered by a separate URL, you can do one of the following things:
Either put and iframe in the popup (easy but dirty)
Or retrieve the HTML content with an XMLHttpRequest and use it to define the content of the popup. Maybe a little more tricky, but cleaner.
You should display a modal div, it look like a windows, but it don't dismiss when you click out
if you can use a javascript lib like jQuery, you can do it this way :
$('yourdivselector').dialog({modal : true});
with the modal option set to true
http://docs.jquery.com/UI/Dialog
I want to close a session in my application when user closes a window by pressing the close button in the title bar or using the Alt + 4 keyboard combo.
I am able to handle X using the code below, but this is not handling Alt + F4. I also tried onbeforeunload but this is not calling the function at all. I am testing with IE9.
<body onunload = "closingWindow()">
<script>
function closingWindow(){
if((window.event.clientX<0) || window.event.clientY<0)){
//Ajax call to close session
}
}
</script>
Check out window.onbeforeunload
https://developer.mozilla.org/en/DOM/window.onbeforeunload
Live Demo
window.onbeforeunload= function(){
// Ajax call to close session
}
Hit alt+f4 in the demo and you will notice the alert before it closes as well.
I am using a Facebook login method in my code on page load, but when I execute this code, the pop-up blocker closes the Facebook permission window.
How can I open this window with Javascript without needing to make an exception in the pop-up blocker?
Below is my code:
FB.login(function(response)
{
if(response.session!=null)
{
window.location.href='http://example.com';
}
},
{ perms: 'email,user_birthday,publish_stream' });
You can do something like -
var uri = encodeURI('http://example.com');
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
window.location.href=uri;
} else {
window.location = encodeURI("https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri="+uri+"&response_type=token");
}
This will just redirect directly instead of opening a pop-up
This is specifically denied in the documentation:
"You should only call this on a user event as it opens a popup. Most browsers block popups, unless they were initiated from a user event, such as a click on a button or a link."
It's also simply poor UX.
There'd be no point in popup blockers existing if you could just code around them. You'll either need to find a method that doesn't use a popup or require some user interaction with the browser to open the popup.
Yeah you need to call it with a user event, but strictly the onclick event, not any other:
Login <!-- works -->
Login <!-- doesnt work -->
Login <!-- doesnt work -->
If you try to open a popup automatically then there is a high possibility that popup blockers will become activated, as far as I know, it has to be based on some User action, for example click of a button. Try to execute this code on click of a button, it should work.
I would like to have a button on a web page with the following behavior:
On the first click, open a pop-up.
On later clicks, if the pop-up is still open, just bring it to the front. If not, re-open.
The below code generally works in Firefox, Safari, and IE8 (see here for Chrome woes). However, I have found a failure mode in Firefox that I don't know how to deal with:
If for some reason the user has opened a second tab in the pop-up window and that second tab has focus within that window, the popupWindow.focus() command fails to have any effect. (If the first tab has focus within that window, everything works just great.)
So, how can I focus the popup and the desired tab in Firefox?
<head>
<script type="text/javascript">
var popupWindow = null;
var doPopup = function () {
if (popupWindow && !popupWindow.closed) {
popupWindow.focus();
} else {
popupWindow = window.open("http://google.com", "_blank",
"width=200,height=200");
}
};
</script>
</head>
<body>
<button onclick="doPopup(); return false">
create a pop-up
</button>
</body>