I have a servlet that generates a PDF and the output goes to a new browser window.
I am attempting to replace the title of that new window using the updateTitle() function below.
However, when I try to assign the report name (repName) to the window instance, IE11 throws a "Permission denied" error. Any ideas?
function showReport(url, repName){
var repWin = window.open(url);
updateTitle(repWin, repName)
}
function updateTitle(repWin, repName) {
setTimeout(function() {
repWin.document.title = repName; //IE11 console throws PERMISSION DENIED here
}, 3000);
}
You will need to use something like postMessage.
On your original window:
function showReport(url, repName) {
var repWin = window.open(url);
repWin.postMessage('setTitle:' + repName, '*');
}
On the repWin:
function updateTitle(message) {
var m = message.data.split(':'),
eventType = m[0],
data = m[1];
if (message.origin === 'YOUR_URL_HERE' && eventType === 'setTitle' ) {
repWin.document.title = data;
}
}
window.addEventListener("message", updateTitle, false);
Note: This will obviously only work if you can modify the source for the window you are opening.
Related
I am building a chrome extension which will get all the opened tabs and convert them to PNG file and download on the system.
What i have done till now
My code gets the URLs of all opened tabs and then gets the HTML DOM of each tab by using a for loop. Not i am using html2canvas to convert the html to png format, but i am getting the following error
Uncaught (in promise) Error: Document is not attached to a Window
at html2canvas.min.js:20
at html2canvas.min.js:20
at Object.next (html2canvas.min.js:20)
at html2canvas.min.js:20
at new Promise (<anonymous>)
at a (html2canvas.min.js:20)
at Vs (html2canvas.min.js:20)
at html2canvas.min.js:20
at getAllOpenWindows (popup.js:38)
My popup.js code is as follows:
// script for popup.html
window.onload = () => {
let btn = document.querySelector("#btnDL");
btn.innerHTML = "Download";
function display(){
// alert('Click button is pressed')
}
btn.addEventListener('click', display);
}
chrome.windows.getAll({populate:true}, getAllOpenWindows);
function getAllOpenWindows(winData) {
var tabs = [];
for (var i in winData) {
if (winData[i].focused === true) {
var winTabs = winData[i].tabs;
var totTabs = winTabs.length;
console.log("Number of opened tabs: "+ totTabs);
for (var j=0; j<totTabs;j++) {
tabs.push(winTabs[j].url);
tab_html_string = get_html_string(winTabs[j].url)
// get the HTML document of each tab
tab_document = get_html_document(tab_html_string)
console.log('======================')
console.log(tab_document)
html2canvas(tab_document.body, {
onrendered: function(canvas) {
var img = canvas.toDataURL("image/png")
chrome.windows.open(img);
}
});
console.log('======================')
}
}
}
console.log(tabs);
}
function get_html_document(tab_html_string){
/**
* Convert a template string into HTML DOM nodes
*/
var parser = new DOMParser();
var doc = parser.parseFromString(tab_html_string, 'text/html');
return doc;
}
function get_html_string(URL_string){
let xhr = new XMLHttpRequest();
xhr.open('GET', URL_string, false);
try {
xhr.send();
if (xhr.status != 200) {
alert(`Error ${xhr.status}: ${xhr.statusText}`);
} else {
return xhr.response
}
} catch(err) {
// instead of onerror
alert("Request failed");
}
}
In the below image you can see the opened 6 tabs in chrome with output of each tabs in the console as document reference, but the error above is irritating me. Please help me out.
output image
I'm trying to post a message using the postMessage function to a browser that I've opened using window.open and while I've found a couple of articles explaining on how to do it:
Window.postMessage()
HTML5’s window.postMessage API
I just can't get it to work.
In my grid, when double clicking on a row, I call the following code:
var win = window.open('#Url.Action("Index", "StandaloneViewer")', '_blank',
'width=600,height=800,scrollbars=yes');
var domainOrigin = document.location.origin;
var message = 'My Message';
win.postMessage(message, domainOrigin);
and in my Index.cshtml, I've got the following:
$(document).ready(function () {
window.addEventListener('message', function (event) {
debugger;
var domainOrigin = document.origin;
var domainPath = document.location.href;
if (event.origin !== domainOrigin) return;
...
}, false);
});
I've also tried the same code in the load event:
$(window).on("load", function () {
window.addEventListener('message', function (event) {
debugger;
var domainOrigin = document.origin;
var domainPath = document.location.href;
if (event.origin !== domainOrigin) return;
...
}, false);
});
but to no avail! Any ideas what I may be missing? I've got the same scenario working just fine when sending a message to an iframe but I have a use case where I need to launch a separate browser and send a message to it.
I'm currently testing this on the latest version of Chrome.
Thanks.
I want to check if the browser opens the popup window. In case the browser denies opening the window, I want to take some other action.
This is my example code:
function open_window(){
window.open("url",_blank);
}
if(!open_window(){
//action
}
If the popup is blocked by browser, window.open will return null. So this function will return false.
function firepopup(url,width,height) {
n=window.open(url,'_blank','toolbar=0,location=0,directories=0,status=1,menubar=0,titlebar=0,scrollbars=1,resizable=1,width='+width+',height='+height);
return n==null;
}
You can try something like this:
var winRef;
var url = 'http://someurl';
winRef = window.open('', 'winPop', 'sampleListOfOptions');
if(winRef == null || winRef.document.location.href != url)
{
winRef = window.open(url, 'winPop')
//Some Another Action
}
How can i get url of a popup opened by JS.
Here is my code:
var _url = 'someurlhere';
var popupwindow = window.open(_url, "Popup", 'width=800, height=600');
var _this = this;
var pollTimer = window.setInterval(function() {
try {
console.log(popupwindow.document.URL);
} catch(e) {
console.log(e.message);
}
}, 500);
but I'm getting a Cannot read property 'URL' of undefined error on line with console.log(e.message). Why ?
It won't work because you are using a url that is a different domain than the page you are running the script on. This is a violation of the "same origin policy". IOW it is XSS, not allowed.
Use popupwindow.location.href instead popupwindow.document.URL
Currently I'm using Javascript with Ajax to fetch some data and present it in a new window. I'm trying to close the window in OpenFileWindow() before a new one is opened, but while the it finds the window object, all properties and methods give a permission denied error.
I'm believe it has to do with scoping with the Ajax call as when I open the window before XMLHttpRequest, there's no problem.
I'm not sure how to proceed and I've searched quite a bit. Anyone have any suggestions? Thanks.
opened
var newWin = null;
function View_onClick(propId, url) {
var param = "propId=" + propId;
param += "&filename=" + url;
var xhr = new XMLHttpRequest();
xhr.open("POST", "GetActivityFileName.ashx", false);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
if (xhr.responseText == "") {
alert("Sorry unable to find file.");
return false;
}
else {
OpenFileWindow(xhr.responseText)
return false;
}
}
}
}
xhr.send(param);
return false;
}
function OpenFileWindow(fileUrl) {
if(newWin != null)
newWin.close();
newWin = window.open(fileUrl);
newWin.focus();
}
If your intention is to just re-use the window, why not name it.
If you give the same name as the 2nd parameter of window.open, it will re-use that window.
How about this. If the window is still open, change the URL. Otherwise, open the window and load the URL.
function OpenFileWindow(fileUrl) {
if(newWin == null || newWin.closed)
newWin = window.open(fileUrl);
else
newWin.location.replace(fileUrl);
newWin.focus();
}