Open new browser windows in JavaScript without making it active - javascript

I have some JavaScript that makes an AJAX call and, if the call fails, opens a new windows (tab in Firefox) and displays the response from the server in that window. This is very convenient for debugging, because the error is typically from Pylons, so it's a full HTML page.
The only problem is that the new tab becomes the active tab, which would totally confuse a regular user. Is there any way to open the tab/window, but not make it active, ie. keep the current active window?
My code currently looks like this:
errorWindow = window.open("", "TCerrorWindow")
if (errorWindow)
errorWindow.document.write(xhr.responseText);

You can call errorWindow.blur(); window.focus(); after, forcing the browser to return focus to the previous window.
The effect you're trying to achieve is commonly called a pop-under window.

AFAIK this is not possible, as a security measure against pop-under windows. For debugging purposes you could
use Firebug (with a handy console, where you can output your own log messages from the code)
create a debug layer (div) on your page, where you output error messages in case an error happens

Related

How can I stop Chrome blocking me setting a window.location after the user has changed the URL?

I open a new window to point at a page in a manual with var manual_win = window.open("", "manual");
I then set the location (after a bit of async code that tests the existence of the page) with manual_win.location = url;
This works and opens the manual page in a new named tab. I can later run the same code with a different URL and it works fine.
If the user edits the URL manually on the named tab to be some other site such as google.co.uk then the code no longer works on Chrome 76. It just brings the tab to the foreground and doesn't change the location. There is no issue with Firefox. I presume this is due to some sort of popup blocking logic in Chrome.
Is there any way to get around this?
It seems like a bug in Chrome that it blocks me from changing the location yet still brings it to the foreground. It would have made more sense for it to have cleared the name from the tab when it took control way from me so I end up opening a new tab instead.

Selenium Webdriver unable to give JavaScript popup window handle

I have one web page page where there is an button which when get clicked, popup an window form, where i have to select some values. I am currently using IE browser and selenium 2.53 libraries to automate this flow. Now my problem is that i am unable to switch to this popup window since i am not getting any window handle and every time it is showing only one parent handle.
I have tried alert/popup switch also but still this popup as invoked via a button from the parent page are not recognizable and so unable to do any action in this pop up page.
Set<String> winhandle= driver.getWindowHandles();
System.out.println(winhandle);
for (String handle : driver.getWindowHandles())
{
System.out.println(handle);
String newURL = driver.getTitle();
System.out.println(newURL);
}
Regards,
Nir
In your code you never actually iterate over the other windows.
In order to correctly print the handles of all the windows (including the popup), try to modify your code:
Set<String> winhandle = driver.getWindowHandles();
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle); // This line was missing. It tells the driver to operate on a different window
System.out.println(handle);
}
And then - after testing - you can just break the loop whenever you're in the right window.
Does this work for you?
Its strange but now true that same code using the window handles and switchTO is working fine with the chrome browser and it was not working for the IE browser. I don't know how to fix it in the IE browser but working great in the chrome browser.
regards,
Nir

Google Chrome closes when I open a new tab using my browser action Javascript, no idea why

I'm in the process of writing a Google Chrome extension, and I'm hung up on what should be a very simple task. I have a browser action that opens a popup, "popup.html", which then loads a small Javascript file, "popup.js". As soon as I get the DOMContentLoaded signal from the popup page, my Javascript opens a new tab with a certain URL. This behavioral quirk is by design - in some cases it will display a menu, and in other cases it will just open this URL.
I currently have some very simple code which is supposed to do this, but whenever it opens this tab, Chrome suddenly closes. I'm using a Mac (OS X Mavericks), so it doesn't crash entirely (still running), the window just closes. When I open it up again, the tab it was supposed to open is there, but the "popup.html" menu is awkwardly hanging over the UI still and won't go away.
Here is my complete popup.js file:
function stuff() {
window.open("http://stackoverflow.com/");
}
document.addEventListener('DOMContentLoaded', stuff);
I'm guessing that I may need to gracefully close my popup window before going to this link or something, but that seems like I'm overthinking it. After all, I've seen other extensions that have normal links in their browser action popups, and you can click those to open new tabs without any Javascript magic.
I've also tried using the chrome.tabs.create function, yet the same thing happens. The Chrome developer console doesn't show any errors. Thoughts?
Edit1: Just disabled all extensions and tried again. Still breaking. Going to boot up my Windows 8 box and see what happens on it.
Edit2: Works with a short delay before opening the window now on Mac, see my answer below.
Ok, I think I may have figured this out, but it's still weird. I did this on my Windows 8 PC, and the browser didn't crash, but the browser action popup didn't close the way it's supposed to either.
On a hunch, I altered my code on the Mac to give the browser some time to render the popup window:
function stuff() {
window.open("http://stackoverflow.com/");
}
setTimeout(stuff, 500);
Worked without a problem on the Mac this time. I'm guessing it's an issue with the browser being interrupted while rendering the popup window, but it's still weird. I would still appreciate an explanation from someone who actually knows what's going on here! :-)

javascript : window.open

I am working in vb.net 2005. I am in a position to start a new browser with process.start().
Now I have to open that browser in a specific size(say height:300 width:500)
Process.Start("firefox.exe", "abc.html")
and I have written this following code on load of abc.html
var myRef = window.open('abc.html','','left=20,top=20,width=300,height=500');
but it does not resize.
If I add 1 button on this page and click on it (by writing same code on its click event), a new window with expected size opens.
Am I going wrong somewhere?
Thanx.
Firefox doesn't let pages resize the window by default. Also note, if you already have Firefox running then browser preferences will dictate whether you get a new window or a tab. You can force a separate instance of Firefox by using the -no-remote command line flag, but then you won't be able to use the default profile (only one Firefox instance per profile).
My questions for you are:
Why are you launching Firefox from another executable at all instead of just having users click on a link and have it open in their default browser?
If you do need to launch Firefox from an executable, why spend all this effort overriding the user's preferences and settings?
If you' re launching from an executable and are keen to annoy your users whatever the cost, why not just find and resize the Firefox window using the normal Windows APIs?

Is there a cross-browser method to bring a popup window into the foreground?

I have a chat in a popup browser window. This chat page (ASP) checks for new messages every 10 seconds.
I would like to bring this popup window to front when there is a new message. I tried with "window.focus()", but this work only in few version of IE.
Are there other options to achieve my goal?
Don't steal the focus.
And no, it will work only in some IEs, as you said. Quoting Mozilla's developer network,
It may fail due to user settings and
the window isn't guaranteed to be
frontmost before this method returns.
This is valid for all modern browsers, I guess.
You can use the following approach in order to notify the user of an activity.
Yes, there is a way out. You can use the JavascriptExecutor class to get the hidden browser and switch to it. This will bring the browser to the foreground.
String window = driver.getWindowHandle();
((JavascriptExecutor) driver).executeScript("alert('Test')");
driver.switchTo().alert().accept();
driver.switchTo().window(window);

Categories