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

Change meta tag content dynamically through javascript

我想使用 javascript 动态更改元标记内容,即刷新率和 url。使用按钮启用 javascript 功能。尝试了 3 种替代方案但没有用。请帮忙

谢谢,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>

我看起来你滥用了元标记的功能,JS 不保存请求之间的状态。而且,顺便说一句,您可以使用 javascript 本身来执行 reload/redirect。

这对我有用。 问题可能是您尝试了第一个不起作用的代码,并且您使用 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>