window.open(url,'_blank') opens in background tab, not getting focus - javascript

In Chromium-based browsers version 107 I notice opening a new tab with window.open does not give focus to the new tab. Previous it did, and in Firefox it still does
//code before
{
let url = "https://google.com";
window.open(url,"_blank");
}
When I run the window open in the console it does give the tab focus.
Also giving a return true or adding event.preventDefault() or event.stopImmediatePropagation() before doe not work.
However, if I move the test code to the top of the code block, it does work.
Is Anyone aware of a change in Chromium, or a constraint that will open the new page in the background?

Yes, it will automatically open a new page in the background.
Unfortunately, there is no way around this.
Sorry :(

My Chrome is 107.0.5304.107.
Doing this will create a new tab and give it focus.
<html>
<body>
<script>
let url = "https://google.com";
window.open(url, "_blank");
</script>
</body>
</html>
Please post your html and JavaScript.

Related

Window.open issue in firefox and IE

I am opening a new window on clicking a hyper link.
Issue:
After minimizing the window, again if I click on hyper link, the same window should be opened(In chrome minimized window will open up). But this is not happening in firefox and IE. Can anyone please help.
<html>
<head></head>
<body>
<p>Visit our HTML tutorial</p>
</body>
</html>
window.open allows you to specify a unique identifier to your popup; this allows you to open many links always in the same popup window.
If you use different identifiers on different links, it should open multiple popup windows.
<p>
Visit our HTML tutorial
</p>
<p>
Visit our HTML tutorial
</p>
If the strWindowFeatures parameter is used and no size features are defined, then the new window dimensions will be the same as the dimensions of the most recently rendered window.
you might want to check this link
window.open web api for mozilla
The idea of Unique ID in the parameter's list simply doesn't work as suggested in another answer.
You need a function for to do what you need in IE and FF. The trick is to get a function to see if it has opened a window before and do nothing if it has.
<script>
var opened = false;
function openWindow(){
if (!opened) {
w = window.open('', 'test', 'width=1500, height=900');
w.location = "http://www.google.com";;
w.onload = function() {
w.onunload = function() {
opened = false;
};
};
opened = true;
}
}
</script>
I'm using the opened global variable to track this. We set the newly created window to set false to this variable when it closes. Now the function can decide if it should really open a new window. Please note the following points:
We use onLoad function of the new window to set onUnload. Because IE seems to replace whatever the event handlers set here soon after it loads the page.
You can see that we first open a blank window and then set the url of it. This is because IE returns nothing when opening a new window if it is from another domain.

Opening a popup in background

I need to open an url in a pop up window, in the background. Currently in my code, the new windows pops up on top of the current window, without going in the background. I am running my code on firefox 23.0.1-fedora 18.
I have tried using .blur() and .focus() js methods, but they are not working.
Is there a clean and reliable solution for this, which will work for firefox and chrome?
http://jsfiddle.net/6La9W/1/
html
<button >click me</button>
js
$("button").click(function(){
var sOptions = 'target=_blank,toolbar=no,scrollbars=yes,location=yes,statusbar=yes,menubar=no,resizable=1';
var popup = window.open('http://www.stackoverflow.com','',sOptions);
popup.blur();
});
Browsers don't allow this nowadays because peoples open illegal things in behind popup, and you shouldn't do this. but still you can do this and it is worked in IE only.
$("button").click(function(){
var sOptions = 'target=_blank,toolbar=no,scrollbars=yes,location=yes,statusbar=yes,menubar=no,resizable=1';
var popup = window.open('http://www.stackoverflow.com','',sOptions);
window.focus();
});

Force window.open() to create new tab in chrome

I use window.open to populate a new window with varying content. Mostly reports and stored HTML from automated processes.
I have noticed some very inconsistent behavior with Chrome with respect to window.open().
Some of my calls will create a new tab (preferred behavior) and some cause popups.
var w = window.open('','_new');
w.document.write(page_content);
page_content is just regular HTML from AJAX calls. Reports contain some information in the header like title, favicon, and some style sheets.
In IE9 the code does cause a new tab instead of a pop-up, while Chrome flatly refuses to show the content in question in a new tab. Since the content is sensitive business data I cannot post it here. I'll answer questions if I can.
I know some people will say this is behavior left up to the user, but this is an internal business platform. We don't have time to train all the users on how to manage popups, and we just need it to be in a new tab. Heck, even a new window would be preferable to the popup since you cannot dock a popup in Chrome. Not to mention none of the popup blocking code would affect it.
Appreciate any insight.
window.open must be called within a callback which is triggered by a user action (example onclick) for the page to open in a new tab instead of a window.
Example:
$("a.runReport").click(function(evt) {
// open a popup within the click handler
// this should open in a new tab
var popup = window.open("about:blank", "myPopup");
//do some ajax calls
$.get("/run/the/report", function(result) {
// now write to the popup
popup.document.write(result.page_content);
// or change the location
// popup.location = 'someOtherPage.html';
});
});
You can try this:
open a new tab please
<script>
function openWindow(){
var w = window.open("about:blank");
w.document.write("heheh new page opened");
}
</script>
Is very easy, in order to force Chrome to open in a new tab, use the onmouseup event
onmouseup="switchMenu(event);"
I have tried this and it worked fine in chrome. If opening a new tab is on a user action(such as a mouse click) this will work fine without any issues. But if the code is executed in another script block, you may need to enable pop-ups in chrome. Looks like this is to handle all the click-bait sites.
let newTab = window.open('', '_blank');
if (this.viewerTab == null) {
console.log("opening in new tab to work, pop-ups should be enabled.");
return;
}
................
newTab.location.href = viewerUrl;
window.open('page.html', '_newtab');
Specify the '_newtab'. This works in IE and FF, and should work in Chrome. But if the user has things configured to not open in new tab then not much you can do.

Javascript function isn't running

Have been trying to fix this for the past hour.
Here goes:
<script type="text/javascript">
function openAd(adType, urlToGo) {
pageTracker._trackPageview(adType);
window.open(urlToGo, '_blank');
return false;
}
</script>
Anyone have any idea why this doesn't work?
Called it like onclick="openAd('/Ads/MMA_Front_Page.com', 'http://www.anrdoezrs.net/click-4706163-10919130');"
I uploaded a live version at:
http://www.easymuaythai.com/
I have went to your page in Opera 11.60 and called openAd('/Ads/MMA_Front_Page.com', 'http://www.anrdoezrs.net/click-4706163-10919130'); through Dragonfly and the information was displayed in my browser that the popup has been blocked. When I click to display the popup anyway, the new windows with your add target is displayed correctly.
Thus, I would suggest to seek different method to open the ad or accept the fact you may be blocked by popup / ad blocker.
if your problem is that the browser loads the url in the href as well as opens a new window then you need to add return false as so:
onclick="openAd('/Ads/MMA_Front_Page.com', 'http://www.anrdoezrs.net/click-4706163-10919130'); return false;"

window opener focus, or active

i want to open a window in a new tab, but when i want that opener page to be active, not the new one. How can i do this... Many thanks
my code is something like this :
<script language="javascript">
window.open("http://www.google.ro");
window.opener.location.focus();
</script>
To give focus to the new window (but you don't want that, and it will probably have focus by default):
var newWindow = window.open("http://www.google.ro", '_blank');
newWindow.focus();
I don't think it's possible to steal the focus from the new opened tabs. I didn't find any official statement telling this, but all the articles I found on this subject talk about configuring your browser to open tabs by default without focus.
The only "solution" I could come up with is this one:
window.open("http://google.com", "_blank");
window.alert('Hello there! This is a message that annoys you, the user.');
Note that it is possible to shift focus when opening popups.
Learn more about the window object.
This works for me...
var newWindow = window.open('http://www.google.ro', '_blank', 'height=300,width=300,menubar=0,status=0,toolbar=0', false);
window.focus();
To give back focus to the opener window, try using window.focus(window.name); instead of window.opener.location.focus();

Categories