如何使用javascript从标签元更改属性内容

时间:2022-10-27 11:55:21

I am trying to make a webpage refresh at a certain time, for this I will be doing some calculations using java script to define when the webpage must be refreshed.

我试图在某个时间刷新网页,为此我将使用java脚本进行一些计算,以定义何时必须刷新网页。

I am using this code but the content attribute for the meta tag is not changing.

我正在使用此代码,但元标记的content属性不会更改。

<script language="javascript"  type="text/javascript">


function myFunction() {

    document.getElementsByTagName('META')[0].getAttribute("content")="5";

}

    myFunction();
</script>

2 个解决方案

#1


You can do it like this in vanilla JS (the example changes the meta tag with the name "keywords")

您可以在vanilla JS中执行此操作(示例更改名为“keywords”的元标记)

document.getElementsByName("keywords")[0].setAttribute("content", "dynamic meta description");

But to solve your problem i would not use the header tag, but a javascript timer, that reloads the page

但要解决您的问题,我不会使用标头标签,而是重新加载页面的javascript计时器

location.reload(); 

#2


<!DOCTYPE html>
<html>
    <head>
        <META HTTP-EQUIV="Content-Language" charset="UTF-8">
        <meta http-equiv="refresh" content="30">
        <script type="text/javascript" src="http://localhost:8080/files/jquery.min.js"></script>
        <link rel="stylesheet" type="text/css" href="http://localhost:8080/files/bootstrap.css">
    </head>
    <body>
        <div>
            click <button type="button" onclick="refreshTime()">here</button> to change page refresh time.
        </div>

        <script>
            var refreshTime = function(){
                var time = Number(prompt('set refresh time',30));
                $("meta[http-equiv='refresh']").attr('content',time);
            }
        </script>
    </body>
</html>

#1


You can do it like this in vanilla JS (the example changes the meta tag with the name "keywords")

您可以在vanilla JS中执行此操作(示例更改名为“keywords”的元标记)

document.getElementsByName("keywords")[0].setAttribute("content", "dynamic meta description");

But to solve your problem i would not use the header tag, but a javascript timer, that reloads the page

但要解决您的问题,我不会使用标头标签,而是重新加载页面的javascript计时器

location.reload(); 

#2


<!DOCTYPE html>
<html>
    <head>
        <META HTTP-EQUIV="Content-Language" charset="UTF-8">
        <meta http-equiv="refresh" content="30">
        <script type="text/javascript" src="http://localhost:8080/files/jquery.min.js"></script>
        <link rel="stylesheet" type="text/css" href="http://localhost:8080/files/bootstrap.css">
    </head>
    <body>
        <div>
            click <button type="button" onclick="refreshTime()">here</button> to change page refresh time.
        </div>

        <script>
            var refreshTime = function(){
                var time = Number(prompt('set refresh time',30));
                $("meta[http-equiv='refresh']").attr('content',time);
            }
        </script>
    </body>
</html>