-1

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:

enter image description here

7
  • Ofcourse that lower than sign was not the problem ;) Commented Apr 10, 2017 at 11:52
  • Did you try putting the inline script above the HTML in the web page? 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 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 Commented Apr 10, 2017 at 11:59
  • I think there is some syntax issue with the first line in the function. when i commented out that line, it worked fine. Commented Apr 10, 2017 at 12:03

4 Answers 4

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>

Sign up to request clarification or add additional context in comments.

1 Comment

Including the <scipt> </script> tags before the html tags resolved this for me! Thanks dude. I always though the script tags go within the <head> tags, which is inherently inside the html tags... Learn something new every day (y)
1

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.

1 Comment

This one helped in my case. Thanks
0

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.

Comments

-1

Make sure you reference the function correctly and also try putting the script above or below as it seems to be function not recognized.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.