How to create popup new window browser with small size - javascript

i tried to create link, if clicked will be popout a new windows browser with small size, i tried this code
<p>Click the button to open a new browser window.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
window.open("http://www.w3schools.com");
}
</script>
the result is it success move to other link but opened in new tab on parent browser, what i tried to create is link opened in new window browser with small size.

You can set height and width.
window.open("http://www.w3schools.com", "", "width=200,height=100");
To set position center.
var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;
var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
var left = ((width / 2) - (w / 2)) + dualScreenLeft;
var top = ((height / 2) - (h / 2)) + dualScreenTop;
window.open("http://www.w3schools.com", "", "width=200,height=100, top=' + top + ', left=' + left");

Try the code below:
window.open(url, windowTitle, "height=100,width=200");
When you specify height and width it will open a new window for you. I checked this with IE and it works fine.
Hope this will help you.

<script type="text/javascript">
function Warn() {
alert ("This is a warning message!");
document.write ("This is a warning message!");
}
</script>

Related

the button doesn't postback which opened by PopupCenter

Hi I used the Popupcenter function which is from neofnd/popupCenter to open the aspx page. The aspx page is opened but buttons in the aspx page doesn't fire, no postback. Does someone tell me how to solve it. Thanks in advance.
There is the code from github
function PopupCenter(url, title, w, h) {
// Fixes dual-screen position Most browsers Firefox
var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;
width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
var left = ((width / 2) - (w / 2)) + dualScreenLeft;
var top = ((height / 2) - (h / 2)) + dualScreenTop;
var newWindow = window.open(url, title, 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
// Puts focus on the newWindow
if (window.focus) {
newWindow.focus();
}
}
There is my button to open call this function.

How to fix window to parent window?

How can I fix popup window (window.open()) to parent window? When I click to another tab my window is lost.
function popitup(url, w, h) {
var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : window.screenX;
var dualScreenTop = window.screenTop != undefined ? window.screenTop : window.screenY;
var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
var left = ((width / 2) - (w / 2)) + dualScreenLeft;
var top = ((height / 2) - (h / 2)) + dualScreenTop;
popupWindow = window.open(url, title,"resizeable=yes,location=no,width=" + w + ", height=" + h + ",top=" + top + ",left=" + left + ", overflow-x=hidden");
popupWindow.pw = pw;
if (window.focus) {
popupWindow.focus()
}
return false;
}
If you want the popup attached to the page like that, don't use a popup at all; use an absolutely-positioned element within the page (for instance, a div) and load the relevant HTML into that div.

window.onbeforeunload not firing in Chrome but working in Firefox

I have a main window and a pop up window. Popup window is created on the main window.
Like parent.php is the main window. On this page, I have a JavaScript function to reload the page as below:
function popUpClosed() {
window.location.reload();
}
We can open popup window from parent.php. Now I want to execute popUpClosed() function on parent.php from popup window when we close / navigate the popup window.
I have tried following methods to achieve the same.
Method 1
window.onunload = window.onbeforeunload = function() {
if(window.opener && !window.opener.closed) {
window.opener.popUpClosed();
}
};
Method 2
window.onbeforeunload = Call;
function Call() {
if(window.opener && !window.opener.closed) {
window.opener.popUpClosed();
}
}
Method 3
window.onpagehide = function() {
window.opener.popUpClosed();
}
Everything is working is fine in every browser except Google Chrome. Chrome is not firing any of the function.
However, this is happening from last 2-3 days. Earlier all the things was working well in Chrome. (Might has been caused due to the latest Chrome updates)
Any suggest will be appreciated.
Latest google Chrome has a problem with window.opener. window.opener navigation inside a beforeunload/unload handler is ignored in the latest version. However there is a workaround for this. Try below code:
var btn = document.getElementById("btn");
function detectClick(event) {
var wind = window.open('urlhere', 'name', 'width=' + screen.width/2 + ', height=' + screen.height/2);
wind.onbeforeunload = function(){
popupClosed(wind);
}
}
btn.addEventListener("click", detectClick, false);
function popupClosed(wind) {
setTimeout(function() {
wind.opener.location.href = window.location.href;
}, 0);
}
<input id= "btn" type="button" value="open popup" />
THIS CODE WON'T EXECUTE HERE ON STACK OVERFLOW BUT WILL WORK IN BOTH: Chrome AND Mozilla Firefox WHEN EXECUTED THROUGH jsfiddle OR SERVER.
Here is a working code: jsfiddle
here is the link of bug report that confirms beforeunload/unload handler issue.
You can try the below code:
I have created the common function for open the POP up window:
function PopupCenter(url, title, w, h) {
var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;
width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
var left = ((width / 2) - (w / 2)) + dualScreenLeft;
var top = ((height / 2) - (h / 2)) + dualScreenTop;
child = window.open(url, title, 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
if (child.focus) {
child.focus();
}
}
For checking the child window closed or not I am using the setInterval function and check the interval like below:
function checkChild() {
if (child != undefined && child.closed) {
//alert("Child window closed");
clearInterval(timer);
location.reload();
}
}
DEMO

TypeError: undefined is not an object (evaluating 'newWindow.focus')

I am having the following javascript which open ups a child window with the sizes and align it int he center of the screen
function PopupCenter(url, title, w, h) {
// Fixes dual-screen position Most browsers Firefox
var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;
var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
var left = ((width / 2) - (w / 2)) + dualScreenLeft;
var top = ((height / 2) - (h / 2)) + dualScreenTop;
var newWindow = window.open(url, title, 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
// Puts focus on the newWindow
if (window.focus) {
newWindow.focus();
}
var timer = setInterval(checkChild, 500);
function checkChild() {
if (newWindow.closed) {
var message = 'You have aborted the transaction by closing the window !';
var status = 'closed';
paymentResponseDisplay(message,status)
clearInterval(timer);
}
}
window.onbeforeunload = function() {
if(!newWindow.closed){
return "Do you really want to abort this transaction?";
}
}
}
But in safari I am getting
TypeError: undefined is not an object (evaluating 'newWindow.focus')
I am unable to find a fix for this. Can some one help out?
In my case, this problem occurred because I called popup from the ajax result in the Safari.
I received ajax with sync and it worked fine.
window.open() works different on AJAX success

FB.login popup is not centered and even on separate screen than the active browser window

When I use the FB.login function from the JavaScript SDK, popup shows up, but it is aligned all the way to the right and appears on the second monitor screen. This only happens if browser window is maximized, if it is not maximized popup is centered correctly.
Reproducible in IE 10, Firefox 20
I've ended up overriding the window.open and changing the left= in attribute string. FB API puts ridiculously large number in there for some reason.
Window.open overide worked for me too...
placing code snippet to help on it:
window.open = function (open) {
return function (url, name, features) {
var w = 475;
var h = 183;
// Fixes dual-screen position Most browsers Firefox
var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;
var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
var left = ((width / 2) - (w / 2)) + dualScreenLeft;
var top = ((height / 2) - (h / 2)) + dualScreenTop;
var override_features = 'width=475,height=183,left=' + left + ',top=' + top + ',scrollbars=1,location=1,toolbar=0';
// set name if missing here
//name = name || "default_window_name";
return open.call(window, url, name, override_features);
};
}(window.open);
w = desired popup window width
h = desired popup window height
u use the override_feature variable to set any param you wish.

Categories