[英]How to dynamically update Meta tags (OG) using JavaScript
This is my first question here in the Stack community.这是我在 Stack 社区的第一个问题。 I know it has been asked earlier, but I wanted to know how to do it specifically just using JS (I'm not a coder by education).
我知道之前有人问过它,但我想知道如何专门使用 JS 来做到这一点(我不是受过教育的编码员)。
This is something what I am trying to do:这是我正在尝试做的事情:
var pageTitle = document.getElementsByTagName('h1'); document.getElementById("demo").innerHTML = pageTitle[0].innerHTML;
<html> <head> <title>Update meta tag dynamically</title> <meta property="og:title" content="id=demo"/> </head> <body> <h1>First header</h1> </body> </html>
I know the that's not how to use tags, just to show you what I want to do.我知道这不是如何使用标签,只是为了向您展示我想要做什么。
PS: We can't run anything at the back-end, so it has to be in the code.
PS:我们不能在后端运行任何东西,所以它必须在代码中。
This is something else that I found, but it doesn't work either:这是我发现的其他东西,但它也不起作用:
<meta name="description" content="Old">
<h1>New</h1>
<script type='text/javascript'>
$(document).ready(function() {
var metaTitle = document.getElementsByTagName('h1');
// Just replacing the value of the 'content' attribute will not work.
$('meta[name=description]').remove();
$('head').append( '<meta name="description" content"$metaTitle">' );
});
</script>
Finally, cracked the code.最后,破解密码。 Here it is for any future wanderers: https://github.com/idaljot/meta-tag-auto-update/blob/master/meta-tag.htm
这是任何未来的流浪者: https : //github.com/idaljot/meta-tag-auto-update/blob/master/meta-tag.htm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.