Javascript opens duplicate tabs in Firefox - javascript

I have the following link that when clicked, opens a new tab and refreshes the page with the link in.
It works fine in Safari and Chrome but opens duplicate tabs in Firefox.
Run Letters
function openWindowReload(link) {
var href = link.href;
window.open(href,'_blank');
location.href = 'index.php' + "?welcome_letters=export&welcome_letters=export"
}
Any ideas why Firefox is doing this and how to resolve it?

I think it's because you don't stop the default behavior.
Could you call preventDefault on the click event?

Related

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

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.

how to set focus on new window or tab in Firefox and IE Browsers? [duplicate]

I have a link which should open in a new tab, but if the tab is already open, just switch to it.
I've tried with javascript, wnd = window.open() and than wnd.focus(), that works in Chrome 19, but not in FF 13 or IE 9.
Here's the code I've written :
<script type="text/javascript">
var loadingTableWnd;
function openOrSwitchToWindow(url){
if(loadingTableWnd == undefined)
loadingTableWnd = window.open(url,'myFrame');
else
loadingTableWnd.focus();
}
</script>
<a href='javascript:openOrSwitchToWindow("/");' >Loading Table</a>
Any idea how can I open or switch to from every browser?
EDIT: I need to open the link in a new tab, not a stand-alone window.
Different browsers behave differently for window.open() and focus().
For this code window.open('www.sample.com','mywindow').focus()
Chrome 20 opens a new tab, and focuses on subsequent open() calls regardless if focus() is called or not.
Firefox 13 opens a new tab, focuses on first open(), does not focus on subsequent open() calls/disregards focus().
IE 8 opens a new window, honors focus().
Safari 5 opens a new window, and focuses on subsequent open() calls regardless if focus() is called or not.
Fiddle to test with: http://jsfiddle.net/jaraics/pEG3j/
You shouldn't need any logic for something like this. By default, specifying the second parameter for window.open() gives the window a "name", that the browser remembers. If you try to call window.open() with the same name (after it's already been opened), it doesn't open a new window...but you might still need to call .focus() on it. Try this:
var a = window.open(url, "name");
a.focus();
Those should be the only lines of code in your function, and you don't need the loadingTableWnd variable...
If the window is already opened and if you want to focus on that window you can use
window.open('', 'NameOfTheOpenedWindow').focus();
If you are not interested in retaining the state of a previously opened tab, you can do this:
var loadingTableWnd;
function openOrSwitchToWindow(url) {
if (loadingTableWnd != undefined) {
loadingTableWnd.close();
}
loadingTableWnd = window.open(url,'myFrame');
}
window.focus() is widely supported and seems to be working fine in both Internet Explorer and Firefox for me, the problem should be in your code. I've created a simple jsFiddle for you to test.

Mobile safari: From fullscreen page, how to open a page in a normal safari window?

The title has it all: My page runs in full screen mode (cf. using an icon on the springboard); When I click a link to an external page, I would like to have it open in the normal safari application, not the full screen instance.
How can this be done?
I expected window.open('url', '_blank') to do the trick, but it doesnt.
Thanks!
Edit: code snippet:
$('#gotoWebsitePlaceholder').selectable({
start: function(){
window.open('http://www. ... .com', "_blank");
}
}).hide();
Are you using an anchor tag? My experience is that the default action is to open the href target in the Safari app. I.E., if I didn't cancel the default action via javascript, my app would shift out of the FS app. Can you post a code snippet?
I have discovered that Safari will remain in fullScreen when executing window.open(myURL) or this.location = myURL. However if your url is called using click it will exit fullScreen mode.
I solved with this piece of js (tested in ipad with iOS 5.1.1):
jQuery(document).ready(function ($) {
$('a').click(function() {
if (($(this).attr("target")!="_blank")){
window.location = $(this).attr('href');
return false;
}
});
So only the links with target="_blank" will open in safari.

Get consistent cross-browser behavior with fancy links

I'm developing for a site that does something a bit strange with some links.
When the user clicks on a link, an indicator GIF that looks kinda like this appears next to the link, and the link itself is disabled. It then navigates to the page. This is because of concerns that impatient users might repeatedly click the link if it doesn't load immediately, putting an extra load on our server.
Here's how we do this:
<a id="link1" href="/target_page"
onclick="var link = document.getElementById('link1');
var loc = link.href;
link.removeAttribute('href');
link.setAttribute('onclick', 'return false;');
document.getElementById('link1_img').style.display='';
window.location = loc;">
Link Text
<img id="link1_img" src="/images/indicator.gif" style="display:none;" />
</a>
Here's the problem. While this has the expected behavior if the user clicks on the link normally, or right-clicks it and opens it in a new window or tab from the context menu, it doesn't always work properly if the user middle-clicks or Ctrl+clicks on the link.
The desired behavior in that case would be to skip all the JavaScript stuff and simply open the link in a new tab. I did a quick test on Windows with the latest version of each major browser, and IE, Firefox, and Opera all do this. Chrome and Safari, however, display the indicator and open the link in the current tab.
Any suggestions on how to make it behave consistently on all browsers?
You can use the code here for handling middle clicks, and take a look here for the Ctrl+click. However, I found that I had to use .mousedown for middle clicks instead of clicks (at least in Firefox).
If you use
$('#link').click(function(e) {
if(e.ctrlKey && e.which == 1 || e.which == 2) { // Ctrl + Click
console.log("hi");
$(this).css('color','#aaa');
$(this).click(function(e) { e.preventDefault(); });
}
});
and don't add e.preventDefault(), it should work as expected. Works for me in Firefox, Chrome, and IE9.
Edit: Without jQuery, this is how I would do it. Do take a look at this post about preventDefault() vs return false;, though.
document.querySelector('#link').onclick = function(e) {
this.style.color = '#aaa';
disableLink(this);
}
// middle clicks only captured in mousedown
document.querySelector('#link').onmousedown = function(e) {
this.style.color = '#aaa';
if (e.button == 1) {
disableLink(this);
}
}
function disableLink(elem) {
elem.onclick = function(e) { console.log("HI"); this.href = '#'; return false;}
elem.onmousedown = function(e) { console.log("HI"); this.href = '#'; return false;}
}
Unfortunately, there seems to be a difference in the way Chrome and Firefox handle middle clicks. Firefox only shows middle clicks in onmousedown, and it opens a new tab during the onmousedown. This means that for Firefox, we have to disable the link after onmousedown. However, in Chrome, the middle click shows up in onmousedown and onclick, but the link is only opened after the onclick. Since we disable the link on the mousedown, the onclick never gets a chance to run :(.
Not sure how to fix this...

Javascript popup - Works in FF & Chrome, fails in IE

I can't seem to figure out why the following simple popup will not work in IE9. FF & Chrome popup as expected, but IE does not appear to do anything when the link is clicked. I tried the IE9 debugger, but didn't get any helpful information out of it.
In the head section:
<script type="text/javascript" language="JavaScript">
function JSPopup(linkref) {
window.open(linkref,"Report Definitions","width=600,height=480");
return false;
}
In the body:
<strong>Report Definitions</strong>
Thanks for your help,
Filip
Turns out the problem was the name given to the popup - IE doesn't allow spaces, FF & Chrome do:
window.open(linkref,"Report Definitions","width=600,height=480");
needed to be changed to:
window.open(linkref,"ReportDefinitions","width=600,height=480");
This works across browsers.
Filip
This is part of the security changes made in IE6. Now you can only call "window.open" from within a user-initiated event. For example, your code would work inside an element's onclick event. The "window.open" http://msdn.microsoft.com/en-us/library/ms536651(VS.85).aspx MSDN page says this:
"This method must use a user-initiated action, such as clicking on a link or tabbing to a link and pressing enter, to open a pop-up window. The Pop-up Blocker feature in Internet Explorer 6 blocks windows that are opened without being initiated by the user."
Example...
function popUpWin(url,wtitle,wprop){
if (!window.open){ return; } // checking if we can't do this basic function
// Kill if(win), since win was a local var this was a usless check
var win = window.open(url,wtitle,wprop);
// next line important in case you open multiple with the same 'wtitle'
// to make sure the window is reused and refocused.
if (win && win.focus){ win.focus(); }
return false;
}

Categories