When the running javascript code is triggered by non-user events (timer, ajax ready, etc.), window.open is blocked (in chrome there will be a popup blocker in the url bar).
But changing window.location.href works fine.
I'm wondering why this decision is made. Are there any spec about this? (I only tested on chrome)
One is trying to open a popup, which people find distracting so browser developers block it.
The other is simply navigating the current page - which would make no sense to block as without that functionality standard links would not work.
Related
I'm using the following code to open a new tab on click of a PDF download.
The problem is the new tab becomes the main tab often before the PDF loads.
How can I make the view stay on the current window (PDF) and open the new tab but not switch to it?
Note: In Chrome and Opera they understand the HTML5 download tag so the PDF simply downloads and the current window redirects - All good! So this is only a problem on IE & Firefox.
<h2 style="text-align: center;"><a href="http://cdn2.hubspot.net/hub/155045/file-847580737-pdf/Stepping_into_a_new_age_of_marketing_with_CRM_FINAL_APPROVED.pdf" onclick="casestudiesopen()" download><strong>Click here to download your eBook</strong></a></h2>
<script>
function casestudiesopen() {
window.open("http://www.workbooks.com/case-studies");
}
</script>
Well, I'll advise you to read this Stackoverflow answer, which is, in a way, quite similar to yours (the purpose anyway) :
Javascript disable switches current tab functionality in browser
JS/JQuery is indeed very powerful but also have its limits. Imagine a web page always requesting and keeping focus once you've opened it. I think you would be really annoyed, among other things.
That's why browsers prevent those kind of actions. Common browsers at least. Meaning, there's no way to prevent a browser like Firefox, Chrome, IE & Co. to focus a table since it depend of user's parameters.
You'll have to find a way to workaround your problem. I can propose this answer since it seems to have worked for the other guy.
Example of what I mean:
http://jsfiddle.net/dtipson/ttebddd5/2/
In all other browsers, and in cases not linking to twitter's intents pages, when you open a new window or target blank, it opens in a new tab. But with android, even once you've set the browser to handle links to twitter.com, the new window opens, then immediately closes, and then the original (calling) page navigates to twitter.com. Example code that won't work properly (though I doubt it's anything to do with this):
window.open(
'https://twitter.com/intent/tweet?text=hi',
'intent',
'scrollbars=yes,resizable=yes,toolbar=no,location=yes,status=no,width=550,height=420');
My guess is that this has something to do with have Android handles "application intents": if a page redirects to something that claims to have a native application link, it looks back up the chain of window.opener and affects the original page instead.
To try and block this behavior, I've tried using window.open to open a page that waits a few seconds and THEN redirects to twitter.com/intents. But even here, the new tab opens, waits for however many seconds on that transition page, and then right when it redirects, it closes itself and the original tab redirects to twitter.com instead. I've tried setting window.opener to null (even though that shouldn't do anything). I'm not sure how any code on twitter.com could even affect the original page as they are obviously not on the same domain (and I've tried setting things up so that the original domain does NOT have twitter's widgets.js on it, so they can't be using POSTMessage).
This really seems to be a (imho, bad) )quirk with how Android handles intents. Anyone know of any workarounds?
I'm brand new to StackOverflow and userscripts, and I'm trying to get a fresh start!
Worthless information aside, I'm having a bit of trouble with a tiny script I recently whipped up.
(function () {
$("#enbut").click();
setTimeout(function () {
open(location, '_self');
window.close();
}, 100);
})();
What it does is clicks a button, waits a second for the website to register that I've clicked it, and then it closes the webpage.
The only issue that I am having is that it does not seem to want to close the webpage when the tab isn't the one I currently have active (Opened up so I can see it).
The script works fine if I open the webpage directly, but if I right click and open in a new tab, it's able to click the button, but the page doesn't close until I open up the tab.
Is there any reason this would be happening, or any way to fix it?
I'm using Chrome, so maybe it's just a browser security feature like what they have done with closing windows?
window.close(); throws a security issue when used as a general userscript in today's browsers. Even when using the work around ( open(location, '_self'); ), it does not seem to allow it in tabs that are not the active window.
In order to resolve this, I had to convert the userscript to a Chrome Extension, which gives the script full control over Chrome's security measures. I don't think there would be any other way to get this working as a plain userscript without messing with Chrome's internals, which would be a stupid thing to do for a simple script.
Thanks for the help, guys!
Due to the limitations of a really clunky CMS, I am unable to use
target="_blank"
to open links in new windows. So I'm thinking that my only real solution is to point the link at an intermediary page that opens a new window and then redirects the current window back to the originating page... so something like this:
window.open ("http://www.NewPage.com/");
window.location = "http://www.OldPage.com/";
Of course, most browsers will block the window.open method... so what are my options? Surely there's some clever Javascript trickery to get around this.
No options. You can't do it, and for good reason.
Browsers will only allow window.open() to work when the code that calls it is handling an event like "click". In that circumstance, the browser assumes that the page provides a clickable element and that the user knows there'll be a popup. If a page just calls window.open() for a new window from some context like straight script code, or in a "ready" or "load" event handler, or an XMLHttpRequest callback, then the browser assumes the popup may be something obnoxious.
Some browsers allow the user to relax those rules, but your code can't force that.
You can probably just inject an iframe on the page and position it over everything, but I would really not recommend this.
The browser will be in control of whether the window.open is allowed or not at the end of the day so I wouldn't bother trying to find a solution.
Hi unable to open window.open on page load in ie8 If I use window.location its not opening in new page please help me out of this.
This is because you're running into the popup blocker. This is a Good Thing(tm) :-) You can only open popups in response to the user taking an explicit action, like clicking something (and then typically only from within the event handler itself), not on things like page load where the unwitting user could be (and historically has been) inundated with dozens of windows opening all over the place. (And even doing it in response to an explicit user action may not be allowed by some blockers.)
Are no-one seeing a big problem with running window.open(window.location.href,'_blank') in the onload handler?
This is systematically a recursive function which would continue until the user manages to close the new window prior to the onload handler running.
I'm not saying that this has anything to do with the problem it might just be that IE8 is clever enough to see this..