»
EnglishFrenchVietnamese

Print - Smarter Print this page script - Print this content - JavaScriptBank.com

Full version: jsB@nk » Utility » Smarter Print this page script - Print this content
URL: https://www.javascriptbank.com/smarter-print-this-page-script-print-this-content.html

Smarter Print this page script - Print this content © JavaScriptBank.comThe name has said feature of this JavaScript code - create a link at specified position (through one id#); this link only will print content that you specified. If browser of visitors disabled JavaScript then the link would not display, and visitors had to use Print command from browser.

Full version: jsB@nk » Utility » Smarter Print this page script - Print this content
URL: https://www.javascriptbank.com/smarter-print-this-page-script-print-this-content.html



JavaScript
<script type="text/javascript"><!--// Created by Roger Johansson - http://www.456bereastreet.com/var addPrintLink = {init:function(sTargetEl,sLinkText) {if (!document.getElementById || !document.createTextNode) {return;} // Check for DOM supportif (!document.getElementById(sTargetEl)) {return;} // Check that the target element actually existsif (!window.print) {return;} // Check that the browser supports window.printvar oTarget = document.getElementById(sTargetEl);var oLink = document.createElement('a');oLink.id = 'print-link'; // Give the link an id to allow stylingoLink.href = '#'; // Make the link focusable for keyboard usersoLink.appendChild(document.createTextNode(sLinkText));oLink.onclick = function() {window.print(); return false;} // Return false prevents the browser from following the link and jumping to the top of the page after printingoTarget.appendChild(oLink);},addEvent:function(obj, type, fn) {if (obj.addEventListener)obj.addEventListener(type, fn, false);else if (obj.attachEvent) {obj["e"+type+fn] = fn;obj[type+fn] = function() {obj["e"+type+fn](window.event);}obj.attachEvent("on"+type, obj[type+fn]);}}};addPrintLink.addEvent(window, 'load', function(){addPrintLink.init('articleDemo','Print this page');});//--></script>


HTML
<div id="articleDemo"><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diamnonummy nibh euismod tincidunt ut laoreet dolore magna aliquam eratvolutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tationullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p><a href="#" id="print-link"></a></div>