2

I will edit this main post to update with the latest code and avoid confusing.

The problem is that Im popping up a new window with a button, but this new window is not making a script work even if its being loaded in the HTML code.

To make this easier, I just added a window.alert to the script that should be working into the new window, and its never showing.

This is what we use to pop up the new window (actually working and popping up the new window properly)

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();
});
});

This can be found at "descargarPDF.js" (the script that I need to be loaded)

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

This is what we find in the HTML code

<!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>

And the problem is that the alert loaded in the script is never showing. Not working in the button click and not working in the self called function.

  • What do you want to do with this line : `var divContents = $("#cartaDeAmor").html();` your `divContents` does not seem to be used anywhere – Deblaton Jean-Philippe May 27 '18 at 17:21
  • #cartaDeAmor is the div that holds the content that I want to be exported into the new window. Withouth that line the content would be incomplete. The rest of the "document.write" are just adding the extra content that cannot be shown on the main page and I'd like to be printed in the PDF. Thanks you so much! sorry if Im missing something – Patrick Ben May 27 '18 at 17:31

0 Answers0