This happens in IE6 when the user opens a popup window that opens a PDF inside. (this part works).
Then, the user opens another popup window, and at this point i get this error.
There is a good description and a possible solution here
my question is this:
Is there a better solution? Opening up a window and closing it right away seems like a silly solution to me.
I think I've got a better solution that doesn't involve closing the window first. The issue is that IE won't override a window (PDF or otherwise) if you attempt to open it again with an empty URL (i.e., ''). It will override a PDF with a non-empty URL, however. That could be a file, but about:blank works even better (which is what an empty URL does normally).
Depending on how your code is written, you may still want the try/catch, but this should eliminate the need:
windowHandle = window.open('about:blank',name,attributes);
windowHandle.document.location.href = url;
windowHandle.focus();
about:blank will force the PDF out of the child window and allow you to do what you need to do. It might not be a bad idea to place the setting of the URL and focus() in a windowHandle.onload() handler, so there aren't any timing issues with disposing of the PDF. I.e.:
windowHandle.onload=function(){
windowHandle.document.location.href = url;
windowHandle.focus();
};
I solved the problem using a try catch block.
windowHandle = window.open('',name,attributes);
try {
windowHandle.document.location.href = url;
} catch (exc) {
windowHandle.close();
windowHandle = window.open('',name,attributes);
windowHandle.document.location.href = url + suffix;
}
windowHandle.focus();
Seems to work for me.
Related
I need to simply open to browser windows when user perform an action (to keep it simple in this example I use setTimeout).
I have notice that the browser is able to open only the first window.open and not the remaining.
What is the cause? How to fix it?
setTimeout(function() {
window.open("https://www.w3schools.com");
window.open("https://www.google.com");
}, 3000);
You need to make the windows unique, by default, the browser gives the new window a name, but doesn't dynamically update it when multiple instances of window.open occur (source - first line of https://developer.mozilla.org/en-US/docs/Web/API/Window/open). Give them unique names (with ids help) like so:
window.open('/path/to/page.php', 'UNIQUE_WINDOW1', 'width=300,height=400');
window.open('/path/to/page2.php', 'MORE_UNIQUE_WINDOW5', 'width=300,height=400');
if this doesn't work you can do:
window.open('/path/to/page.php');
$.post('/path/to/page2.php', {}, function(res)
{
var win = window.open('', 'WINDOW_NAME', 'width=540,height=440');
with(win)
{
open();
write(res);
close();
}
});
what this will do is, post nothing to the page but, res returns the output of that file, so you assign window.open to a variable, and with it, open it and write the output to the file. :)
I am working on a web based application, in which I have to open popup window. I am using window.open() method to open the popup, like this:
window.open(url, "popupWin");
where url contains the URL I would like my popup window to navigate to. Now, the problem is, if I execute window.open() from multiple tabs (with same or different URLs), at least on Chrome, it might / might not give you the same window which was opened earlier. This behaviour is inconsistent, I mean, either it should get me fresh window every time, or it should get me previously opened window every time.
I need to persist the same popup window for entire domain. How can I do that?
Well looks like there is a direction to go or at least to give it a try.
It fully remains on localStorage which gives you ability to share the knowledge across your tabs within a single domain.
The code I give below does not work yet (it is only a direction), so don't expect too much from running it as it is.
What it does: it saves the popups by the url in a localStorage and when you try to open a new one with the same url it won't do that. If you don't want to distinguish them by URL it is even simpler: store boolean in a localStorage instead of an object.
What it does not do but should:
it should listen to the popup onunload (close) event and reset the localStorage information accordingly. Best for you here is just to set your localStorage boolean value to false
it should listen to the current tab onunload (reload, close) event and also reset something according to Your logic. As I understand the best for you would be just check whether this tab is the last one from your domain (you can also do this using localStorage, e.g. on every new tab adding its identifier, e.g. creation timestamp and destroying it on tab close) and if it is set your localStorage boolean value to false.
This, I think, would be enough to solve the problem. And finally a small piece of code:
// get the localstorage url map
function getOpenPopups() {
var obj = localStorage.getItem('mypopups');
return obj ? JSON.parse(obj) : {};
}
// set the localstorage url map
function setOpenPopups(object) {
localStorage.setItem('mypopups', JSON.stringify(object))
}
// open the popup
function popup(url, title) {
var popups = getOpenPopups();
// check whether popup with this url is already open
// if not then set it and open the popup
if (!popups[url]) {
popups[url] = true;
setOpenPopups(popups);
return window.open('abc', 'cde');
}
else {
return false;
}
}
jsFiddle
From w3c documentation we can see that window.open() returns a reference to the newly created window, or null if the call failed. That means we can keep it in memory and check for closed flag of that window.
var newWindow = window.open('/some/path', 'TestWindow');
// ...
if (!newWindow.closed) {
}
Keep in mind that if window with following name exists, page will be loaded in the same window without opening new one.
Other variants of name parameter like _blank, _self, _top, _parent you can find in official docs too.
I have a JQUERY function as follows
this.getURL = function()
{
var name = getName();
alert("Menu.aspx?name"+name);
//window.location = "Menu.aspx?name"+name;
}
When I alert the URL I am attempting to go to, it is correct. However, when I call window.location on that string, the page just refreshes without going anywhere.
I have similar code where I have used window.location and it works. I typed in the url into my browser and it works as well.
At worst (even if the URL was wrong), I was hoping that it would just redirect me to some URL. However, I can't get it to do anything other than refresh the current page.
Also to clarify, the page which calls this function is not Menu.aspx
Thanks in advance.
If you're using a relative path try setting window.location.pathname, otherwise set window.location.href for a full path.
You may also want to try self.location.href
In my experience, it's been difficult to get redirects like this to work right. I've had to use window.location.replace(<url>). If you're just changing an anchor tag, it's even more difficult. You have to do the following to get it to work in all browsers:
window.location.replace(<url>);
window.location=<url>;
window.open(<url>,'_self');
window.location.reload();
I'm using a script to mount my mailto link and call the default email editor. But I can't use document.location.href because of some bug in IE9, so I use window.open. It works. But I need to close the IE windows opened.
The problem is the window.close doesn't return the window reference.
function doMailto() {
var sMailto = 'mailto:?bcc=';
sMailto += document.getElementById('<%= txtEmails.ClientID %>').value;
out = window.open(sMailto);
out.close(); //CANT CALL CLOSE, BECAUSE OUT IS NULL
}
You have an extra paren at the end of the assignment line.
Also, are you trying to close the window in the same function as assigned? If not, you may need to declare the window variable outside the function so it can be closed when needed.
aside the syntax errors (two commas, two parentesis closing)...
you are opening a window that is external to the browser, your default mail client. You cannot control it through javascript.
Maybe it's an immediacy problem, try using:
var out = window.open()...
setTimeout(function(){out.close()}, 200)
and fiddle with the 200ms to see if it works then.
try
top.location.href = 'mailto:....';
you won't need to open or close any windows this way
I have general purpose bookmarklet, which begins with:
javascript:with (window.open("")) {/* lots of irrelevant characters */
It works as should (opens new window and writes a report about page elements, specifically), however fails on certain pages, when authors decide what open is a good name for function:
/* somewhere in global scope */
function open() { /* something */ }
... effectively replacing window.open and breaking bookmarklet operation. By the nature of bookmarklet i cannot do anything to prevent such misbehaviour.
Is there any way to call original native window.open to recover in this cases?
Please note, my primary browser is Opera, i really want to make it work in there.
window.constructor.prototype.open
Nope, sorry. You can do some hackery to get a new one though :)
var ifr = document.createElement('iframe');
document.body.appendChild(ifr);
ifr.contentWindow.open(...)