简体   繁体   中英

Using a variable in a Blogger JSON-LD template

I'm refering to the question JSON-LD: Using data:post.body in Blogger template .

Surprisingly, <data:post.body> can be used in the meantime. But now I have a new problem: How to eliminate tags, linefeeds and special characters and replace characters like ' and " ?

I found something in the kind of the following instead of directly write JSON-LD code, but it does not work:

<script type='text/javascript'>
  var thisDdoesNotWork = removeHtmlTags_And_ChangeSomeCharacters("<data:post.body/>");
  var el = document.createElement('script');
  el.type = 'application/ld+json';
  el.text = JSON.stringify({
    "v1":"thisWorks",
    "v2":"<data:post.thisWorksToo/>",
    "v3":thisDdoesNotWork});
  document.querySelector('head').appendChild(el);

Someone an idea?

Firstly render the content somewhere in the HTML via

<div class='post-body'>
    <data:post.body/>
</div>

Then, change your code as follows -

el.text = JSON.stringify({
"v1":"thisWorks",
"v2":"<data:post.thisWorksToo/>",
"v3":document.querySelector('.post-body').textContent});

You can also use innerText property (Refer to this question to know the difference between innerText and textContent )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM