I used the following code to redirect all the HTTP pages of a site to the https://
version.
<link rel="canonical" href="">
<script>
var url = window.location.href; // get current URL
var link = document.querySelector('link[rel=canonical]'); // get the link element
var changedUrl = [url.slice(0, 4), 's', url.slice(4)].join(''); // change the URL to https://
link.setAttribute('href', changedUrl); // set href attribute of link element
</script>
Now my questions is, will Google read the dynamically generated canonical tags and index them like normal canonical tags? Or will I have to try another approach?