Copy Html page source code to clipboard with JS

I’ve been trying to copy an html page’s source code through the click of a button currently I’m getting an error that i’ve pasted below. I’m not familiar with the .execCommand function and why it’s blowing up in this instance.

Uncaught TypeError: body.execCommand is not a function
at copy (index.js:8)
at HTMLButtonElement. (index.js:14)

The code:

document.addEventListener("DOMContentLoaded",function(event) {
  document.designMode = "on"
  var mode = document.designMode

    function copy(){
    body =  document.getElementsByTagName("BODY")[0]
    // .execCommand("copy")

    var button = document.getElementsByClassName("copybutton")


27 thoughts on “Copy Html page source code to clipboard with JS”