通过javascript动态更改元标记内容

问题描述:

我想要使用javascript动态更改元标记内容,即刷新率和网址。使用一个按钮来启用JavaScript功能。尝试了3种选择,但没有工作。请帮忙。通过javascript动态更改元标记内容

感谢,Amresh

<!DOCTYPE html> 
<html> 
<head> 
<meta HTTP-EQUIV="refresh" name="description" id="mymetatag" 
     content="5;URL=http://localhost:6985/ChartJSDemo/Is_Mainpage.html"> 
<meta charset="ISO-8859-1"> 

<title>Processing Details</title> 
<link rel="stylesheet" type="text/css" href="css/MF_job_failTable.css"> 
</head> 

<body> 

<button onclick="myFunction()">Click me</button> 

<script> 
function myFunction() { 
    <!--document.querySelector('meta[name="description"]').setAttribute("content","5;URL=http://google.co.in");--> 
    <!--document.getElementById("mymetatag").setAttribute("content", "5;URL=http://google.co.in");--> 

    var m = document.createElement('meta'); 
    m.name = 'description'; 
    m.id = 'mymetatag'; 
    m.content = '5;URL=http://google.co.in'; 
    m.HTTP-EQUIV= 'refresh'; 
    document.head.appendChild(m); 
} 
</script> 

这适用于我。 这个问题可能是因为您尝试了第一个没有工作的代码,并且您使用HTML注释<!-- [...] -->而不是Javascript评论注释了代码:// [...]/** [...] */

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <meta HTTP-EQUIV="refresh" name="description" id="mymetatag" content="5;URL=http://localhost:6985/ChartJSDemo/Is_Mainpage.html"> 
 
    <meta charset="ISO-8859-1"> 
 

 
    <title>Processing Details</title> 
 
    <link rel="stylesheet" type="text/css" href="css/MF_job_failTable.css"> 
 
</head> 
 

 
<body> 
 

 
    <button onclick="myFunction()">Click me</button> 
 

 
    <script> 
 
    function myFunction() { 
 
     document.getElementById("mymetatag").setAttribute("content", "5;URL=http://google.co.in"); 
 
    } 
 
    </script> 
 
</body> 
 
</html>

+0

是的,这是工作在Web浏览器中。我尝试在eclipse tomcat服务器上,它不工作。多谢兄弟。 – Amresh

我看起来像你滥用元标记的功能,JS不保存请求之间的状态。而且,顺便说一下,你可以使用JavaScript本身来重新加载/重定向。