首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何解决无法在弹出窗口中触发脚本?

如何解决无法在弹出窗口中触发脚本?
EN

Stack Overflow用户
提问于 2018-06-13 11:12:51
回答 1查看 0关注 0票数 0

问题是,Im弹出一个带有按钮的新窗口,但是这个新窗口并没有使脚本工作,即使它被加载到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();
});
});

这可以在“descargarPDF.js”(我需要加载的脚本)中找到:

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>

脚本中加载的警报永远不会显示。不在按钮中工作,不在自我调用函数中工作。

EN

回答 1

Stack Overflow用户

发布于 2018-06-13 20:48:02

这个脚本只是简单地调用你的函数eKoopman2。代码:

jQuery(document).ready(function( $ ){
      $("#imprimirPdf").live("click", function () {
        var divContents = $("#cartaDeAmor").html(); // <-- Exports the DIV previous page content. The following "document.write" are the "extra content" to be printed in PDF that cannot be shown in the previous page.
        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>');
        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>');   // /!\ I added this body 
        printWindow.document.write('<div id="carta">Content to be exported</div>');
        printWindow.document.write('<input type="button" value="Function trigger test" onclick="eKoopman2();">');
        printWindow.document.write('<script>')
        printWindow.document.write('(function() { eKoopman2(); })();');
        printWindow.document.write('</script>')
        printWindow.document.write('</body>')
        printWindow.document.write('</html>');  
        printWindow.document.close();
});
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100004898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档