
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
Execute a script before the document is printed in HTML?
The task we are going to perform in this article is execute a script before the document is printed in HTML.
When a page is about to be printed, the HTML onbeforeprint attribute is used. And before the print dialogue box appears, the alert message is displayed. Together with the onafterprint attribute, the onbeforeprint attribute is utilised. This is a part of event attribute.
Let’s dive into the following examples to understand more about to execute a script before the document is printed in HTML.
Example 1
In the following examples we are using HTML onbeforeprint attribute.
<!DOCTYPE html> <html> <body onbeforeprint="mytutorial()"> <h1>MS DHONI</h1> <h2>Finishes off in his Style.</h2> <script> function mytutorial() { alert("Sure To Print!"); } </script> </body> </html>
When the script gets executed, it will generate an output displaying the text used in the script on the webpage.
When the user tries to print the webpage, the event gets triggered and displays an alert "sure to print" on the webpage.
Example 2
Here is the another example for implementing onbeforeprint attribute.
<!DOCTYPE html> <html> <body onbeforeprint = "display()"> <h1>TUTORIALSPOINT</h1> <script> function display() { alert("The document will now print."); } </script> </body> </html>
On running the above script, the output window pops up and displays the text "tutorialspoint" on the webpage.
If the user tries to print the webpage, the event gets triggered and displays an alert on the webpage.
- Related Articles
- Execute a script after the document is printed in HTML?
- Execute a script when the document is about to be unloaded in HTML?
- Execute a script when fetching the media data is stopped before it is completely loaded in HTML?
- Execute a script when the file is unavailable in HTML?
- Execute a script when the element is invalid in HTML?
- Execute a script when the browser window is closed in HTML?
- Execute a script when the element is being dragged in HTML?
- Execute a script when the element is being clicked in HTML?
- Execute a script when the element is finished loading in HTML?
- Execute a script when media data is loaded in HTML?
- Execute a script when the browser window is being resized in HTML?
- Execute a script when the element is being double-clicked in HTML?
- Execute a script when the dragged element is being dropped in HTML?
- Execute a script when the element loses focus in HTML?
- Execute a script when the element gets focus in HTML?
