Is
it possible to use JavaScript to change the meta-tags of the page?
Yes, it is definitely possible to use JavaScript
to change the Meta tags of the page.
Change
Meta tag content dynamically through JavaScript:
Let’s
see the Example,
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" id="viewport" content="width=device-width,
initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>dynamically add meta tag JavaScript</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js" async="async"></script>
<script>
$(function(){
//update Meta Tag in
Header
updateMetaTag("viewport");
});
var updateMetaTag = function (id) {
var meta = document.getElementById(id);
meta.setAttribute("content", "width=device-width,
initial-scale=1.0");
OR
//document.querySelector('meta[name="viewport"]').setAttribute("content",
"width=device-width, initial-scale=1.0");
}
</script>
</body>
</html>