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?
Asked
Active
Viewed 2,864 times
2
-
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
-
1Take 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 Answers
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
-
-
-
Ok thanks! Functions onClick eventhough I have an ng-click in side this button? – Daniel Sep 28 '16 at 10:41
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
-
-
Yes you can use id of button or write it document.ready function – RAUSHAN KUMAR Sep 28 '16 at 10:18
-
I tried. If I have the same page in another browser this remains still with the same data, and not updated until you make a reload. But I want to reload by itself – Daniel Sep 28 '16 at 10:25
-
What if I want to reload another page ? So I have thgis item #something which reloads another page. How to reload another page? – Daniel Sep 28 '16 at 10:38
-
-
No this takes me to another page. But If I have this page operned in another pc or browser this page doesnt reload if the data are submited bu another pc.Right? – Daniel Sep 28 '16 at 10:55