I have a button on index page (index.php) that when you click it, it should take you to other page (login.php) without reloading the page simultaneously. Below is javascript with pushState.
document.getElementById('cta_btn').onclick = function() {
window.history.pushState('', '', 'login.php');
};
The problem is that I stay on same page (index.php) only url changes /login.php
pushState is a way of saying "I am using JavaScript to modify the DOM of this page so that it becomes the same as what you would get if you requested this URL". You don't appear to have written the JS to modify the DOM.
It doesn't cause new content to load by itself. It doesn't really navigate anywhere. It just pretends to so you can hook into popstate and change the DOM back to how it was before when the back button is pushed.
If you use pushState then you all need to change the DOM (with createElement and friends for simple cases and with frameworks like React for complex ones).
If you want to navigate to a new page, then assign a value to location.href.
If you don't want to load content from the server in order to do that navigation then make sure the user has previously visited the page and that caching headers were sent then.
I guess State is a Object, You should try this...
window.history.pushState({}, '', 'login.php');
Related
I am writing a site on asp net core and all pages in it are presented in the form of partialview. Thus, I achieve a spa model without using angular and other frameworks. When switching partialview, I can change the url of the page using
window.history.pushState ("object or string", "Title", "/ new url");
But when I switch back in the browser, my url just changes to the previous one, and partialview does not change. I want to intercept this transition and call the desired presentation. Is it possible?
I think window.onpopstate or window.onhashchange would be useful. In your case you're creating history by pushstate which fires the former one when this entry has changed or replaced.
I'm a beginner, hope some help to understand these questions, thanks~
For web pages like youtube.com, it uses ajax + history:
does a "pushState" reload the page?
if I injected some java scripts into this web page, will DOM Element I injected be removed or replaced? when will this happen?
if this happens, how to re-inject DOM element to make it displayed, or how to prevent the DOM element from being removed...?
does a "pushState" reload the page
No. It changes the URL and stores some data (which you pass to it as an argument) in the history. It doesn't change the page at all.
You are expected to use pushState when you write other code that changes the state of the page. (And you should change the state of the page so it is the same as if you freshly loaded the page at the URL you passed to pushState).
Then you are expected to change the state of the page back to host it was before (using the stored data) when you get a popState event.
I'm building a little CoffeeScript application with 10 buttons and a container (simple). When the user press on one of the button : the container change.
The buttons look like a navbar and instead of using links (that will reload the entire page), I used javascript (Coffeescript, jquery or whatever) to change the content of the page (with some Ajax query to load data).
The problem is that the back and forward button of the browser can't work with that solution... and I need to find a solution for that. Routing maybe ?
I really like the way Asana.com resolved this issue: actually the address change but the content seems not to be entirely reloaded.
What do you suggest ? Thanks for the help
Hashes. The simplest solution is to define an URL hash every time the user clicks on a button. For example:
location.href = "#" + button.id;
With that, you create a history entry, and the user can press back or forward in the browser.
But how can you check when this happens? There's the hashchange event:
window.onhashchange = function() {
var state = location.hash.substring(1); // chomps the initial #
...
};
Basing your code on the state variable, you can trigger your AJAX calls from there.
By the way, you can change your code altogether, using links instead of buttons with an hash as the href property, which does not reload the page, but creates an history entry and fires the hashchange event.
The hashchange event is supported by every modern browser (that support history.pushState too, a more flexible and powerful way to control your history) and IE8-9.
What happens when a JQUERY MOBILE page is refreshed?
I use ajax to fetch data in a variable called "json" on page1, when user clicks a particular dynamically generated element, i store the id of clicked element in a session variable and changePage() to a new JQM page2 where i use json.thepropertyiwant to generate a list, everything works fine, even back and forward buttons work perfectly however if i refresh page2
then json.thepropertyiwant becomes undefined here is the error i get:
Uncaught TypeError: Cannot read property 'responseData' of undefined
i am using a multipage in a single html5 page model
edit:
I have used variable name json and not JSON i just typed it like
that to emphasize, however now i think that was foolish!
I have also figured my problem. My mistake was that i was assuming
page refresh would call pageinit for the page i am on but it works
no differently than a normal html page refresh and triggers
document.ready each time and then pageinit for the page i am on.
Is there a way to listen to a
pagerefresh event and override its normal functionality?
I believe that you generate the dynamic content of page2 in some event.
I believe that the event is pageinit or pagecreate. Those events fire only once (if you have ajax page load set to true).
What happens is that you request the data when you load page1 (something like pageinit?)
When you refresh page2, the data is not there because the event that you request your data in, is not firing, which is completely normal, because when you refresh page2, page1 has nothing to do with it. Post the section of your code where you request your data, and the section where you generate the dynamic content for page2 for more.
Hmm... there's a few issues going on here.
For starters, don't use a variable called 'JSON'. That's no good - there's already a global object called JSON which is used for parsing and encoding JSON! You could accidentally overwrite it.
As you discovered, if you store data in a variable in javascript - that variable is only in memory for the current page. If you refresh the page, then that variable will no longer be in memory.
But that's not really anything to do with jQuery Mobile. That's just how web browsers work - javascript structures created in-memory are not persisted between page refreshes.
Generally you need to save the data yourself using localStorage or cookies for it to persist between page refreshes.
Typically you don't refresh a jQuery Mobile app anyway. So the previous point is generally not too much of an issue. A user refreshing the page is like rebooting the app.
The app should when it starts reload any data that it needs, i.e.
from localStorage, from cookies, or make some new ajax calls to
reload. You would have to program that logic in.
I want to change html without reload. I do it like:
$('#left_menu_item').click(function(e) {
if (!!(window.history && history.pushState)) {
e.preventDefault();
history.pushState(null, null, newUrl);
}
});
It works correctly. But if I want to go back with "Back" button - browser change url on previous, but it not reload page. Why?
this behaviour is expected and is in accordance with the specifications of manipulating the history stack.
this is a relatively complex problem to explain. but in short think of it as this: any history entry the user pushes on the history stack (using pushState etc) doesn't merit a page load when you move from it because it is considered a fake (user generated) history entry.
why?
this behaviour is a good thing and is consistent with the intent of giving the developer more control over the page without being forced to reload it (think of it like ajax: you can do things that were previously only possible by page reloading like fetching data but now you can do it without reloading the page using the XMLHttpRequest object).. if you want to mimic the behaviour of reloading the page when clicking the back button.. you can simply call location.reload() when you handle the window.onpopstate event
how?
this may be outside the scope of your question but i just wanted to put it there to describe what we're talking about
let me explain by using an existing example here (excerpted text will be italicised):
Suppose http://mozilla.org/foo.html executes the following JavaScript:
var stateObj = { foo: "bar" };
history.pushState(stateObj, "page 2", "bar.html");
This will cause the URL bar to display http://mozilla.org/bar.html, but won't cause the browser to load bar.html or even check that bar.html exists.
think of it as that you are creating an entry in the history stack that is not associated with an actual page load.. rather a 'fake' page load (ie you are just using javascript to manipulate the dom and insert html)..
Suppose now that the user now navigates to http://google.com, then clicks back. At this point, the URL bar will display http://mozilla.org/bar.html, and the page will get a popstate event whose state object contains a copy of stateObj. The page itself will look like foo.html, although the page might modify its contents during the popstate event.
the point here is that bar.html is a fake history entry that sits on top of the original http://mozilla.org/foo.html.. so you will see on the url http://mozilla.org/bar.html but the contents will belong to foo (in this example notice that we didnt manipulate the content of the dom when we pushed bar.html.. if we did like in your example.. then that content will also show up). the key thing here is that the page reloads!.. because we are serving a page that has a genuin entry on the history stack (even if on the url.. we are displaying a url that is associated with a fake entry on the history stack).
also separate this discussion from the page manually handling the popstate event.. that's a different story and will just complicate things.
If we click back again, the URL will change to http://mozilla.org/foo.html, and the document will get another popstate event, this time with a null state object. Here too, going back doesn't change the document's contents from what they were in the previous step, although the document might update its contents manually upon receiving the popstate event.
here.. the page will not load!.. that's because we are making the transfer from a fake history stack entry to the real one (and the real one was already loaded in the previous step.. so the page reloaded and that's it).
that's it for the example. the concept is kind of hard to explain and i encourage you to test your code by clicking through a combination of real and fake pages and you will see a pattern of when the page actually loads and when it doesn't..
window.onpopstate = function(event) {
if(event && event.state) {
location.reload();
}
}
This is what I use :)