简体   繁体   中英

onclick attribute can't find Javascript function

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:

在此处输入图像描述

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>

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.

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.

确保您正确引用了该函数,并尝试将脚本放在上面或下面,因为它似乎无法识别function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM