过去这个曾经有效:
然而,现在当我尝试执行这段代码时,Firefox会提示一个安全错误:
错误:SecurityError: The operation is insecure.
我在论坛上搜索了一种替代方法,这个方法可以解决问题:
// writing
var newTab = window.open();
newTab.document.write("<html><head><title>I'm just a tab</title></head>");
newTab.document.write("<body id='hello'>With some text on it</body>");
newTab.document.write("</html>");
newTab.document.close();
// reading what was wrote
newTab.document.getElementById('hello').addEventListener("click", custom_search_function(), false);
然而,现在当我尝试执行这段代码时,Firefox会提示一个安全错误:
错误:SecurityError: The operation is insecure.
我在论坛上搜索了一种替代方法,这个方法可以解决问题:
var textOnPage = "<html><head><title>I'm just a tab</title></head><body>";
var newTab = window.open("data:text/html;charset=UTF-8," + encodeURIComponent(textOnPage));
newTab.document.close();
但是我无法通过getElementById
访问该页面
newTab.document.getElementById('hello').addEventListener("click", custom_search_function(), false);
返回:
错误:类型错误:newTab.document.getElementById(...) 为 null
如何在新标签页中编写内容,并通过 getElementById
等函数返回并读取它?
postMessage
API。 - David-SkyMesh