admin管理员组

文章数量:1302981

Here is the Bin. I am trying to make an example of reloading the page by using location.reload() in the function refresh() and using onclick='refresh()' in a button. For some reason the page is not reloading.

The JS function:

function refresh() {
    location.reload();
}

The HTML button:

<button onclick='refresh()'>Try Refreshing!</button>

Here is the Bin. I am trying to make an example of reloading the page by using location.reload() in the function refresh() and using onclick='refresh()' in a button. For some reason the page is not reloading.

The JS function:

function refresh() {
    location.reload();
}

The HTML button:

<button onclick='refresh()'>Try Refreshing!</button>
Share asked Sep 1, 2013 at 15:19 Joe PigottJoe Pigott 8,4697 gold badges33 silver badges43 bronze badges 1
  • You cannot access the function. Its out of scope. – CharliePrynn Commented Sep 1, 2013 at 15:22
Add a ment  | 

4 Answers 4

Reset to default 5

Your refresh method is not in the global scope - it's enclosed within your document.ready.

Move it out from document.ready into it's own script tag.

<script>
    function refresh() {
        location.reload();
    }
</script>

The edited bin

The refresh() function isn't in the global scope. In order to make the function globally accessible, you can do this:

window.refresh = function() {
    location.reload();
};

Your function is defined inside the

$(document).ready(function () {
    ...
});

body, so the scope of the function name is just that body. As a result, you can't reference it from inline onXXX attributes.

Either associate it with the element using

$("#buttonid").click(refresh);

or define the function outside the document ready function.

you can do directly as

<button onclick="window.location.reload()">Try refreshing!</button>

本文标签: htmlSimple javascript reload not workingStack Overflow