Necesito guardar un archivo html desde el método window.open, document.execCommand('SaveAs', null, 'abc.html'); resuelve el propósito en Internet Explorer pero el mismo script no funciona en Microsoft-Edge (chromium).
He usado Window.showSaveFilePicker() para guardar los archivos en Chrome, pero puedo pasar una cadena codificada. Necesito obtener los datos de Window.Open HTML file y guardarlo usando microsoft-edge.
Necesita conocer un código común que funcione tanto en Internet Explorer como en Microsoft-Edge (chromium)
Gracias
//Índice.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="Index.js"></script> </head> <body> <button type="button" onclick="onSaveInIE()">In IE</button> <button type="button" onclick="onSaveInChrome()">In Chrome</button> </body> </html>//Índice.js
function onSaveInIE() { var csvWindow = openNewWindowObj(); csvWindow.document.execCommand('SaveAs', null, 'abc.html'); csvWindow.close(); } function openNewWindowObj() { var newWindowObj = window.open("window.html", "New popup Window", 'height=200,width=150') newWindowObj.focus(); return newWindowObj; } async function onSaveInChrome() { let fileHandle = await window.showSaveFilePicker(); const writeable = await fileHandle.createWritable(); await writeable.write("DummyData"); writeable.close(); }//ventana.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <b>From popup window</b> </body> </html>Creo que puedes intentar usar diferentes métodos en diferentes navegadores. Consulte este caso , determine el navegador utilizado.
Además, si necesita guardar el contenido de la página abierta por window.open() en Edge, puede intentar obtener su identificador después de cargar la página y luego obtener su elemento DOM.
Una demostración sencilla:
function onSaveInIE() { var csvWindow = openNewWindowObj(); csvWindow.document.execCommand('SaveAs', null, 'filename.html'); csvWindow.close(); } function openNewWindowObj() { var newWindowObj = window.open("window.html", "New popup Window", 'height=200,width=150') newWindowObj.focus(); return newWindowObj; } function onSaveInChrome() { var csvWindow = openNewWindowObj(); csvWindow.onload = function () { popupHtml = csvWindow.document.documentElement.outerHTML; download('filename.html', popupHtml); csvWindow.close(); } } function download(filename, text) { var element = document.createElement('a'); element.setAttribute('href', 'data:text/html;charset=utf-8,' + encodeURIComponent(text)); element.setAttribute('download', filename); element.style.display = 'none'; document.body.appendChild(element); element.click(); document.body.removeChild(element); }