问题是,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>
脚本中加载的警报永远不会显示。不在按钮中工作,不在自我调用函数中工作。
发布于 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();
});
});
https://stackoverflow.com/questions/-100004898
复制相似问题