5

When I click "Print" using the jQuery Print Preview Plugin the following error pops up and Firebug:

Error: Permission denied to access property 'name'

     if (window.frames[i].name == "print-frame") { 

I am not sure exactly what it means or how to correct it.

L84
  • 45,514
  • 58
  • 177
  • 257

2 Answers2

3

There is a way around this that will solve this problem and work properly with all major browsers. This solution was found by Derick over on the Github page for jQuery Print Preview.

Here is the solution, around line 44 you will see the following code:

// The frame lives
        for (var i=0; i < window.frames.length; i++) {
            if (window.frames[i].name == "print-frame") {
                var print_frame_ref = window.frames[i].document;
                break;
            }
        }

Replace the above code with this:

print_frame_ref = print_frame[0].contentWindow.document;

issue solved.

L84
  • 45,514
  • 58
  • 177
  • 257
2

Here is the error in Chrome, I expect this makes it clear?

Unsafe JavaScript attempt to access frame with URL http://s7.addthis.com/static/r07/sh090.html#iit=1341762779832&tmr=load%3D1341762779182%26core%3D1341762779520%26main%3D1341762779826%26ifr%3D1341762779833&cb=0&cdn=0&chr=UTF-8&kw=&ab=-&dh=www.ubhape2.com&dr=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F11384440%2Ferror-permission-denied-jquery-print-preview&du=http%3A%2F%2Fwww.ubhape2.com%2Ftest-print.html&dt=TEST%20Page&md=0&cap=tc%3D0%26ab%3D0&inst=1&irt=0&jsl=33&lng=en-US&ogt=&pc=men&pub=ra-4dfb00d56c76d2a5&ssl=0&sid=4ff9acdb1a41cc60&srd=0&srf=0.02&srp=0.2&srl=1&srx=1&ver=300&xck=0&xtr=0&og=&rev=114791&ct=1&xld=1&xd=1 from frame with URL http://www.ubhape2.com/test-print.html. Domains, protocols and ports must match.

Your page is located on ww.ubhape2.com and you are accessing a frame on s7.addthis.com


To fix this problem, change this line

<script type="text/javascript" src="https://s7.addthis.com/js/300/addthis_widget.js#pubid=ra-4dfb00d56c76d2a5"></script>

To point to the copied script on your site.

You will also have to edit that script to only access your own site.

This is an example of XSS or cross site scripting.


My question then turns into can I edit the jQuery Print Preview Script to prevent the conflict from happening?

No.

The point of the error is that the javascript is running in the context of another party, and you can't "inject" your code into it.

This is enforced by the browser.

If it was not enforced then every user on the internet would have had their machines compromised.

Read up on a google search of XSS to find out more

However,

If you host the javascript (and thus the iframe) on your server than the issue goes away. It is your code (and your iframe) to do with as you wish.

Hogan
  • 69,564
  • 10
  • 76
  • 117
  • That does make it more clear. I am still not sure how to work around this though. I want to show a preview for my page. Not AddThis. How do I prevent it from accessing an AddThis Frame? – L84 Jul 08 '12 at 16:01
  • Thanks for the information, that basically means beyond hosting the script itself I must also host all of the CSS files etc. Whatever the script points to externally I must host. Correct? – L84 Jul 08 '12 at 16:15
  • My last question is instead of hosting the AddThis scripts/files, is there a way I can edit the Print Preview JS to fix the conflict? – L84 Jul 08 '12 at 16:23
  • q1, you can probably get away with not hosting the CSS and image files. q2, no, the point of the error is that the javascript is running in the context of another party, and you can't "inject" your code into it. This is enforced by the browser. If it was not enforced then every user on the internet would have had their machines compromised. Read up on XSS to find out more. – Hogan Jul 08 '12 at 16:28
  • Got it, thanks for your help. I have attempted to host the JS on my site and it still throws the same error. One thing that does confuse me is we keep talking about an iframe. I do not see an iframe in the code after AddThis has generated what it will generate. Where is the iframe coming into play? – L84 Jul 08 '12 at 16:36
  • You are right -- frame is a Chrome term -- replace that with "pop up window" – Hogan Jul 08 '12 at 16:38
  • I was reading the GitHub page for the Print Preview Plugin and [found a solution](https://github.com/etimbo/jquery-print-preview-plugin/issues/6). I posted it here. – L84 Jul 25 '12 at 01:43