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

html - onclick attribute can't find Javascript function - Stack Overflow

programmeradmin4浏览0评论

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();">&#10006;</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();">&#10006;</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
 |  Show 2 more ments

4 Answers 4

Reset to default 4

Your script is after your HTML. Include it before html so that your function is defined

function closeAndRefresh(){
  
        window.location.reload(true);
}
<span onclick="closeAndRefresh();">&#10006;</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.

发布评论

评论列表(0)

  1. 暂无评论