Tutorial

Take Webpage Screenshot with HTML5 and JavaScript

Here I come with the one quick tip related to HTML5 and JavaScript. This is about to take webpage screenshot using HTML5 and JavaScript code only. Yes you have read it correct its using HTML5 and JavaScript only. :)

[gads]

Without wasting much time let’s have a look at code and yes working example for sure.

[cc lang=”js”]

(function (exports) {
function urlsToAbsolute(nodeList) {
if (!nodeList.length) {
return [];
}
var attrName = ‘href’;
if (nodeList[0].__proto__ === HTMLImageElement.prototype
|| nodeList[0].__proto__ === HTMLScriptElement.prototype) {
attrName = ‘src’;
}
nodeList = [].map.call(nodeList, function (el, i) {
var attr = el.getAttribute(attrName);
if (!attr) {
return;
}
var absURL = /^(https?|data):/i.test(attr);
if (absURL) {
return el;
} else {
return el;
}
});
return nodeList;
}

function screenshotPage() {
urlsToAbsolute(document.images);
urlsToAbsolute(document.querySelectorAll(“link[rel=’stylesheet’]”));
var screenshot = document.documentElement.cloneNode(true);
var b = document.createElement(‘base’);
b.href = document.location.protocol + ‘//’ + location.host;
var head = screenshot.querySelector(‘head’);
head.insertBefore(b, head.firstChild);
screenshot.style.pointerEvents = ‘none’;
screenshot.style.overflow = ‘hidden’;
screenshot.style.webkitUserSelect = ‘none’;
screenshot.style.mozUserSelect = ‘none’;
screenshot.style.msUserSelect = ‘none’;
screenshot.style.oUserSelect = ‘none’;
screenshot.style.userSelect = ‘none’;
screenshot.dataset.scrollX = window.scrollX;
screenshot.dataset.scrollY = window.scrollY;
var script = document.createElement(‘script’);
script.textContent = ‘(‘ + addOnPageLoad_.toString() + ‘)();’;
screenshot.querySelector(‘body’).appendChild(script);
var blob = new Blob([screenshot.outerHTML], {
type: ‘text/html’
});
return blob;
}

function addOnPageLoad_() {
window.addEventListener(‘DOMContentLoaded’, function (e) {
var scrollX = document.documentElement.dataset.scrollX || 0;
var scrollY = document.documentElement.dataset.scrollY || 0;
window.scrollTo(scrollX, scrollY);
});
}

function generate() {
window.URL = window.URL || window.webkitURL;
window.open(window.URL.createObjectURL(screenshotPage()));
}
exports.screenshotPage = screenshotPage;
exports.generate = generate;
})(window);

[/cc]

Demo

[gads]

I have created a demo page for above code so you can see that in action. Live Example.

I left this article open for discussion, so it would be great if you post your views, comments, suggestion via comment form below.

Shares:
  • cristine
    cristine
    December 2, 2012 at 2:03 am

    As usual you have posted with a few extremely intriguing elements and also
    I have already added this web-site to one I’ll pay attention to ! ! !

    Reply
  • tommie
    tommie
    January 7, 2013 at 9:30 pm

    Hi colleagues, its great article regarding cultureand completely defined, keep it up all the time.

    Reply
  • alif
    alif
    May 17, 2013 at 12:30 am

    nice one sir

    Reply
  • Guest
    Guest
    May 17, 2013 at 7:04 pm

    and how would i save it as a real graphic?

    Reply
  • Suejb
    Suejb
    December 20, 2019 at 2:54 pm

    Is it possible the result image to be saved into a file?

    Reply
  • viral
    viral
    December 20, 2013 at 2:02 pm

    Avinash sir
    this good article
    so pls tell me how to save this snapshot in image in c#

    Reply
  • Rido
    Rido
    March 4, 2014 at 3:53 pm

    Heyyy, I would like to use this code but the example is not working and I have some questions. Would you reupload the example? Very thank you

    Reply
  • KoffeeBean
    KoffeeBean
    April 18, 2014 at 5:43 am

    Nifty. But what’s the point if one can’t save the image or do anything with it except seeing it in another tab? I am curious to learn more about what you did there though.

    Reply
  • Kashban
    December 20, 2019 at 2:55 pm

    Unfortunately the demo site is no longer there. Any mirror available?

    Reply
  • Mummy Ninja
    Mummy Ninja
    July 20, 2014 at 12:47 am

    Will this work in an iframe? I want to use this in a Google Maps iframe to automatically get a snapshot of the map. :(

    Reply
    • vishal shori
      vishal shori
      August 6, 2014 at 8:34 pm

      if you want to take snap shot of google map..use html2canvas and while using that make “userCors: True” this will convert your google map into dataurl and you can save it as image

      Reply
  • Divy Singh Rathore
    Divy Singh Rathore
    August 2, 2014 at 10:20 pm

    Avinash sir can you tell how to take snapshot remote website without opening it?

    Reply
    • Ashok Pachauri
      Ashok Pachauri
      January 11, 2017 at 1:18 pm

      You Should Use The Phantom JS For That Which Can Easily Provide you the images without opening the url.

      Reply
  • Avinash
    August 28, 2014 at 12:03 am

    Its up now, thanks for pointing…

    Reply
    • Malik Jehangir Riaz
      Malik Jehangir Riaz
      March 13, 2015 at 4:21 pm

      Hi avinash;
      is there a way to get the blob inside an image control ?

      Reply
  • Amandine
    Amandine
    September 9, 2014 at 7:49 pm

    i tried the code, but me i don’t get any img or css…. any idea why?

    Reply
  • gaynorvader
    gaynorvader
    October 9, 2014 at 9:33 pm

    All this does in your example is open the code in a new tab. No screenshot is taken. Am I missing something?

    Reply
  • Muthu
    Muthu
    December 29, 2014 at 5:04 pm

    please say the method to save the image

    Reply
  • Georges Langeard
    February 12, 2015 at 10:10 pm

    Have you found a solution to save the resulting image ? Thanks a lot anyway..

    Reply
  • Ashok Pachauri
    Ashok Pachauri
    January 11, 2017 at 1:00 pm

    I am unable to use this code plaese sir let u know me how to use to save the image created by the code.

    Reply
  • Saurabh
    Saurabh
    March 1, 2018 at 9:25 am

    Hi Avinash, I have a question can we assign the blob to base64 and then save it or any other method is available to save the screen shot .Please let me know about it.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *