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 yourpackage.json
– Hao Wu Commented Jan 14, 2021 at 4:58
2 Answers
Reset to default 5no-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>