Opening two urls with one click while avoiding popup blocker in Chrome - javascript

My objective is to open two URLs in two tabs with just one click with a single link.
So far I have tried two javascript codes (found in this forum that attemps to open 3 URLs) and a jquery code.
It seems that Internet Explorer and Mozilla in their default settings allow this to happen. While in Chrome, I am getting the pop-up blocker. I know that it would be just a matter of allowing the pop-up to happen in chrome in order for the multiple links to open, but it would be a huge advantage for the experience I am trying to build, to have all browsers providing same user experience: one click, two URLs opened in two tabs.
Is there any workaround as of now that you might know that avoids getting the pop-up blocker in Chrome?
Below there are two the javascript codes and the jquery one that I have been testing. All of them have the same effect in chrome triggering, and succeed in Internet Explorer and Mozilla.
I thank you in advance.
JAVASCRIPT
<a href="#" target="_blank" onclick="window.open('http://google.com');
window.open('http://yahoo.com');" >Click to open Google and Yahoo</a>
<a href="http://bloggersentral.blogspot.com/" target="_blank"
onclick="window.open("http://www.tourism.gov.my/");
window.open("http://www.tic.kelantan.gov.my/");">This anchor opens three links in a single click</a>
JQUERY
<body>
Click
<script>
$('.each').click(function() {
var urlList = "http://www.google.com,http://www.yahoo.com"
$.each( urlList.split( "," ), function( index, item ) {
window.open( item, "_blank" )
});
})
</script>
</body>

The user has to manually allow the popups because otherwise it would mean that there could be a whole lot of spam websites. So no.
But also here is a very simple html code for it
Click Here
You could ask the user to disable it: How do I detect popup blocker in Chrome?

I think the only way to prevent pop up blocker blocking popups is that the user says "allow popups for this page". Otherwise this would be a security issue.

If there was an easy workaround to the popup blocker than it wouldn't do a very good job at blocking popups, would it? The best solution in this scenario would either be to separate the links into two separate clicks, or inform the user that they may have to manually allow the tabs to be opened if they have a popup blocker enabled. So in short. No, you can't but there are plenty of better ways to approach the issue.

Related

Open multiple pages in once with javascript/ jquery

I want to create elements which (when clicked) will guide visitors towards two differenct websites (both in new windows). How can I do this? Below are the two ways which I tried, which both do not work as only one page/ window is opened. Thanks in advance!
Timo
The solution as suggested here: How to make a link open multiple pages when clicked . But maybe the solution is outdated, but for me only the first webpage is openend (with all the indicated solutions. (for example the code which is suggested there)
HTML Click Here
$('a.yourlink').click(function(e) {
e.preventDefault();
window.open('https://www.youtube.com/watch?v=H9XIXFwpyEc');
window.open('https://stackoverflow.com/search?q=open+multiple+pages');
});
While both using a class and an id with both a different a href and both with the extra options of opening new window also does not work as it only opens the first window
HTML Click Here
$('.yourlink').html(''); // Add page
$('.yourlink a').attr('target', '_blank');// Make sure it opens in new window
$('#extra').html(''); // Add page
$('#extra a').attr('target', '_blank');// Make sure it opens in new window
Using Chrome, I'm not seeing the problem you describe. But when I tested it in IE, I suddenly got a "popup blocked" message.
It's not strange though. I'd hate it if a single click on a link could suddenly spawn 10 new windows. In this scenario I actually think IE handles it better (by blocking the second window).
The thing is that window.open will only work if the action that invokes it is a trusted event. That usually means a user-initiated event, like click. But what Chrome doesn't account for (I assume) is that a single trusted event can then invoke several window.open.
I've tried to work around this feature, but have not (yet) been able to fool IE. The options, as I see it, are:
Ask users to add your site to the popup exceptions (internet settings).
Spawn the pages in iframes within your own site
Ask users to use another browser ;)
Or the obvious:
Use separate links for the windows

Open new tab in background in Firefox with Javascript?

