-1

I know this is a duplicate but for the life of me I cannot figure out why my basic code is not working. If anyone can dope slap me in the right direction I would greatly appreciate it. I want to be able to update any of the meta tags; description, og:description, etc...

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="" />
    <meta name="keywords" content="Online Learning" />
    <meta name="twitter:card" content="summary_large_image" />
    <meta name="twitter:site" content="@mysite" />
    <meta property="og:url" content="https://online.mysite.com" />
    <meta property="og:title" content="" />
    <meta property="og:description" content="" />
    <title>Hello, world!</title>
  </head>
  <body>
    <h1>Hello, world! +</h1>
    <script src="//www.mysite.com/jquery-3.4.1.min.js"></script>
    <script src="/mysite.com/js/kube.js?t=1569848987"></script> 
    <script>
        $(document).ready(function(){ 
            $('meta[name=description]').attr('content', "new content descr");
        });
    </script>
  </body>
</html>

As well as the javascript below but neither has updated the description. I have placed the js below, at the bottom of the code as well as in the head.

document.getElementsByTagName('meta')["description"].content = "My new page description!!";

Thanks in advance for anyone who can dope-slap me in the right direction.

user1469
  • 111
  • 7
  • I've had similar problems before because my browser had cached the page. Have you tried opening your page in an environment that doesn't persistently store the cache like incognito mode on Chrome, for example? – TomBonynge Feb 12 '21 at 14:57
  • Duplicate of [Is it possible to use JavaScript to change the meta-tags of the page?](https://stackoverflow.com/questions/2568760/is-it-possible-to-use-javascript-to-change-the-meta-tags-of-the-page) Try `document.head.children.namedItem('description').content = 'xxx'` just tasted it, it works. And getElementsByTagName returns array of elements, hence `ElementsByTag`, not `ElementByTag` – ikiK Feb 12 '21 at 15:03
  • I know the problem is me! I have placed the following code in the head and still not working. – user1469 Feb 12 '21 at 15:11

1 Answers1

0

This should work:

document.querySelector('meta[name="description"]').setAttribute("content", "My new page description!!");
TomBonynge
  • 303
  • 1
  • 5
  • You copy/pasted pasted this of other topic here, the one I reported as duplicate? https://stackoverflow.com/a/47212820/7158959 There is report as duplicate button here on SO just for cases like this. Just for future reference. – ikiK Feb 12 '21 at 15:07
  • I know, I should have added to that post instead of creating a new one. Definitely my bad! – user1469 Feb 12 '21 at 15:13