简体   繁体   中英

print div or iframe content from another div using javascript

I have a PHP web page. On the web page i have a iframe and few DIV. Now I want a single print button, which will print the content of the current active window. If no iframe or div is open then it will print the main page else the current iframe source or div content using javascript.

is it possible?

Here is an sample to print an element. Hope this will help you to get an idea.

<html>
<head>

<input type="button" value="Click to Print" onclick="printDiv()" />

<script type="text/javascript"
src="http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js">

</script>
<script type="text/javascript">
function printDiv() {
    var data = $('#divToPrint').html()
    var printWin = window.open('', 'Print Preview', 'height=600,width=800');
    printWin.document.write('<html><head><title>Print Preview</title>');
    printWin.document.write('</head><body >');
    printWin.document.write(data);
    printWin.document.write('</body></html>');
    printWin.print();
    printWin.close();
    return true;
}
</script>
</head>
<body>
<div id="divToPrint">jQuery is a fast, small, and feature-rich
    JavaScript library. It makes things like HTML document traversal and
    manipulation, event handling, animation, and Ajax much simpler with an
    easy-to-use API that works across a multitude of browsers.</div>
</body>
</html>

The data variable should be replaced with what ever that you want to print.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM