DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
Javascript

Print a web page content using JavaScript

Share

Are you aware of printing the content of a web page via an actual printer using JavaScript? Don’t get confuse!

JavaScript can help you to implement print functionality using a print function of window object. This works on all modern browsers, so this can be a valid substitute inside a browser where toolbar is disabled.

Here is the syntax to print using JavaScript:

window.print()

The JavaScript print() method prints the contents of a current window.
This opens a Print Dialog Box, which tells the user to select desired printing options.

Browser Support: Chrome, Internet Explorer, Firefox, Safari, Opera.

You can use this function using the onclick event as shown in the example below:

<html>
<head>
</head>
<body>
<form>
<input type="button" value="Print" >

Always make sure that your visitor’s browser have JavaScript enabled before providing them a button that works using JavaScript.

Read Also: HTML noscript Tag

If this is disabled then, saving them frustration of clicking a same button which does absolutely nothing:

<script language="JavaScript">
if (window.print) {
document.write('<form><input type="button" value="Print" >

If you do not like the functionality about print using JavaScript on a web page, then you can use the browser’s default toolbar to get print a web page like below:
File >  Print > Click OK  button.

Printing Suggestions:

  • Ready a page for printing
  • Make that page printer friendly
  • Allow users to choose to print
  • Never attach this to any user event
Jacob Frazier

Based on United States, Jacob Frazier is a skilled JavaScript developer with over 8 years of experience. He is passionate about change and trying new things, both professionally and personally. He loves startups and is extremely proactive.

Recent Posts

Benefits of Manual Therapy for Faster Recovery from Muscle and Joint Pain

Pain in muscles and joints can be a problem in everyday life, whether it involves…

3 days ago

How Digital-First Brands Can Make Offline Marketing Feel Connected

Most modern businesses spend a lot of time thinking about digital touchpoints. Websites, email campaigns,…

6 days ago

X Bets on Video

While TikTok and Instagram battle for users’ attention with short vertical videos, Elon Musk’s social…

2 weeks ago

5 Reasons to use a professional mortgage broker

When you are looking to apply for a mortgage the one thing that most people…

4 weeks ago

Tips – Advanced Blackjack Strategy

Once you've memorized basic blackjack strategy, it's time to delve deeper into the game. Advanced…

1 month ago

The Modern Practice of Yoga: How Balance, Mindfulness, and Lifestyle Design Are Transforming Everyday Wellbeing

Yoga is no longer just a physical practice. Yoga is popular as a lifestyle practice.…

1 month ago