admin管理员组文章数量:1321250
I can't manage to link my Javascript function to my 'onclick' html attribute in the span tag.
I keep getting the error:
"Uncaught ReferenceError: closeAndRefresh is not defined" "at HTMLSpanElement.onclick"
My code looks like this:
<div class="error">
<div id="errorMessage">
<span onclick="closeAndRefresh();">✖</span>
<p id="message">
<?php echo $confirmationMessage ?>
</p>
</div>
</div>
<script>
function closeAndRefresh() {
<?php $visibility = 'hidden' ?>
window.location.reload(true);
}
</script>
I'll also add a screenshot of my code:
I can't manage to link my Javascript function to my 'onclick' html attribute in the span tag.
I keep getting the error:
"Uncaught ReferenceError: closeAndRefresh is not defined" "at HTMLSpanElement.onclick"
My code looks like this:
<div class="error">
<div id="errorMessage">
<span onclick="closeAndRefresh();">✖</span>
<p id="message">
<?php echo $confirmationMessage ?>
</p>
</div>
</div>
<script>
function closeAndRefresh() {
<?php $visibility = 'hidden' ?>
window.location.reload(true);
}
</script>
I'll also add a screenshot of my code:
Share Improve this question edited Jun 5, 2021 at 15:23 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 10, 2017 at 11:50 ItsShowtimeItsShowtime 1932 gold badges2 silver badges11 bronze badges 7- Ofcourse that lower than sign was not the problem ;) – ItsShowtime Commented Apr 10, 2017 at 11:52
- Did you try putting the inline script above the HTML in the web page? – GavinBrelstaff Commented Apr 10, 2017 at 11:54
- try to wrap your function inside window onload event or better use event listener rather than on click – Vinod Louis Commented Apr 10, 2017 at 11:56
- @MaximDelaet Include your script before your HTML code as your function should be defined before its reference in html – Gaurav Chaudhary Commented Apr 10, 2017 at 11:59
- I think there is some syntax issue with the first line in the function. when i mented out that line, it worked fine. – iqbal Commented Apr 10, 2017 at 12:03
4 Answers
Reset to default 4Your script is after your HTML. Include it before html so that your function is defined
function closeAndRefresh(){
window.location.reload(true);
}
<span onclick="closeAndRefresh();">✖</span>
If you have recently added the function to your javascript code, consider clearing the browser's cache, in order to force it reload the javascript code. To check if this is the problem, on your browser show the source html code, find the line where the javascript code is included, and click on it; it will show you the javascript source code the browser is currently using, that may be out of date. Also, as mentioned by others, make sure you include the javascript code before you reference it.
Try writing the script function closeAndRefresh()
in the head tag of your html page.
Maybe the html page is not able to load the script when you write it inside the body.
Make sure you reference the function correctly and also try putting the script above or below as it seems to be function
not recognized.
本文标签: htmlonclick attribute can39t find Javascript functionStack Overflow
版权声明:本文标题:html - onclick attribute can't find Javascript function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742094602a2420469.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论