Prevent window.open from focusing - javascript

I want to open a page in a new tab in Google Chrome with window.open(), but I don't want that window to gain focus after it's opened, but to stay in the background.
Is this possible? It only has to work on Google Chrome. It can also use the Google Chrome extension API.
Thanks

The proper way would be to use extension API:
chrome.tabs.create({url: "http://...", selected: false});
Code should be placed in a background page. If you need it inside a content script you can pass a message to a background page, like so:
//content script
chrome.runtime.sendMessage({link: link});
//background page
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if(message.link) {
chrome.tabs.create({url: message.link, selected: false});
}
});

window.open(url, name, features);
window.focus();
You will see the new window for a short moment though.

There is a way out in all the browser
function openURL(url, opt){
if (opt == 0){ // current window
window.location = url;
}else if (opt == 1){ // new window
window.open(url);
}else if (opt == 2){ // background window
window.open(url); self.focus();
}
}
so By using this you can do anything you want.
openURL( "http://www.google.com", 0 ) --> open in same window
openURL( "http://www.google.com", 1 ) --> open in new window
openURL( "http://www.google.com", 2 ) --> open in new window but in background.

Yes you can do that just use:
var myWindow = window.open(url,name,features);
myWindow.blur();

Activate parent window from child or activate self once child is opened.

Related

Move to tab but don't reload

I'm using the code below with window.open to open a new link (same origin) in a new tab. If a tab with the new link is already open, the open tab should be focused instead.
This is all working fine, the focused tab is however always refreshed when it gains focus by this. How can I stop the focused tab from refreshing?
window.open(`./${_id}`, `module_${_id}`);
Check if the window is closed orelse use focus(), this should not refresh the page.
var windowObject = null; // global variable
function openURL(url) {
if(windowObject == null || windowObject.closed) {
windowObject= window.open(url);
} else {
windowObject.focus();
}
}
<button value="Check" onClick="openURL('https://www.google.com')"></button>

How can i close an already opened window and then open a new window every time i click a button in angular?

I want to open a window using window.open(). but i have to make sure that if that same window has already opened previously , i have to first close that window then i have to open a new one .
try this code
<button onclick="openNewWindow()">Open New Window</button>
var popup_window;
function openNewWindow(){
if(popup_window != undefined){
popup_window.close();
}
popup_window = window.open("");
}
Try following code
let openedWindow;
let url = "www.example.com"
function openWindow() {
if(openedWindow) {
openedWindow.close();
}
openedWindow = window.open(url);
}
NOTE : The Window.close() method can only be called on windows that were opened by a script using the Window.open() method. If the window was not opened by a script, an error similar to this one appears in the console:
Scripts may not close windows that were not opened by script.
Check MDN web docs

how to open in new window

i have another problem :(
this is my script
<script type="text/JavaScript">
function first()
{
var first = confirm("Are you sure you want to navigate away from this page?");
if (first)
window.location = "http://www.yahoo.com.sg"
else
window.close;
}
function second()
{
var second = confirm("Are you sure you want to navigate away from this page?");
if (second)
window.location = "http://www.google.com"
else
window.close;
}
</script>
<p><b>Click the following link to enter yahoo</b></p>
<p>yahoo</p>
<p><b>Click the following link to check google</b></p>
<p>google</p>
its doesnt open in a new window.
please i need some help. any suggestion or recommendation?
Try window.open. This will open a new window:
window.open("http://www.google.com");
See also:
MDN: window.open usability issues
Use the window.open method and specify _blank as the target:
window.open('http://www.yahoo.com.sg', '_blank');
This will open the page in a new window or a new tab, depending on the user settings in the browser.
If you want it to open in a new popup window always, you can specify a parameter string with width and height for the window. However, most browsers will block such a popup that opens a page from a different domain.
If you are opening in a new window, you don't need to check with the user about navigating away.
function first()
{
window.open("http://www.yahoo.com.sg")
}
function second()
{
window.open"http://www.google.com")
}

Javascript, Chrome Extension development, close popup

In my manifest i have this:
"popup": "1_options.html"
and in the above html file I have this code
var saved_email = localStorage['saved_email'];
if (saved_email !== undefined || saved_email != "a#a.com")
{
chrome.tabs.create({url: '0_register.html'});
}
which is working exactly as I want, it opens a new tab with the register.html BUT it still has the popup open on the top right :( (1_options.html)
is there anyway to close the popup automatically as I open this new tab?
Thanks!
Ryan
Have you tried :
self.close();
There's several ways to do this, but the easiest is just to call:
window.close();
You can even do this in a callback function when you create your tab...
chrome.tabs.create({url: '0_register.html'}, function() {
window.close();
});
You could also add a listener in your background script to check for tab updates, and if your new tab is your registration window, you could remove the popup:
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if(changeInfo.status == "loading") {
if(tab.url == "chrome-extension://[extension-id]/0_register.html") {
chrome.tabs.remove(tabId);
}
}
});
chrome.tabs.create({url: '0_register.html', selected: true});
If you don't mind the new tab being selected when it is created, this also forces the popup to close.

Is it posible to use "confirmit" to open new url in the same window?

I'm using a the confirmit java script function to redirect to a new url with a positive response. It doesn't do exactly what I want - and maybe I can't have exactly what I want.
Here's what it does - onunload, with a positive response it opens the new url in a new window, which can be blocked by a pop-up blocker without the user even knowing, giving the impression that nothing happened. When it does open the new URL it opens in a new window making the desktop crowded. Its just not a great solution.
What I want it to do - onunload I'd like it to open the new url in the same window and have it not be classified as a "pop-up" to be blocked. I tried switching to onbeforeunload.... nuthin.
Is there a simple solution?
here's my current code:
<script language=javascript>
function confirmit()
{
var closeit= confirm("My message");
if (closeit == true)
{window.open("http://MyDestinationURL");}
else
{window.close();} } window.onunload = confirmit
</script>
Instead of opening a new window you can set the location of the current window:
<script type="text/javascript">
function confirmit() {
if (window.confirm("My message")) {
window.location.href = 'http://MyDestinationURL';
} else {
window.close();
}
}
window.onunload = confirmit;
</script>
I am not sure if you actually want the else case where it tries to close the current window. If you just want to stay on the current page on a negative response, remove the else part.

Categories