Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error - Permission denied - jQuery Print Preview?

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.

like image 823
L84 Avatar asked Jul 08 '12 15:07

L84


2 Answers

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.

like image 99
L84 Avatar answered Oct 13 '22 02:10

L84


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.

like image 31
Hogan Avatar answered Oct 13 '22 01:10

Hogan