Show content from DIV in a META-tag

ⅰ亾dé卋堺 提交于 2019-12-12 01:22:58

问题


I have a div which is like this:

            <div id="beskrivelse" class="description html_content" itemprop="description">
                {{description}}
            </div>

The content {{description}} is made in the backend.

How can I make it so that this content will be shown in my meta here:

            <meta property="og:description" content=" HERE "/>

I get a syntax error if I write:

            <meta property="og:description" content=" {{description}} "/>

Is ther a simple way to do it? Maybe put it in a variable of some sort?

There is no way for me to change this in the backend as I am using a closed system, Tictail.com. I want what I have written in my {{description}} to be shown in the meta tag.

I cannot use any php, asp (etc) code as the site doesnt allow this.

Any suggestions?


I have tried this with no succes:

    <script language="javascript" type="text/javascript">  
    var descval = document.getElementById('beskrivelse').innerHTML;
    var paras = document.getElementsByTagName('meta');
    for (i = 0; i < paras.length; i++) {
        var test = paras[i].getAttribute('property');
        if(test == "og:description")
        {
           paras[i].content = descval;
        }
    }
    </script>

回答1:


Your code should work, given that you already have a meta-element in you DOM with a property og:description and that you run you script either at the bottom of your page, or in a DOM-ready callback. As it does in your Fiddle, if you set the JS to be run onDomready (as I did here).

If you don't have the meta-element in the DOM from the beginning, you might need to create it and append it to the DOM. Something like this:

<script>
// Get the content
var descval = document.getElementById('beskrivelse').innerHTML;
// Create the new meta-element
var elm = document.createElement("meta");
elm.setAttribute("property", "og:description") 
elm.setAttribute("content", descval);
// Append the element to the HEAD
document.getElementsByTagName("head")[0].appendChild(elm);
</script>


来源:https://stackoverflow.com/questions/24360615/show-content-from-div-in-a-meta-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!