最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do print specific content inside the iframe - Stack Overflow

programmeradmin3浏览0评论
<script type="text/javascript">
function printDoc() {
   document.getElementById("frame_singleCheque").contentWindow.print();
}
</script>

<iframe  style ="height:400px; width: 750px; overflow:scroll;"  id="frame_singleCheque" src=""></iframe>
<input type="button" id = "btnCPrint" value = "Print" onclick="javascript:printDoc()" />

Error

[16:41:44.054] Error: Permission denied to access property 'print' @ http://localhost/pdf/print.php:3

i have verified with lot of stack suggested threads for print iframe content. but for me those are not worked.

Exactly above code only present in my print.php file. i want to print the iframe content.

Also i want to know, how to print the specific div which is present inside the iframe. example in this iframe " class = 'example_code notranslate' " .

<script type="text/javascript">
function printDoc() {
   document.getElementById("frame_singleCheque").contentWindow.print();
}
</script>

<iframe  style ="height:400px; width: 750px; overflow:scroll;"  id="frame_singleCheque" src="http://www.w3schools."></iframe>
<input type="button" id = "btnCPrint" value = "Print" onclick="javascript:printDoc()" />

Error

[16:41:44.054] Error: Permission denied to access property 'print' @ http://localhost/pdf/print.php:3

i have verified with lot of stack suggested threads for print iframe content. but for me those are not worked.

Exactly above code only present in my print.php file. i want to print the iframe content.

Also i want to know, how to print the specific div which is present inside the iframe. example in this iframe " class = 'example_code notranslate' " .

Share Improve this question asked Aug 28, 2013 at 11:16 BharanikumarBharanikumar 25.7k50 gold badges135 silver badges201 bronze badges 4
  • First focus() the window in iframe and then simply print(). – Teemu Commented Aug 28, 2013 at 11:20
  • 1 do you have access to the contents of iframe? is it your page? – Reigel Gallarde Commented Aug 28, 2013 at 11:21
  • 1 you cannot inject/get content into/from a page from a different domain it violates the security sandbox. – Patrick Evans Commented Aug 28, 2013 at 11:23
  • Since HTML5, you get more control over iFrames. By using the sandbox or seamless attributes, you can integrate the iframe even further in your webpage - w3ctutorial./html5-tags/tag-iframe – BlueCacti Commented Nov 24, 2014 at 22:35
Add a ment  | 

3 Answers 3

Reset to default 4

You can print a cross-domain iframe page perfectly by nesting the cross domain iframe in a locally hosted iframe. A "proxy iframe".

This way the parent javascript can print the proxy iframe without issue since it's local, which will transitively print it's inner iframe originating from another domain.

This technique works and has been verified by myself.

In your container page, host the iframe like this

 <iframe id="printf" name="printf" class="A4" src="/SomeController/IFrameProxy?url=TARGETURL"></iframe>

The proxy iframe should look like something like this

        @model string
        <html>
            <head>
                <title>IFrame Proxy</title>
                <style>
                    html, body, iframe {
                        height: 100%;
                        width: 100%;
                        margin: 0;
                        padding: 0;
                        border-style: none;
                    }
                </style>

            </head>
            <body>
                <iframe src="@Model" seamless></iframe>
            </body>
        </html>

The javascript that prints the iframe (defined on container page) should look something like this:

        function printFrame() {
            var frm = document.getElementById("printf").contentWindow;
            frm.focus();// focus on contentWindow is needed on some ie versions
            frm.print();
        }

It look like you've got a pretty standard cross-domain iframe issue here - namely that browsers are designed so you can't do this. There's a lot of fudges I could suggest, but largely, iframes are the wrong solution to this problem.

Basically, your best bet is to scrape the page - make a http request and parse out the content you want. Then you can interact with it as part of your own page's DOM.

How do I print an IFrame from javascript in Safari/Chrome
Print iFrame Content (#5)
Print contents of IFRAME from parent window

发布评论

评论列表(0)

  1. 暂无评论