Advertisement
nicolaslagios

Create FAQ google rich content with js

Jul 31st, 2023
3,494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.09 KB | Source Code | 0 0
  1. <script>
  2. //prwtes dyo erotisis
  3. var question1Title = document.querySelectorAll("h2")[0].innerText;
  4. var question1Answer = document.querySelectorAll("h2")[0].nextElementSibling.innerText;
  5. var question2Title = document.querySelectorAll("h2")[1].innerText;
  6. var question2Answer = document.querySelectorAll("h2")[1].nextElementSibling.innerText;
  7.  
  8. //dimiourgia tou javascript tag
  9. var scriptTag = document.createElement("script");
  10. scriptTag.type = "application/ld+json";
  11.  
  12. //periexomeno tou javascript tag
  13. scriptTag.textContent = JSON.stringify({
  14.     "@context": "https://schema.org",
  15.     "@type": "FAQPage",
  16.     "mainEntity": [
  17.       {
  18.         "@type": "Question",
  19.         "name": question1Title,
  20.         "acceptedAnswer": {
  21.           "@type": "Answer",
  22.           "text": question1Answer
  23.         }
  24.       },
  25.       {
  26.         "@type": "Question",
  27.         "name": question2Title,
  28.         "acceptedAnswer": {
  29.           "@type": "Answer",
  30.           "text": question2Answer
  31.         }
  32.       }
  33.     ]
  34.   });
  35.  
  36. //prosthiki tou script element sto kefali
  37. document.head.appendChild(scriptTag);
  38. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement