Change Meta Title and Description using Vue.js












0















Is it possible to change anything higher than the body tag in Vue.Js? The contents for both these elements is currently stored in the JSON file that is attached to an element further down the DOM tree.



I need to try and inject a meta title and description that can be crawled by Google (ie. It injects, then renders before it gets crawled) and understand the issues with accessing the body element and higher up the DOM tree, as the current Vue JSON is injected using the App ID on a DIV lower down.



I have previously used some jQuery code to address this issue on a Square Space template in some previous work



jQuery('meta[name=description]').attr('content', 'Enter Meta Description Here');



PAGE HTML



<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="{{items[0][0].meta-desc}}">
<meta name="author" content="">
<title>{{items[0][0].meta-title}}</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<!-- Vue.js CDN -->
<script src="https://unpkg.com/vue"></script>
</head>

<body>
<!-- Page List -->
<div class="container text-center mt-5" id="app">
<h1 class="display-4">Vue Page Output:</h1>
<h2>{{items[0][0].page1}}</h2>
</div>
<div class="container text-center mt-5">
<h3>Other Pages</h3>
<a href="products.html">Products</a>
<a href="contactus.html">Contact Us</a>
</div>
<!-- /.container -->

<script type="text/javascript">
const app = new Vue({
el: '#app',
data: {
items:
},
created: function () {
fetch('test.json')
.then(resp => resp.json())
.then(items => {
this.items = items
})
}
});
</script>
</body>

</html>


