无法在弹出窗口中触发脚本

时间:2018-05-27 17:17:55

标签: javascript

我将编辑此主帖以使用最新代码进行更新,避免混淆。

问题在于我使用按钮弹出一个新窗口,但是这个新窗口即使在HTML代码中加载脚本也不会使脚本工作。

为了使这更容易,我只是将一个window.alert添加到应该在新窗口中工作的脚本,并且它永远不会显示。

这是我们用来弹出新窗口(实际工作并正确弹出新窗口)

jQuery(document).ready(function( $ ){
      $("#imprimirPdf").live("click", function () {
        var divContents = $("#cartaDeAmor").html();.
        var printWindow = window.open('', '', 'height=400,width=800')
        printWindow.document.write('<!DOCTYPE html>');    
        printWindow.document.write('<html>');  
        printWindow.document.write('<head>');      
        printWindow.document.write('<script type="text/javascript" src="http://www.mywebsiteurl.com/descargarPDF.js"><\/script>'); // <-- Loading the script that is not working.
        printWindow.document.write('<script type="text/javascript" src="http://www.mywebsiteurl.com/html2pdf.bundle.min.js"><\/script>');         
        printWindow.document.write('</head>');
        printWindow.document.write('<body>');    
        printWindow.document.write('<div id="carta">Content</div>');
        printWindow.document.write('<input type="button" value="Function trigger test" onclick="eKoopman2();">'); // Calling the function 
        printWindow.document.write('<script>')
        printWindow.document.write('(function() { eKoopman2(); })();'); // Calling the function again
        printWindow.document.write('<\/script>')
        printWindow.document.write('</body>')
        printWindow.document.write('</html>');  
        printWindow.document.close();
});
});

这可以在&#34; descargarPDF.js&#34; (我需要加载的脚本)

function eKoopman2() {
  var element = document.getElementById('carta');
  html2pdf(element);
  window.alert("sometext");
}

这是我们在HTML代码中找到的

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript"   src="http://www.mywebsiteurl.com/descargarPDF.js"></script>
    <script type="text/javascript" src="http://www.mywebsiteurl.com/html2pdf.bundle.min.js"></script>
</head>
<body>
    <div id="carta">Content to be exported</div>
    <input type="button" value="Function trigger test" onclick="eKoopman2();">
    <script>(function() { eKoopman2(); })();</script>
</body>
</html>

问题是脚本中加载的警报永远不会显示。不按下按钮,不能在自称函数中工作。

0 个答案:

没有答案