I want to open a tab inside an opened window. Something like this
(it should open a new window with "google.co.uk" with a new tab "google.de"
newWindow = window.open('http://www.google.co.uk', '', 'width=10');
newWindowTab = newWindow.open('http://www.google.de', '_blank');
but this opens "newWindowTab" only in the window, where this code is.
I have also tried this, to give the window time to load, until it (should) open the new tab:
newWindow = window.open('http://www.google.co.uk', '', 'width=10');
setTimeout(function() {
newWindowTab = newWindow.open('http://www.google.de', '_blank');
}, 500);
But then I get:
Error: Permission denied to access property "open"
I have used firefox. I heard that it might be possible to do in Chrome, but I want to use this script in Firefox.
This is not possible, unless the window being opened is from the same origin (ie the same domain). MDN says this:
The reference can be used to access properties and methods of the new
window provided it complies with Same origin policy security
requirements.
Related
I have a URL, which is working with JavaScript. When I am hitting that URL then it will open a new window popup(window.open()) and again when I am hitting the same URL from a new tab and on the same browser, It is opening a new window popup even previous popup window is already opened.
So, Is there any way to focus on an already opened popup window instead of opening a new popup window from the new tab on the same browser?
(Un)Fortunately, there seems to be no reliable way to bring a window or tab into focus (see here or here).
For your other issue (not opening a new popup if it is already open), you have some options based on cross window/tab communication (assuming same origin):
You can set a cookie in your popup and delete it once the popup is closed (or more specifically unloaded). When attempting to open a new popup, check whether the cookie is already set. This has some pitfalls like you not having control over how many times the popup is opened by the user (manually, without your guard).
You can implement a form of tab/window communication (summarized in this question).
Assuming you can modify the popup and they share the same origin, an implementation based on Broadcast Channels might look like this (just to give you an idea):
From the source:
const bc = new BroadcastChannel('channel_id');
let isPopupOpen = false;
bc.onmessage = (ev) => {
if (ev.data === 'popup-id-exists') {
isPopupOpen = true;
}
};
function openPopup() {
isPopupOpen = false;
bc.postMessage('popup-id');
setTimeout(() => { if (!isPopupOpen) window.open('popup.html') }, 100);
}
From the popup:
const bc = new BroadcastChannel('channel_id');
bc.onmessage = (ev) => {
if (ev.data === 'popup-id') {
bc.postMessage('popup-id-exists');
}
};
i want to open a new window using window.open(). but the issue is i don't want to show the URL to user, so i have to use POST URL for this. but window.open() function opens the new window and shows the URL in URL bar. i have to hide the URL.
here is the Example :
window.open(URL,"_blank",'status=0,toolbar=0,resizable=0,menubar=0,titlebar=0,width=1180,height=770');
No, you can't hide address bar in modern browser.
location=no paramter will not working.
See the MDN document.
https://developer.mozilla.org/en-US/docs/Web/API/Window/open
In Firefox 3, dom.disable_window_open_feature.location now defaults to
true, forcing the presence of the Location Bar much like in IE7.
location parameter is also always enabled (means location=yes) in Google Chrome.
But you can assign a fake URL:
var win = window.open('/path/page.htm');
win.addEventListener('DOMContentLoaded', function () {
win.history.replaceState(null, null, '/fake.htm'); // must be same domain (or ignore domain)
});
I need to open console and run one function on new tab, that I opened using javascrip. The opening part is easy, but how to run function on other tab?
var google = window.open("http://google.com")
Upon reading your question, it seems you're looking to open the dev console for the popup? Assuming this is what you're looking for, you should just be able to right-click the popped-up window and hit 'Inspect Element'. Then go to the console from there.
If you're trying to programatically run a function from the parent onto the popup window, here's an idea for you.
Assuming the new window is on the same domain as yours, this solution may work for you. (browser support is limited)
On the parent page:
//store the function in localStorage
localStorage.runThis = function(){ alert("Hello world"); }
//open the popup window
newWindow = window.open("http://your-domain.com/your-page");
On the page to open in the popup:
//check if the function has been stored
if(typeof localStorage.runThis === "function"){
//run the function in localStorage
localStorage.runThis();
}
One issue is that this method relies on this criteria being met:
Browser supports localStorage
Parent page and Popup page come from the same origin
Popup page must actually look for the function in question and execute the function itself
One drawback of this is that if someone were to go to the Javascript Console and set their own function into localStorage, the popup page would see their function and run potentially dangerous code - a security hole.
A common solution is using localstorage.
if (typeof(Storage) !== "undefined") {
// Code for localStorage/sessionStorage.
localStorage.setItem("lastname", "Smith");
var lastname = localStorage.getItem("lastname");
} else {
// Sorry! No Web Storage support..
}
I'm using an IFrame that contains a remote page on a different domain (that I have no control of), and it sometimes tries to pop a window using window.open.
Instead of popping the window - I want to dynamically add another IFrame to the page with the URL that the first IFrame was trying to open.
Is there a way to override the IFrame's window.open so I'll be able to 'catch' the URL it tries to pop?
I've tried:
var myFrame = document.getElementById("ifrm"); // 'ifrm' is the iframe's ID
myFrame.contentWindow.open = function (url, name, features) {
alert(url);
};
But it didn't work (no alert, and the iframe still tried to pop the new window)...
Any ideas?
Try:
function bindings(newDoc){
try
{
newDoc.contentWindow.window.open = function (url, name, features,
replace) {
alert(url);
};
}
catch( e )
{
console.log(e.message);
}
}
While calling the iFrame, use onload event:
<iframe onload="bindings(this)" ... />
It throws the error:
Permission denied to access property 'open' in Firefox
LOG: Permission denied in Internet Explorer
Apparently Dan F was right.. UA will deny access to the remote (cross-domain) document loaded via iframe.
I'm using Execute JS to write and test Javascript code within Firefox. I want to open a new tab/window and write something to it and I tried
var wm = Components.classes["#mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
var win = wm.getMostRecentWindow("navigator:browser");
printWindow = win.open("about:blank");
printWindow = wm.getMostRecentWindow("navigator:browser");
printWindow.gBrowser.selectedBrowser.contentDocument.write('hello');
And
myWindow=window.open('','','width=200,height=100')
myWindow.document.write("<p>This is 'myWindow'</p>")
myWindow.focus()
However I always get this error
[Exception... "The operation is insecure." code: "18" nsresult:
"0x80530012 (SecurityError)"
Is there any way to get through this exception?
Top-level navigation to data URLs has been blocked in Chrome, Firefox (with some exceptions), IE, and Edge (and likely other browsers to boot). They are apparently commonly used for phishing attacks, and major browser vendors decided that the danger outweighed the value provided by legitimate use cases.
This Mozilla security blog post explains that Firefox will block
Web page navigating to a new top-level data URL document using:
window.open("data:…");
window.location = "data:…"
clicking <a href="data:…"> (including ctrl+click, ‘open-link-in-*’, etc).
Web page redirecting to a new top-level data URL document using:
302 redirects to "data:…"
meta refresh to "data:…"
External applications (e.g., ThunderBird) opening a data URL in the browser
but will not block
User explicitly entering/pasting "data:…" into the address bar
Opening all plain text data files
Opening "data:image/*" in top-level window, unless it’s "data:image/svg+xml"
Opening "data:application/pdf" and "data:application/json"
Downloading a data: URL, e.g. ‘save-link-as’ of "data:…"
You can also read the proposal to deprecate and remove top-frame navigation to data URLs in Chrome and view the current Chrome status indicating that is has been removed.
As for how to actually open HTML in a new tab or window, this should be sufficient:
var tab = window.open('about:blank', '_blank');
tab.document.write(html); // where 'html' is a variable containing your HTML
tab.document.close(); // to finish loading the page
Note that at least in Chrome, external scripts injected via document.write might not be loaded on slower connections. That might not be relevant here, but something to watch out for.
Edit: As of 2018, this solution no longer works. So you are back to opening about:blank in a new window and adding content to it.
Don't "write" to the window, just open it with the contents you need:
var data = "<p>This is 'myWindow'</p>";
myWindow = window.open("data:text/html," + encodeURIComponent(data),
"_blank", "width=200,height=100");
myWindow.focus();
For reference: data URIs
var winPrint = window.open('', '', 'left=0,top=0,width=800,height=600,toolbar=0,scrollbars=0,status=0');
winPrint.document.write('<title>Print Report</title><br /><br />
Hellow World');
winPrint.document.close();
window.open(uri) does not work in chrome as of 2018