Change Meta tag content dynamically in JavaScript

Add /Change Meta Tags Dynamically Using JavaScript

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>
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^