JSON



  [
[
{
"page1": "Company Name",
"meta-title": "Acme Corp"
"meta-desc": "Welcome to Acme Corp"
}
],
[
{
"products": "Product List"
}
],
[
{
"contactus": "Contact Us at Acme Corp"
}
]


Here is the code in action, the incoming JSON file comes in a fixed array format with the meta details alongside the body elements. Making this a bit more tricky.



https://arraydemo.netlify.com/










share|improve this question



























    0















    Is it possible to change anything higher than the body tag in Vue.Js? The contents for both these elements is currently stored in the JSON file that is attached to an element further down the DOM tree.



    I need to try and inject a meta title and description that can be crawled by Google (ie. It injects, then renders before it gets crawled) and understand the issues with accessing the body element and higher up the DOM tree, as the current Vue JSON is injected using the App ID on a DIV lower down.



    I have previously used some jQuery code to address this issue on a Square Space template in some previous work



    jQuery('meta[name=description]').attr('content', 'Enter Meta Description Here');



    PAGE HTML



    <!DOCTYPE html>
    <html lang="en">

    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="{{items[0][0].meta-desc}}">
    <meta name="author" content="">
    <title>{{items[0][0].meta-title}}</title>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <!-- Vue.js CDN -->
    <script src="https://unpkg.com/vue"></script>
    </head>

    <body>
    <!-- Page List -->
    <div class="container text-center mt-5" id="app">
    <h1 class="display-4">Vue Page Output:</h1>
    <h2>{{items[0][0].page1}}</h2>
    </div>
    <div class="container text-center mt-5">
    <h3>Other Pages</h3>
    <a href="products.html">Products</a>
    <a href="contactus.html">Contact Us</a>
    </div>
    <!-- /.container -->

    <script type="text/javascript">
    const app = new Vue({
    el: '#app',
    data: {
    items:
    },
    created: function () {
    fetch('test.json')
    .then(resp => resp.json())
    .then(items => {
    this.items = items
    })
    }
    });
    </script>
    </body>

    </html>


    JSON



      [
    [
    {
    "page1": "Company Name",
    "meta-title": "Acme Corp"
    "meta-desc": "Welcome to Acme Corp"
    }
    ],
    [
    {
    "products": "Product List"
    }
    ],
    [
    {
    "contactus": "Contact Us at Acme Corp"
    }
    ]


    Here is the code in action, the incoming JSON file comes in a fixed array format with the meta details alongside the body elements. Making this a bit more tricky.



    https://arraydemo.netlify.com/










    share|improve this question

























      0












      0








      0








      Is it possible to change anything higher than the body tag in Vue.Js? The contents for both these elements is currently stored in the JSON file that is attached to an element further down the DOM tree.



      I need to try and inject a meta title and description that can be crawled by Google (ie. It injects, then renders before it gets crawled) and understand the issues with accessing the body element and higher up the DOM tree, as the current Vue JSON is injected using the App ID on a DIV lower down.



      I have previously used some jQuery code to address this issue on a Square Space template in some previous work



      jQuery('meta[name=description]').attr('content', 'Enter Meta Description Here');



      PAGE HTML



      <!DOCTYPE html>
      <html lang="en">

      <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
      <meta name="description" content="{{items[0][0].meta-desc}}">
      <meta name="author" content="">
      <title>{{items[0][0].meta-title}}</title>
      <!-- Bootstrap CSS -->
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
      <!-- Vue.js CDN -->
      <script src="https://unpkg.com/vue"></script>
      </head>

      <body>
      <!-- Page List -->
      <div class="container text-center mt-5" id="app">
      <h1 class="display-4">Vue Page Output:</h1>
      <h2>{{items[0][0].page1}}</h2>
      </div>
      <div class="container text-center mt-5">
      <h3>Other Pages</h3>
      <a href="products.html">Products</a>
      <a href="contactus.html">Contact Us</a>
      </div>
      <!-- /.container -->

      <script type="text/javascript">
      const app = new Vue({
      el: '#app',
      data: {
      items:
      },
      created: function () {
      fetch('test.json')
      .then(resp => resp.json())
      .then(items => {
      this.items = items
      })
      }
      });
      </script>
      </body>

      </html>


      JSON



        [
      [
      {
      "page1": "Company Name",
      "meta-title": "Acme Corp"
      "meta-desc": "Welcome to Acme Corp"
      }
      ],
      [
      {
      "products": "Product List"
      }
      ],
      [
      {
      "contactus": "Contact Us at Acme Corp"
      }
      ]


      Here is the code in action, the incoming JSON file comes in a fixed array format with the meta details alongside the body elements. Making this a bit more tricky.



      https://arraydemo.netlify.com/










      share|improve this question














      Is it possible to change anything higher than the body tag in Vue.Js? The contents for both these elements is currently stored in the JSON file that is attached to an element further down the DOM tree.



      I need to try and inject a meta title and description that can be crawled by Google (ie. It injects, then renders before it gets crawled) and understand the issues with accessing the body element and higher up the DOM tree, as the current Vue JSON is injected using the App ID on a DIV lower down.



      I have previously used some jQuery code to address this issue on a Square Space template in some previous work



      jQuery('meta[name=description]').attr('content', 'Enter Meta Description Here');



      PAGE HTML



      <!DOCTYPE html>
      <html lang="en">

      <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
      <meta name="description" content="{{items[0][0].meta-desc}}">
      <meta name="author" content="">
      <title>{{items[0][0].meta-title}}</title>
      <!-- Bootstrap CSS -->
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
      <!-- Vue.js CDN -->
      <script src="https://unpkg.com/vue"></script>
      </head>

      <body>
      <!-- Page List -->
      <div class="container text-center mt-5" id="app">
      <h1 class="display-4">Vue Page Output:</h1>
      <h2>{{items[0][0].page1}}</h2>
      </div>
      <div class="container text-center mt-5">
      <h3>Other Pages</h3>
      <a href="products.html">Products</a>
      <a href="contactus.html">Contact Us</a>
      </div>
      <!-- /.container -->

      <script type="text/javascript">
      const app = new Vue({
      el: '#app',
      data: {
      items:
      },
      created: function () {
      fetch('test.json')
      .then(resp => resp.json())
      .then(items => {
      this.items = items
      })
      }
      });
      </script>
      </body>

      </html>


      JSON



        [
      [
      {
      "page1": "Company Name",
      "meta-title": "Acme Corp"
      "meta-desc": "Welcome to Acme Corp"
      }
      ],
      [
      {
      "products": "Product List"
      }
      ],
      [
      {
      "contactus": "Contact Us at Acme Corp"
      }
      ]


      Here is the code in action, the incoming JSON file comes in a fixed array format with the meta details alongside the body elements. Making this a bit more tricky.



      https://arraydemo.netlify.com/







      javascript jquery vue.js vuejs2 vue-component






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 13:25









      GracieGracie

      838




      838
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Since what you want to change is outside the area controlled by Vue, you just use ordinary DOM manipulation. It would be something like



          created() {
          fetch('test.json')
          .then(resp => resp.json())
          .then(items => {
          this.items = items;
          const descEl = document.querySelector('head meta[name="description"]');
          const titleEl = document.querySelector('head title');

          descEl.setAttribute('content', items[0]['meta-desc']);
          titleEl.textContent = items[0]['meta-title'];
          })
          }





          share|improve this answer
























          • Thanks Roy J. That's worked a treat - updated the site now. So this is just vanilla JS but within the Vue method because it is being used on the JSON response right? I'm sure I heard on a tutorial about a certain component way in Vue to work with <head> elements but this will do just fine.

            – Gracie
            Nov 13 '18 at 14:49











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282040%2fchange-meta-title-and-description-using-vue-js%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Since what you want to change is outside the area controlled by Vue, you just use ordinary DOM manipulation. It would be something like



          created() {
          fetch('test.json')
          .then(resp => resp.json())
          .then(items => {
          this.items = items;
          const descEl = document.querySelector('head meta[name="description"]');
          const titleEl = document.querySelector('head title');

          descEl.setAttribute('content', items[0]['meta-desc']);
          titleEl.textContent = items[0]['meta-title'];
          })
          }





          share|improve this answer
























          • Thanks Roy J. That's worked a treat - updated the site now. So this is just vanilla JS but within the Vue method because it is being used on the JSON response right? I'm sure I heard on a tutorial about a certain component way in Vue to work with <head> elements but this will do just fine.

            – Gracie
            Nov 13 '18 at 14:49
















          0














          Since what you want to change is outside the area controlled by Vue, you just use ordinary DOM manipulation. It would be something like



          created() {
          fetch('test.json')
          .then(resp => resp.json())
          .then(items => {
          this.items = items;
          const descEl = document.querySelector('head meta[name="description"]');
          const titleEl = document.querySelector('head title');

          descEl.setAttribute('content', items[0]['meta-desc']);
          titleEl.textContent = items[0]['meta-title'];
          })
          }





          share|improve this answer
























          • Thanks Roy J. That's worked a treat - updated the site now. So this is just vanilla JS but within the Vue method because it is being used on the JSON response right? I'm sure I heard on a tutorial about a certain component way in Vue to work with <head> elements but this will do just fine.

            – Gracie
            Nov 13 '18 at 14:49














          0












          0








          0







          Since what you want to change is outside the area controlled by Vue, you just use ordinary DOM manipulation. It would be something like



          created() {
          fetch('test.json')
          .then(resp => resp.json())
          .then(items => {
          this.items = items;
          const descEl = document.querySelector('head meta[name="description"]');
          const titleEl = document.querySelector('head title');

          descEl.setAttribute('content', items[0]['meta-desc']);
          titleEl.textContent = items[0]['meta-title'];
          })
          }





          share|improve this answer













          Since what you want to change is outside the area controlled by Vue, you just use ordinary DOM manipulation. It would be something like



          created() {
          fetch('test.json')
          .then(resp => resp.json())
          .then(items => {
          this.items = items;
          const descEl = document.querySelector('head meta[name="description"]');
          const titleEl = document.querySelector('head title');

          descEl.setAttribute('content', items[0]['meta-desc']);
          titleEl.textContent = items[0]['meta-title'];
          })
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 13:39









          Roy JRoy J

          26.3k33158




          26.3k33158













          • Thanks Roy J. That's worked a treat - updated the site now. So this is just vanilla JS but within the Vue method because it is being used on the JSON response right? I'm sure I heard on a tutorial about a certain component way in Vue to work with <head> elements but this will do just fine.

            – Gracie
            Nov 13 '18 at 14:49



















          • Thanks Roy J. That's worked a treat - updated the site now. So this is just vanilla JS but within the Vue method because it is being used on the JSON response right? I'm sure I heard on a tutorial about a certain component way in Vue to work with <head> elements but this will do just fine.

            – Gracie
            Nov 13 '18 at 14:49

















          Thanks Roy J. That's worked a treat - updated the site now. So this is just vanilla JS but within the Vue method because it is being used on the JSON response right? I'm sure I heard on a tutorial about a certain component way in Vue to work with <head> elements but this will do just fine.

          – Gracie
          Nov 13 '18 at 14:49





          Thanks Roy J. That's worked a treat - updated the site now. So this is just vanilla JS but within the Vue method because it is being used on the JSON response right? I'm sure I heard on a tutorial about a certain component way in Vue to work with <head> elements but this will do just fine.

          – Gracie
          Nov 13 '18 at 14:49


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282040%2fchange-meta-title-and-description-using-vue-js%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues