Window.open() not working on google chrome - javascript

Good day.
In my vuejs application i am trying to open a new tab trough window.open() but whenever i do that the a new tab opens and immediately closes before loading anything at all. Window.open() works just fine on Firefox and window.location.replace also works normally.
Why window.open() ain't working?
openWindow(info) {
window.open('http://10.100.100.100:9999/window?someInfo=' + info);
},
PS: I also tried the following code just to see what happens and it worked just fine.
openWindow(info) {
window.open("http://www.google.com");
},

Per https://developer.mozilla.org/en-US/docs/Web/API/Window/open
It appears you need the second param.
window.open(url, windowName, [windowFeatures]);
openWindow(info) {
window.open('http://10.100.100.100:9999/window?someInfo=' + info, '_blank');
},

I was lodged here on search.
I faced such issue only on mobile chrome.
The two param didn't work for me. So i used 3 params.
openWindow(info) {
window.open('http://10.100.100.100:9999/window?someInfo=' + info, '_blank', 'popup=1');
}
or
openWindow(info) {
window.open('http://10.100.100.100:9999/window?someInfo=' + info, '_blank', '');
}

Related

maps.apple.com search does not open apple maps [duplicate]

I've build a web page that let's you select a page name from a drop down list and then transfers the browser to that page. The code that does the transfer is
if (url){
window.open(url, '_blank');
}
where "url" is the page selected.
A console log just before the window.open line prints something like:
executing: window.open(http://www.mywebsite.com/44/threats.html, '_blank')
and then the browsers opens the page in a new tab.
This works fine on Windows 7 for all the browsers, including Safari.
On an iMac it works for Firefox but not for Safari.
Does anyone know why iMac/Safari won't do this?
Safari is blocking any call to window.open() which is made inside an async call.
The solution that I found to this problem is to call window.open before making an asnyc call and set the location when the promise resolves.
var windowReference = window.open();
myService.getUrl().then(function(url) {
windowReference.location = url;
});
Using setTimeout
Wrapping your window.open(url, '_blank') line of code in the async function with a setTimeout works as well,
setTimeout(() => {
window.open(url, '_blank');
})
setTimeout code runs on the main thread, instead of the asynchronous one.
Tested in Chrome and Safari.
To use window.open() in safari you must put it in an element's onclick event attribute.
For example:
<button class='btn' onclick='window.open("https://www.google.com", "_blank");'>Open Google search</button>
You can't rely on window.open because browsers may have different policies. I had the same issue and I used the code below instead.
let a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
a.href = <your_url>;
a.download = <your_fileName>;
a.click();
document.body.removeChild(a);
Taken from the accepted answers comment by Steve on Dec 20, 2013:
Actually, there's a very easy way to do it: just click off "Block popup windows" in the iMac/Safari browser and it does what I want.
To clarify, when running Safari on Mac OS X El Capitan:
Safari -> Preferences
Security -> Uncheck 'Block pop-up windows'
window.location.assign(url) this fixs the window.open(url) issue in ios devices
Open link in a new tab, programatically with JavaScript for: safari, mobile safari and other browsers:
const link = 'https://google.com';
const a = document.createElement("a");
a.setAttribute('href', link);
a.setAttribute('target', '_blank');
a.click();
There's a setting in Safari under "Tabs" that labeled Open pages in tabs instead of windows: with a drop down with a few options. I'm thinking yours may be set to Always. Bottom line is you can't rely on a browser opening a new window.
This should work: window.location.assign(url);
Usually it is important to save the state, before leaving the page, so have this in mind as well.
The correct syntax is window.open(URL,WindowTitle,'_blank') All the arguments in the open must be strings. They are not mandatory, and window can be dropped. So just newWin=open() works as well, if you plan to populate newWin.document by yourself.
BUT you MUST use all the three arguments, and the third one set to '_blank' for opening a new true window and not a tab.

Javascript opens duplicate tabs in Firefox

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?

Page redirection not working in firefox using window.location but working in chrome

I wrote the following code to redirect the user to different pages on click. While the following code works fine in chrome, but it does not work in IE or firefox.
However, when i open the same buttons in new tab, they work just fine. But with single left click they do not work on any browser other than chrome.
I tried variations like, window.location='url' and window.location.assign(url) and window.location.href="url" but to no avail.
Please if someone can help me.
if (this.href == "http://www.successlabs.pk/download.php") {
window.location.href= "http://www.successlabs.pk/download.php";}
else if (this.href == "http://www.successlabs.pk/ContactUs.html") {
window.location.href= "http://www.successlabs.pk/ContactUs.html";}
Thanks in advance.
this question is over a year old, but this is what worked for me:
function gotoPage(_link) {
var _link = 'http://www.successlabs.pk/download.php';
window.location(_link);
return false;
}
It's possible that adding the 'return false;' line is what will make it work for you. This SO page explains why.
From the horse's mouth: https://developer.mozilla.org/en-US/docs/Web/API/Window/location
Example #1: Navigate to a new page
Whenever a new value is assigned to the location object, a document
will be loaded using the URL as if location.assign() had been called
with the modified URL. Note that security settings, like CORS, may
prevent this to effectively happen.
location.assign("http://www.mozilla.org"); // or location =
"http://www.mozilla.org";
I have never used location.href before only the two examples given above - and they work in all browsers

How to open a mailto: link from a Chrome Extension?

I have a URL shortening Chrome extension called Shrtr. Right now, it allows users to copy the shortened URL to clipboard, but in the next version, I've added the ability to email the shortened URL, using a mailto: link (i.e. mailto:?subject=<original page title>&body=<short URL>).
The problem is, you cannot just assign document.location.href = 'mailto...'; from an extension. The following 2 methods worked for me, but with both, I end up with an open blank tab in the browser:
Method 1: window.open
var wnd = window.open(emailUrl);
setTimeOut(function() {
wnd.close();
}, 500);
Notice the need to wait before closing the window. This works (i.e. mail client new message dialog appears, pre-populated), but the new tab remains open.
Method 2: using chrome.tabs
chrome.tabs.create({ url: emailUrl }, function(tab) {
setTimeOut(function() {
chrome.tabs.remove(tab.id);
}, 500);
});
Again, works - but tab remains open. Any ideas?
var emailUrl = "mailto:blah#blah.com";
chrome.tabs.update({
url: emailUrl
});
I realize this is an old question, but I myself had the same issue, and I figured out how to solve the problem so I thought I would share.
The issue stems from the fact that (I believe) you are calling the below code from your extension popup page.
chrome.tabs.create({ url: emailUrl }, function(tab) {
setTimeout(function() {
chrome.tabs.remove(tab.id);
}, 500);
});
The problem with this is that as soon as a new tab is created, the popup page dies and the callback code is never executed.
We can remedy this by moving that code into a function within the background page whose lifetime is not tied to the popup page:
function sendEmail() {
var emailUrl = "mailto:blah#blah.com";
chrome.tabs.create({ url: emailUrl }, function(tab) {
setTimeout(function() {
chrome.tabs.remove(tab.id);
}, 500);
});
}
and calling it via chrome.extension.getBackgroundPage().sendEmail() from your popup page.
Using the above method, the default email client will be opened, and the new tab will be automatically closed after 500 milliseconds.
wouldnt u need a close statement like this
close
credits go to Daniel How to close current tab in a browser window?
or even something like this
//keep a handle to the window you open.
var newWin = window.open('my window', 'http://.../');
...
//some point later in the code
newWin.close();
credits go to Tracker1 Javascript Code to close tab that works in IE, Firefox and Chrome?
I have solved the problem. Please check if it fits your solution space.
I have used the following code to open a mailto client in chrome extension.
$('.email').click(function(){
var hrefString = $(this).attr('href');
var myWindow = window.open(hrefString, "Opening mail client", "width=200, height=100");
myWindow.document.write("<p>Opening mail client.Please wait!!</p>");
setTimeout(function(){ myWindow.close() }, 2000);
});
where the element was :
<a style="padding: 0 0 0 5px;" href="mailto:abc#xyz.com" class="email">
This worked for me.
Hope it will help others.
Regards,
Karan Ratra

window.open(url, '_blank'); not working on iMac/Safari

I've build a web page that let's you select a page name from a drop down list and then transfers the browser to that page. The code that does the transfer is
if (url){
window.open(url, '_blank');
}
where "url" is the page selected.
A console log just before the window.open line prints something like:
executing: window.open(http://www.mywebsite.com/44/threats.html, '_blank')
and then the browsers opens the page in a new tab.
This works fine on Windows 7 for all the browsers, including Safari.
On an iMac it works for Firefox but not for Safari.
Does anyone know why iMac/Safari won't do this?
Safari is blocking any call to window.open() which is made inside an async call.
The solution that I found to this problem is to call window.open before making an asnyc call and set the location when the promise resolves.
var windowReference = window.open();
myService.getUrl().then(function(url) {
windowReference.location = url;
});
Using setTimeout
Wrapping your window.open(url, '_blank') line of code in the async function with a setTimeout works as well,
setTimeout(() => {
window.open(url, '_blank');
})
setTimeout code runs on the main thread, instead of the asynchronous one.
Tested in Chrome and Safari.
To use window.open() in safari you must put it in an element's onclick event attribute.
For example:
<button class='btn' onclick='window.open("https://www.google.com", "_blank");'>Open Google search</button>
You can't rely on window.open because browsers may have different policies. I had the same issue and I used the code below instead.
let a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
a.href = <your_url>;
a.download = <your_fileName>;
a.click();
document.body.removeChild(a);
Taken from the accepted answers comment by Steve on Dec 20, 2013:
Actually, there's a very easy way to do it: just click off "Block popup windows" in the iMac/Safari browser and it does what I want.
To clarify, when running Safari on Mac OS X El Capitan:
Safari -> Preferences
Security -> Uncheck 'Block pop-up windows'
window.location.assign(url) this fixs the window.open(url) issue in ios devices
Open link in a new tab, programatically with JavaScript for: safari, mobile safari and other browsers:
const link = 'https://google.com';
const a = document.createElement("a");
a.setAttribute('href', link);
a.setAttribute('target', '_blank');
a.click();
There's a setting in Safari under "Tabs" that labeled Open pages in tabs instead of windows: with a drop down with a few options. I'm thinking yours may be set to Always. Bottom line is you can't rely on a browser opening a new window.
This should work: window.location.assign(url);
Usually it is important to save the state, before leaving the page, so have this in mind as well.
The correct syntax is window.open(URL,WindowTitle,'_blank') All the arguments in the open must be strings. They are not mandatory, and window can be dropped. So just newWin=open() works as well, if you plan to populate newWin.document by yourself.
BUT you MUST use all the three arguments, and the third one set to '_blank' for opening a new true window and not a tab.

Categories