AnsweredAssumed Answered

JavaScipt Error: Cannot read property 'document' of undefined

Question asked by Chelsea Clay on Feb 27, 2019
Latest reply on Feb 28, 2019 by Jay Jiang

I'm working on a landing page that uses tokens to populate a certificate. Users need to be able to print their certificates. I used JavaScript for this in order to print just the certificate and not the whole landing page. I thought I was missing an addon from Marketo, but our representative said I should just be able to put the JavaScript into an HTML element. I did this and the code isn't working. I have the code working right locally. But as soon as I add the same code to the Marketo page, it doesn’t work. When I check the error, it says it cannot read the property “document” of undefined. But JavaScript is based off the document property so I don't understand why Markteto isn't reading it.

 

This is the button I have the code set to run on:

<div style="background-color: #00a0df; border-radius: 5px; font-family: Arial, Helvetica, sans-serif; font-size: 26px; text-align: center; padding: 5px;">
    <a href="javascript:printDiv('div1')" style="color: white; text-decoration: none;">Print</a>
</div>

 

This is the JavaScript I have to run when the Print btn is clicked:

        <script>
            printDivCSS = new String ('<link href="myprintstyle.css" rel="stylesheet" type="text/css">')
            function printDiv(divId) {
                window.frames["print_frame"].document.body.innerHTML=printDivCSS + document.getElementById(divId).innerHTML;
                window.frames["print_frame"].window.focus();
                window.frames["print_frame"].window.print();
            }
    
        </script>

 

If it helps, here is the link to the sample page: http://pages.chatsworth.com/Sample-Credit-Certificate-LP_Sample-Credit-Certificate-LP.html

Outcomes