I want to use JS to open a new tab in Firefox but in the page group. That will be Google, in the example. I want the original tab to stay focused and open Facebook. Would also work in as many other browsers as possible, ideally.
I am using this code, and it works, in a way.
The first click will open the new tab and focus on that, which is not what I need.
But, if I make the first tab, the original, go back then press it again, I get the desired behaviour. The new tab will open in the background but the original one will be the one that's focused and changes to the webpage.
<button type="button" onclick="open_in_bg('http://facebook.com', 'http://google.com')" >Press Me</button>
<script type="text/javascript">
function open_in_bg(c_url, n_url)
{
window.open (n_url, "mywindow" );
window.open (c_url+"#maintain_focus","_self");
}
</script>
My first thought is that it may be possible to replicate the result from the 2nd click for the 1st. Possible? Any viable solution?
Sorry to say, I do not believe this is possible. Most browsers have removed any support for controlling tabs via javascript as a security issue. This was removed to prevent pop-unders by nefarious sites.
Here is an info about the user settings

Userscript won't close window if it isn't active

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!

How to get Chrome to open multiple sites in a new tab

I'm developing a tool that lets you open multiple pages at once with a shortcut, to be used for things like opening your daily sites or querying multiple search engines for a phrase. In Firefox, Internet Explorer and Opera, assuming you've unblocked pop-ups for the domain, the code works as expected.
Chrome, however, opens the sites in new windows instead of tabs if the links are opened automatically when the page loads. If openAll() is commented out and the button is clicked or a key is pressed, the pages open in tabs. Note it's calling the exact same function.
The best solution I've found (which isn't saying much) is the One Window extension. It works, but you can see the new window open then get sucked back in, and it keeps you from opening new Windows with Ctrl-N, forcing you to drag tabs out to use another Chrome window.
I can understand there not being a programmatic way to change this because it's a browser setting, but as a user of the tool it's annoying to have the sites all open in new windows. Is there a Chrome setting or extension that will open links in tabs when they're loaded without user input? I realize opening a bevy of windows is the very thing browsers aim to stop, but this is one time where I want to allow it.
<input id="openAllBtn" type="button" value="Open all links"> (Or press any key)
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
function openAll() {
window.open('http://yahoo.com/');
window.location.replace('http://www.bing.com/');
return false;
}
$(document).ready(function() {
$(document).bind('keypress', openAll);
$("#openAllBtn").bind("click", openAll);
openAll();
});
</script>
Here's a Fiddle of the code: http://jsfiddle.net/sfzjR/
Is there a Chrome setting or extension
that will open links in tabs when
they're loaded without user input?
Check out the create method in the chrome extension docs. By default it will open a new tab, you can optionally specify the window you want that tab to open in, and give the tab a url.

How can you open 2 windows even when ie6 popup blocker is enabled

It seems like when trying to open 2 windows from a succession of windows.open calls, it only allows the first window to open and deletes the reference to the second window. I know this probably sounds a little kludgy, but we do need to have that second popup.
Any ideas?
Unfortunately we are addressing a user-case, where ie6 on the user end has popup blocker enabled.
EDIT: I just realized that you probably are using a blank ('') window name for both windows:
var win = window.open(url, '', 'blah=1');
var win2 = window.open(url2, '', 'stuff=1'); //later
This is probably handled with different windows in browsers other than IE6.
If that does not work, you might consider injecting divs that display on top of your content (instead of using popups), which is considered a better practice.
The IE pop-up blocker, by default, only allows one new window to be opened per user-initiated-action (i.e. a click on some element). If you try to open two new windows in the same handler in response to a single user-initiated-action, only the first window will successfully be opened. This is by design.
There is an override key that users can use: on IE6 I think it is CTRL, but it might be CTRL+ALT because it got changed in later versions (not sure if that was back-ported or not).
If you go to Tools->Internet Options->Pop-up Blocker->Settings->Blocking Level: and look at the value in the drop-down box for "High" it will tell you the override key in a parenthetical phrase.
In the same settings dialog, you can also add this specific site to the "Allowed sites" list, and then pop-up blocker will let all new window creation attempts on said site succeed. I'm pretty sure this list can also be pre-populated through group policy or IEAK or something like that too. But it's just a list that is stored in the registry, so you can also write log-in scripts that will just add things if they need to be added.
If you have further questions, let me know (I was the developer who implemented the IE pop-up blocker).
IE6 makes it sound like it's a corporate installation. Assuming that's true, contact your administrators and have group policy set your internal website to be in the Intranet zone, and turn off the popup blocker for that zone.

Categories

Resources