Printing DIV Contents with Enhanced Functionality
In various web development scenarios, you may encounter the need to print the contents of a specific DIV element. To achieve this, there are several approaches available. In this article, we will delve into a method that provides enhanced functionality and compatibility across different browsers, particularly Chrome.
The Solution
The provided function, PrintElem(), offers a comprehensive approach for printing the contents of a DIV:
function PrintElem(elem) { var mywindow = window.open('', 'PRINT', 'height=400,width=600'); mywindow.document.write('<html><head><title>' + document.title + '</title></head>'); mywindow.document.write('<body>'); mywindow.document.write('<h1>' + document.title + '</h1>'); mywindow.document.write(document.getElementById(elem).innerHTML); mywindow.document.write('</body></html>'); mywindow.document.close(); // necessary for IE >= 10 mywindow.focus(); // necessary for IE >= 10*/ mywindow.print(); mywindow.close(); return true; }
Explanation
By employing this function, you can effortlessly print the contents of any DIV element with additional features like a page title, ensuring compatibility and providing a smooth printing experience for your users.
The above is the detailed content of How Can I Print a Specific DIV's Content with Enhanced Browser Compatibility?. For more information, please follow other related articles on the PHP Chinese website!