Javascript 如何在访问打开的窗口时避免“操作不安全”错误

Javascript 如何在访问打开的窗口时避免“操作不安全”错误,javascript,dom,Javascript,Dom,我正在尝试打开一个新窗口,并用数据填充它,以便在剪贴板中复制、打印或保存到硬盘。我是通过Greasemonkey用户脚本来实现的 脚本的简化版本: window.addEventListener("load", function() { /*EXPORT DATA*/ //Create a button var button = document.createElement("input"); //call the actual export function butto

我正在尝试打开一个新窗口,并用数据填充它,以便在剪贴板中复制、打印或保存到硬盘。我是通过Greasemonkey用户脚本来实现的

脚本的简化版本:

window.addEventListener("load", function() {

  /*EXPORT DATA*/
  //Create a button
  var button = document.createElement("input");
  //call the actual export function 
  button.onclick = function() {window.exportujRozvrh();};
  //I also tried this:
    //button.onclick = (function() {window.exportujRozvrh();}).bind(window);

  button.value = "Print data in new tab";
  button.type = "button"; 
  //Append button to a node
  getElementByXpath(tisk_path).appendChild(button);
});


   window.exportujRozvrh = function() {
      //create a new tab (or window if tab not supported)
      var okno = window.open(); //No url should be fine for same origin policy, shouldn't it?
      //check if popup exists
      if(okno==null)
        alert("Popup blocked.");
      //Write the data
      okno.document.open();
      okno.document.write("data"); 
      okno.document.close();
   }
如果从按钮运行,此函数将引发以下错误:

SecurityError: The operation is insecure.
但是,如果通过键入javascript从调试控制台或URL栏启动函数:window.exportujRozvrh;void01脚本工作正常。 所以我猜这是这个函数在事件中启动的上下文。这就是我尝试使用.bindwindow的原因


1调用此抛出窗口未定义错误。我已经习惯了窗口总是被定义的事实,所以我放弃了这一点。

是否需要将函数附加到窗口?是的,因为greasemonkey脚本中定义的变量不是全局变量。