sorry if this is a repeat question!
I have the following Javascript which works fine in Firefox and produces a pop up window. In IE 9 however it does nothing at all and in Chrome it works like a link and changes the current page!
Any advice appreciated!
window.open(page,name,'width='+width+', height='+height+',location=yes,menubar=no,resizable=no,toolbar=no,scrollbars=yes');
Thanks in advance.
This is a working example
JS
function openWindow()
{
var width=668;
var height=548;
var page="http://google.com";
window.open(page, "awindow", "width="+width+",height="+height+",location=yes,scrollbars=yes, resizable=yes");
}
HTML
Open
DEMO.
Did you create the variables correctly?
This code is working for me:
var page = 'page.php';
var name = 'pagename';
var width = 200;
var height = 100;
window.open(page,name,'width='+width+', height='+height+',location=yes,menubar=no,resizable=no,toolbar=no,scrollbars=yes');
EDIT
In my webapp I use the following function for opening windows. It should work in all browsers.
function wopen(url, name, w, h, scrollb) {
scrollb = typeof(scrollb) != 'undefined' ? scrollb : 'no';
w += 32;
h += 96;
wleft = (screen.width - w) / 2;
wtop = (screen.height - h) / 2;
var win = window.open(url, name,
'width=' + w + ', height=' + h + ', ' + 'left=' + wleft + ', top=' + wtop + ', ' +
'location=no, menubar=no, ' +
'status=no, toolbar=no, scrollbars=' + scrollb + ', resizable=yes');
// Just in case width and height are ignored
win.resizeTo(w, h);
// Just in case left and top are ignored
win.moveTo(wleft, wtop);
win.focus();
}
Where are you making the call to window.open? IE9 will block calls if they're made during page load as part of its popup blocker. Chrome does something similar, but redirects new windows to the main tab (thus, putting the user in control). As for Firefox... check your FF popup blocker settings.
Related
window.open not working in iphone at the time of document ready event
I have write the code to open the new windows at the time of page load event. this event working fine in evenry browser instead of iphone(Safari). now i dont know what the alternative way to do this?
please help me to solve this issue.
OpenWindow('http://www.google.com', 800, 600, true);
function OpenWindow(query, w, h, scroll) {
var l = (screen.width - w) / 2;
var t = (screen.height - h) / 2;
winprops = 'resizable=1, height=' + h + ',width=' + w + ',top=' + t + ',left=' + l + 'w';
if (scroll) winprops += ',scrollbars=1';
var f = window.open(query, "_blank", winprops);
}
Here is a workaround
<script>
document.write("<a id='mylink' href='mypage.html'></a>");
document.getElementById('mylink').click();
</script>
or
<body onload="myfunct('mylink')">
and declare the function as above
I am doing FreeCodeCamp's Random Quote Machine exercise.
Using this answer, I tried to set up my tweet button to open up a new window rather than a tab that the user could use to Tweet this quotes with.
The only issue is is that it is opening up a new tab as well as a new window. Is there any way I could get it to just open the new window?
HTML for Tweet Link
<a class="twitter-share-button" href="https://twitter.com/intent/tweet" target="_newwin">
Tweet</a>
Related JavaScript
jQuery('a[target^="_newwin"]').click(function() {
var width = 500;
var height = 300;
window.open(this.href , 'newwindow', 'width=' + width + ', height=' + height + ', top=' + ((window.innerHeight - height) / 2) + ', left=' + ((window.innerWidth - width) / 2));
});
Pen of my Project
Thank you!
You are getting both your custom behavior (opening a new window) and the standard behavior of a link (in most browser nowadays, opening a new tab). To prevent the latter, you need to use the ´preventDefault´ method:
jQuery('a[target^="_newwin"]').click(function(e) {
e.preventDefault();
var width = 500;
var height = 300;
window.open(this.href , 'newwindow', 'width=' + width + ', height=' + height + ', top=' + ((window.innerHeight - height) / 2) + ', left=' + ((window.innerWidth - width) / 2));
});
I have a link on a page on clicking which I open a new window like a popup.
Now I would like the user to close it before he can go back to the old page. He should not be able to navigate to other tab before closing it. How would I do this?
<script type="text/javascript">
function popup(url) {
var width = 300;
var height = 200;
var left = (screen.width - width) / 2;
var top = (screen.height - height) / 2;
var params = 'width=' + width + ', height=' + height;
params += ', top=' + top + ', left=' + left;
params += ', directories=no';
params += ', location=no';
params += ', menubar=no';
params += ', resizable=no';
params += ', scrollbars=no';
params += ', status=no';
params += ', toolbar=no';
newwin = window.open(url, 'windowname5', params);
if (window.focus) {
newwin.focus()
}
return false;
}
</script>
Centered popup window
You should define your window as a modal window, window.showModalDialog.
Look at the following instructions showModalDialog
An alternate way is to use jQuery dialog. in this case the whole screen is blocked and user can not do anything until dialog is closed. see here
Create Close button in your popup.html page then close a window.
<input type="button" value="Close" onclick="window.close()">
thank u..
I am implemented a custom twitter button by using the following
var url = attrs.twitterShare;
var width = 575;
var height = 400;
var left = (getScreenWidth() - width) / 2;
var top = (getScreenHeight() - height) / 2;
var opts = 'status=1' + ',width=' + width + ',height=' + height + ',top=' + top + ',left=' + left;
window.open('http://twitter.com/share?original_referer=' + url, 'twitter_share', opts);
Summary, an element is clicked which calls the javascript open that opens up twitter.com/share with the url to be shared. But I wondering is there a callback I can use to get stats on the share linked such as the number of retweets?
You could embed an iframe in the popup window, which loads the share url, and when the page changes you can handle the
function callback(url) { alert(url); };
var window = open('url','name','height=300,width=300');
window.document.write('<iframe onLoad="window.opener.callback(this.contentWindow.location);" src="https://twitter.com/intent/tweet?url=http%3A%2F%2Fbbc.co.uk&text=This is a Twitter title">');
I have a custom function that will open a window to the center of the screen from a different url. On my current case I am opening a url outside my domain. This is my function.
function wopen(url, name, w, h) {
w += 32;
h += 96;
wleft = (screen.width - w) / 2;
wtop = (screen.height - h) / 2;
if (wleft < 0) {
w = screen.width;
wleft = 0;
}
if (wtop < 0) {
h = screen.height;
wtop = 0;
}
var win = window.open(url,
name,
'width=' + w + ', height=' + h + ', ' +
'left=' + wleft + ', top=' + wtop + ', ' +
'location=no, menubar=no, scrollbars=yes');
// +
//'status=no, toolbar=no, scrollbars=no, resizable=yes');
win.resizeTo(w, h);
win.moveTo(wleft, wtop);
win.focus();
}
This works perfectly on IE6, and FF but not on IE7
The issue is that you are attempting to open a window with a separate domain, which in IE7 and higher is considered a security issue. Essentially, when you open that new window, it creates a new process and leaves your process separate, so you can no longer manipulate that other window.
http://social.msdn.microsoft.com/Forums/en/iewebdevelopment/thread/e9cebb92-f943-4a79-b29b-7376039ea6a0
http://msdn.microsoft.com/en-us/library/Bb250462.aspx
So, once you open that new window with a domain different from your own, you lose control of it. I don't see a way to change this without adjusting the end-users computer.
EDIT
Hmm, apparently you can get around this by opening a window that you do have control of, then changing the window.location.href to your url. Try this:
function wopen(url, name, w, h) {
w += 32;
h += 96;
wleft = (screen.width - w) / 2;
wtop = (screen.height - h) / 2;
if (wleft < 0) {
w = screen.width;
wleft = 0;
}
if (wtop < 0) {
h = screen.height;
wtop = 0;
}
var win = window.open('about:blank', // <- Note about:blank
name,
'width=' + w + ', height=' + h + ', ' +
'left=' + wleft + ', top=' + wtop + ', ' +
'location=no, menubar=no, scrollbars=yes');
// +
//'status=no, toolbar=no, scrollbars=no, resizable=yes');
win.location.href = url;
win.resizeTo(800, 150);
win.moveTo(wleft, wtop);
win.focus();
}
wopen('http://www.yahoo.com/', 'yahoo', 250, 250);
I don't know if this is a hack or not; I'm surprised it's that easy to get around, at least for changing window resize and whatnot. But, it works (at least on IE8).
there are many security things that a browser and os checks for any window.
for this case, i am not sure but try this also.
if your mouse button is clicked and hold on at the time of resizing browser window through your js code then you will get access denied error.
reason is OS denied such activities when real physical users is ready for mouse drag event.
see below url
http://prcoldfusion.blogspot.com/2012/06/access-denied-javascript-error-internet.html