Sample ScriptsQ. How can I make a button to print a page?
A.
There are several
ways to do this.
NEW
- Here is a new cross-browser script that should work in both Internet
Explorer and Netscape.
- Switch to HTML View.
- In the <head> section of your code,
put:
<SCRIPT
Language="Javascript">
function printthis(){
if (NS) {
window.print() ;
} else {
var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box WebBrowser1.outerHTML = "";
}
}
</script>
- In the <body> section of your code
put:
<script Language="Javascript">
var NS = (navigator.appName == "Netscape");
var VERSION = parseInt(navigator.appVersion);
if (VERSION > 3) {
document.write('<form><input type=button value="Click to Print this Page" name="Print" onClick="printthis()"></form>');
}
</script>
Below
is the old individual code for Internet Explorer and Netscape which
you can still use (or combine to use) on your page
Here is the code for Internet Explorer:
- Switch to HTML View.
- In the <head> section of your code,
put:
<script
language="javascript">
function printo()
{
var OLECMDID_PRINT = 6;
var OLECMDEXECOPT_DONTPROMPTUSER = 2;
var OLECMDEXECOPT_PROMPTUSER = 1;
var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0
CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
oldHandler = window.onerror;
window.onerror = deal;
WebBrowser1.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER);
WebBrowser1.outerHTML = "";
window.onerror = oldHandler;
function deal()
{
WebBrowser1.outerHTML = "";
window.onerror = oldHandler;
return true;
}
}
</script>
- In the <body> section of your code
put:
<form>
<input TYPE="Button" VALUE="Print" ONCLICK="printo()">
</form>
Here is the code for Netscape:
- Switch to HTML View.
- In the <head> section of your code,
put:
<script>
<!--
/*
Custom Print Button Script-
© Dynamic Drive (www.dynamicdrive.com)
For full source code, installation instructions,
100's more DHTML scripts, and Terms Of
Use, visit dynamicdrive.com
*/
function doit(){
if (!window.print){
alert("You need NS4.x to use this print button!")
return
}
window.print()
}
//-->
</script>
- In the <body> section of your code
put:
<form>
<input type="button" value="Print Page"
onClick="doit()">
</form>
Note: Both of the
above scripts will only work
in version 4 browsers. |