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

javascript - function is defined but never used - Stack Overflow

programmeradmin6浏览0评论

I am having trouble reloading the page. I tried to use JavaScript in Vue by adding this code

<body onload="myFunction()">

function myFunction() {
   window.location.reload()
}

This error is poppng up:

 74:10  error  'myFunction' is defined but never used  no-unused-vars

Any suggestions?

I am having trouble reloading the page. I tried to use JavaScript in Vue by adding this code

<body onload="myFunction()">

function myFunction() {
   window.location.reload()
}

This error is poppng up:

 74:10  error  'myFunction' is defined but never used  no-unused-vars

Any suggestions?

Share Improve this question edited Jan 14, 2021 at 5:00 Dan 63.2k18 gold badges111 silver badges119 bronze badges asked Jan 14, 2021 at 4:36 Jeremy WarrenJeremy Warren 231 gold badge2 silver badges6 bronze badges 4
  • I don't see your function enclosed inside script tag – Amaarockz Commented Jan 14, 2021 at 4:43
  • yes I have included my function in script tag – Jeremy Warren Commented Jan 14, 2021 at 4:48
  • <script> function myFunction() { window.location.reload() } </script> – Jeremy Warren Commented Jan 14, 2021 at 4:51
  • Add "no-unused-vars": "off" in the "rules" of "eslintConfig" in your package.json – Hao Wu Commented Jan 14, 2021 at 4:58
Add a ment  | 

2 Answers 2

Reset to default 5

no-unused-vars is a ESLint warning. It occurs because you don't call your function anywhere in your javascript code, and ESLint cannot read that it is being called from an HTML attribute.

You can turn off this warning like this:

<script>
/* eslint-disable no-unused-vars */
function myFunction() {
  window.location.reload()
}
/* eslint-enable no-unused-vars */
</script>

Although calling javascript functions from HTML attributes is not good practice today.

There is a javascript way to wait for the load event, instead of using onload attribute:

<script>
function myFunction() {
  window.location.reload()
}

document.addEventListener('DOMContentLoaded', myFunction);
</script>

Regarding the ment on the page reload only once. You can keep the fact that the page has been reloaded in the localStorage:

<script>
function myFunction() {
  // Check that the page has not been reloaded
  if (localStorage.getItem('reloaded') === null) {
    // Save the fact that we are reloading the page, and reload page
    localStorage.setItem('reloaded', true);
    window.location.reload();
  } else {
    // Otherwise, reset the flag so that on the fresh load the page can be reloaded again
    localStorage.removeItem('reloaded');
  }
}

document.addEventListener('DOMContentLoaded', myFunction);
</script>

please use script tag for function or any javascript related code.

<body onload="myFunction()">

<script type='text/javascript' charset='UTF-8'>
     function myFunction() {
          window.location.reload()
     }
</script>
发布评论

评论列表(0)

  1. 暂无评论