2

I have created a form which insert the location of some users in database and these data should appear in the page. The problem is that the page doesn't reload by itself after I fill the form but I have to make a page reload.Or if the page is opened in another browser it doesn't reload. Is there a way that this page reloads by itself without making any action after I insert some data in database from this form?

Daniel
  • 41
  • 1
  • 8
  • You don't need to reload the page, that's what [`ajax`](https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started) is for. But if you insist on reloading the page, then yeah, sure it can be done. you can use [`location.reload()`](https://developer.mozilla.org/en-US/docs/Web/API/Location/reload) – Andrei Sep 28 '16 at 10:13
  • @Andrew what ajax function should I use to not make the reload? – Daniel Sep 28 '16 at 10:18
  • The whole idea is to not reload. Page reloads are not visually appealing and depending on the net connection can be quite slow. Don't reload, update on the go. Make an ajax call, return whatever info you need and update the DOM without reloading the page. – Andrei Sep 28 '16 at 10:20
  • @Andrew any answer with the corresponding code to do that? I ll vote it up if functions. – Daniel Sep 28 '16 at 10:28
  • 1
    Take some time to read on how to do an ajax and how it works. It's much more important to understand than to copy+paste blindly. [See here](http://stackoverflow.com/questions/18614548/accessing-dom-object-after-ajax-call), [here](http://stackoverflow.com/questions/1510011/how-does-ajax-work). – Andrei Sep 28 '16 at 10:30
  • Use `location.reload()` in success block. – Atul Sharma Sep 28 '16 at 10:51

2 Answers2

2

Use the assign() method. The assign() method is supported in all major browsers.

window.location.assign(data); "data being the URL"

or

window.location.href

function myFunction() {
    location.assign("http://www.example.com");
}
<button onclick="myFunction()">Load new document</button>

Ok thanks! Functions onClick eventhough I have an ng-click in side this button?

Yes it will still function properly. See JSFiddle demo

Edit: I saw you had this in your code?

window.history.back(); or location.reload(); 

consider replacing with with:

window.location.replace("pagehere.html");
norcal johnny
  • 2,050
  • 2
  • 13
  • 17
1

You can refresh the page after your completing your operation using jquery as

Javascript 1.0

window.location.href = window.location.pathname + window.location.search +window.location.hash;
// creates a history entry

Javascript 1.1

window.location.replace(window.location.pathname + window.location.search + window.location.hash);
// does not create a history entry

Javascript 1.2

window.location.reload(false); 
RAUSHAN KUMAR
  • 5,846
  • 4
  • 34
  • 70