How can i redirect users of my website to another URL in a new tab, without switching to the new tab? I know this question has been asked so much time but all the solutions dont work for me, i found this code on stackoverflow:
function openWindow( url )
{
window.open(url, '_blank');
window.focus();
}
Click Me
but it does not work... does anyone have a solution for me? thanks!
Other than window.focus(), there isn't really a way to do that. On some devices, you can click using the scroll wheel or right-click the link/button to open something without opening a new tab. That's probably your best bet.
From: How to open new tab in JavaScript without switching to the new tab?
Unfortunately, you can't currently do that -- but you can get close.
You can open a new window, and if you do that without specifying any
window dimensions or window features, most modern browsers will open a
new tab instead (depending on the user's preferences, but then, you
want to do what the user prefers anyway, right?). So just
window.open(url) or window.open(url, name) if you're going to use the
name for something. Be sure to do this in direct response to a
user-initiated event, otherwise the browser's pop-up blocker will
probably...block the pop-up. :-)
Live example
Regarding keeping focus on your window...good luck with that. You can
call window.focus() after window.open(...), but in my experience it
doesn't usually work.
Throwing it out there: If you make the thing the user interacts with a
genuine link with a URL, the user can decide whether to open it in a
new tab, a new window, whatever and whether to give it focus (if
they're sophisticated enough to know Shift+Click and Ctrl+Shift+Click,
or the right-click menu).
It is very browser specific if you can use window.focus() to call back to the tab. My assumption is that in new browsers, this is completely disabled as to stop bad actors from switching tabs without the users permission.
Related
I have a function that I want to open up a URL in a new tab on a click event, but not give that tab focus. Is this possible with javascript?
You can't steal focus from a newly opened window. It's a security feature preventing sites from "taking control" of your browser. That would be a browser configuration setting.
As far as "hiding" focus from a popup, you might be thinking of what's called "PopUnder". Basically you use window.open() and set the option _blank and the paramater alwaysLowered, but it will not work gracefully for an average website. It requires you to have a signed script and take advantage of the Netscape Security PrivilegeManager, like this:
netscape.security.PrivilegeManager.enablePrivilege("UniversalPreferencesRead UniversalBrowserAccess"); // etc...
Sorry bro :)
You can attempt to open a new window, then set focus back to the current window. However, user settings may make this impossible, or it might already be the default behaviour.
Pop–unders are used by some web sites probably to disassociate the web site from the window (i.e. so you don't know where it came frome). So they are assuming a certain naivety on behalf of the user. They are considered spam and treated with the same contempt.
If you outline what it is you are trying to achieve using a pop–under, you might get advice on better ways of doing it. Or not. :-)
To my knowledge it is impossible bro.Since the user's browser Settings will conflict with your logic
i'm trying to open popup window this this jscript:
window.open(myUrl, "");
for some users the page appears in a new tab, but I want it in a popup window.
maybe someone know any reason for it?
For the most part, this is a configuration setting in most modern browsers that the user controls. However, if you specify window dimensions and/or features on the window.open call, some browsers may open a new window when they otherwise would have opened a new tab. For example:
window.open("mypage", "_blank", "width=400,height=200,menubar=no,toolbar=no");
At the end of the day, though, it's stil down to the browser implementation, and as ever, you'll need to test to ensure you're getting the results you hope for across the majority of browsers your app/site supports/typically sees.
This article might be of help: http://www.blazonry.com/javascript/windows.php.
You need to specify size attributes.
window.open(myUrl, "_blank", "width=640,height=480,menubar=no,toolbar=no");
I have a JavaScript chat client running in one browser tab (call it tab A). Now when a message arrives the user might be on another browser tab (call it tab B). I'm looking for ways to change the focus from tab B to my chat client (tab A) when such a message arrives.
I could not find a way to do this.
It is not possible - due to security concerns.
unless by "tab" you mean a window and a popup window that (due to browser preferences) opened up in a new tab. If this is the case, then yes you can.
//focus opener... from popup
window.opener.focus();
//focus popup... from opener
yourPopupName.focus();
The best you could would probably be to change the title of the page alerting the user the tab needs attention (maybe also the favicon - look at how meebo does it, it's really annoying but effective)
It is possible to shift focus back to Tab A by means of an alert in Tab A e.g. alert('New Message')
However, you need to be careful using this as it is very likely to annoy people. You should only use it if you make it optional in your app. Otherwise, updating Tab A's title and/or the favicon would appear to be best as nc3b says.
Chrome and firefox now have notifications. I think notifications are probably a more user friendly way to alert the user that something has changed on your app than popping an alert and forcing them to your page.
Using Javascript, triggering an alert can have the desired effect. Run this code in your console, or add to your html file in one tab and switch to another tab in the same browser.
setTimeout(function(){
alert("Switched tabs");
},
5000);
The alert appearing after the timeout will trigger tab switch. Or you can do something similar! For UX reasons however, you can still use a ping or add and indicator like in Facebook's message counter in the page title ( (1) Facebook ). You can also experiment with Notifications API (experimental).
this worked for me on form submit to reopen the target window..
so it will call window.open on the same target (or new if changed) and then continue to submit the form.
var open_target = function (form){
var windowName = jQuery(form).attr('target');
window.open("", windowName );
return true;
};
<form target="_search_elastic" onsubmit="return open_target(this);">
</form>
Some regular chrome based browser may be controlled by chrome debugger protocol, If browser open with flag --remote-debugging-port=***,I have used the tool cyrus-and/chrome-remote-interface on github and call the CDP.Activate([options],callback) method to switch the browser tab. It works on New MS Edge, should work on chrome also. But sadly this did not work in vivaldi browser, the most feature rich browser I want to use.
When I window.open("http://blarg") in chrome, I get a new tab. If I delay the open, say using a jquery $(hrm).animate({},5e3,function(){window.open(url)); it opens the url in a new window with no status bar, etc — if I give it permission to pop-up that is.
I'm looking for a way to get the instant behavior, that is, I wish to open a URL after an animation, but still in a new tab.
I imagine I could get by with learning a way to instruct chrome to never ever open pop-ups and to always open them in tabs (I imagine there's a webkit setting, why it's not a built in is a mystery); but I'd rather try to find a way to do it from the javascript if possible.
I somewhat doubt there's any way to do this though. I'm not aware of any javascript that's tab-aware.
A similar question was asked about tabs in Firefox, but the same answer applies:
There is no way to force a window to open as a tab. It's all dependent on the user's preference settings.
I've written a bookmarklet to look a word up in a Chinese dictionary:
javascript:Qr=document.getSelection();if(!Qr){void(Qr=prompt('%E8%AF%8D%E8%AF%AD',''))};if(Qr)(function(){window.open('http://nciku.com/search/all/'+Qr);})();
This opens a new tab with search results for your selected word or a word you type in at the prompt. Is there a way to load the new tab in the background? I'd like to keep the focus on the page I'm looking at, and look at the search results later.
There is an option "When I open a link in a new tab, switch to it immediately" in Firefox, this doesn't help.
Edit: Note that this is for my use, so an answer that tells me how to change Firefox (3.0.11) settings to do this would work as well. Also I've tried the following modification, but it's still focusing the new tab.
javascript:Qr=document.getSelection();if(!Qr){void(Qr=prompt('%E8%AF%8D%E8%AF%AD',''))};if(Qr)(function(){var%20oldWin=this;window.open('http://nciku.com/search/all/'+Qr);oldWin.focus()})();
Edit 2:
Looking around to see if I can find an answer I see this guy who's got the opposite problem (new tabs don't get focus, but he wants them to have it), but with no resolution:
Possible to set tab focus in IE7 from JavaScript
There's apparently talk about a _tab target in HTML 5, but that doesn't help me much.
http:/ /forums.whatwg.org/viewtopic.php?t=185&highlight=tab+focus
(apparently as a new user I can only post one link, so I've mauled it)
This seems pretty broken browser behaviour if this is impossible.
In FireFox type about:config and change browser.tabs.loadDivertedInBackground to true.
This has worked for me with browser bookmarklets.
source: http://lifehacker.com/263940/force-links-to-open-in-the-background
No, not programmatically through JavaScript. You don't have control over the user's browser preferences, only they have control over that.
Moreover, even if you did have control over that, you shouldn't do it, because it undermines the control that your script is given to you by the browser. If the user wants a page to open in the background, they should be able to control it, not you, as the developer.
Apparently this is only possible with previously opened windows, not the root window.
Calls to window.open with the same
window name as an already existing
window, loads the URL into that window
and gives a reference to the window
back. The window isn't given focus,
its opener property isn't changed, and
a third argument to window.open is
ignored. You can use the focus method
to give the window focus manually.
var oldWin = window.open("url.html","oldName");
oldWin.focus(); // give focus
Facing the same issue, I only noticed that if you alert() something just after opening the window, Firefox would not switch to the newly opening tab.