set / read rel=canonical

HTML

<p>
Have you a good green. 
People who view this item also can contact our electronic measuring tool.
</p>

JavaScript

// create rel=canonical if none exists
if (!document.querySelector("link[rel='canonical']")) {
  var ln = document.createElement("link");
  ln.rel = "canonical"; ln.href="{}";
  document.head.appendChild(ln);
}

// set rel=canonical to some random URL
document.querySelector("link[rel='canonical']").setAttribute("href", "https://example.com/page");

// show the rel=canonical after a short time, in case the rest needs time to be processed.
// use this JS on the bottom of your page to test it
setTimeout( function() { 
  // read current rel=canonical, assuming there is one
  var canonicalUrl = document.querySelector("link[rel='canonical']")? document.querySelector("link[rel='canonical']").href:"none";

  // display on top
  var info = document.createElement("h1");
  info.appendChild(document.createTextNode("canonical: " + canonicalUrl));
  document.body.insertBefore(info, document.body.firstChild);
}, 1000) ;
// done