最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Change meta tag content dynamically through javascript - Stack Overflow

programmeradmin1浏览0评论

I want to change the meta tag content i.e. refresh rate and url dynamically using javascript. Using a button to enable javascript function. Tried 3 alternatives but not working. Please help.

Thanks,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=");-->
    <!--document.getElementById("mymetatag").setAttribute("content", "5;URL=");-->

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

I want to change the meta tag content i.e. refresh rate and url dynamically using javascript. Using a button to enable javascript function. Tried 3 alternatives but not working. Please help.

Thanks,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>
Share Improve this question asked Sep 13, 2017 at 8:06 AmreshAmresh 3831 gold badge6 silver badges16 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

This works for me. The issue could have been that you tried a first code which did not work and you mented the code with HTML ments <!-- [...] -->instead of Javascript ments: // [...] or /** [...] */.

<!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>

I looks like you misuse the functionality of metatags, JS doesn't save the state between requests. And, BTW, you can do reload/redirect using the javascript itself.

发布评论

评论列表(0)

  1. 暂无评论