Omar Jandali
Omar Jandali

Reputation: 824

Reload or Refresh a page using Jquery from local machine

I have a html document on my local machine, I created a button using JQuery and set an on click for the button. When the button is clicked, I want the current index.html page refresh.

This following is in the script tags:

    var refreshButton = '<button id="refresh"> Refresh </button>';
    $body.append(refreshButton);
    $('#refresh').click(function(){
      refresh(forceGet);
    });

i tried, location.refresh(); & window.location.refresh(); & refresh(); but they didnt work.

Upvotes: 0

Views: 17176

Answers (2)

TechLemur
TechLemur

Reputation: 36

$body.append(refreshButton);

needs to be

$('body').append(refreshButton);

and window.location.reload(true); should work. Try this:

$( document ).ready(function() {
   $('body').append('<button id="refresh"> Refresh </button>');
   $('#refresh').click(function(){
      window.location.reload(true);
   });
});

Upvotes: 2

Jim Wright
Jim Wright

Reputation: 6058

You need to use window.location.reload().

$('document').ready(() => {
    $('#last-date').html(new Date())
    $('#refresh').on('click', () => {
        console.log('Refreshing...')
        window.location.reload(true)
    })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button id="refresh">Refresh</button>
<p>Last loaded at <span id="last-date"></span></p>

Upvotes: 1

Related Questions