How to manage page navigation in ajax based MVC.NET Web Applications - ajax

When we don't use ajax, browser's backward and forward arrows provide navigation to previous (and next) pages. Although when we use ajax to retrieve information (to have a single page application), we loose previous pages and browser does not know anything about the history of navigation.
What is the best solution for managing the history of previous pages when we use ajax specially in MVC.NET web applications context.
Thanks

The history api of html5 is the key. See:
https://css-tricks.com/using-the-html5-history-api/

Related

What are full page reloads and Why did we need to do full page reloads without ajax?

I was reading up on ajax and how it empowers us to exchange data with a server behind the scenes and consequently avoid full page reloads. My confusion lies here, I don't really understand what full-page reloads mean. I think it's probably cause I've been working with ajax/react since the start I guess and have not really seen any webpage of mine fully reload when I access stuff from a database or an api.
It'd be great if someone could explain what they are and why did we need them before ajax?
A full page load is where the entire page is downloaded from the server. A page typically consists of several sections: header, footer, navigation, and content. In a classic web application without AJAX, a user clicks on a link to another page, and has to download the full page, even though only the main content is changing. The header, footer, and navigation all get downloaded again even though they don't change.
With AJAX there is the opportunity to only change the parts of the page that will change. When a user clicks on the link, JavaScript loads just the content for that link and inserts it into the current page. The header, footer, and navigation don't need to reload.
This introduces other problems that need attention.
When AJAX inserts new content into the page, the URL doesn't change. That makes it difficult for users to bookmark or link to specific content. Well written AJAX applications use history.pushState() to update the URL when loading content via AJAX.
There are then two paths to get to every piece of content. Users can either load the URL containing that content directly, or load the content into some other page by following a link. Web developers need to test and ensure both work.
Search engines have trouble crawling AJAX powered sites. For best compatibility, you need to employ server side rendering (SSR) or pre-rendering to serve initial content on a page load that doesn't require JavaScript.
Even for Googlebot (which executes JavaScript) care must be taken to make an AJAX powered site crawlable. Googlebot doesn't simulate user actions like clicking, scrolling, hovering, or moving the mouse.
Content needs to appear on page load without any user interaction
You must use <a href=...> links for navigation so that Googlebot can find other pages by scanning the document object model (DOM). For users, JavaScript can intercept clicks on those links and prevent a full page load by using return false from the onclick handler or event.preventDefault() in the click handler.

Does a pages URL change with AJAX?

I'm taking an online course from a site that does not provide for asking questions about the content, so I am asking you.
This is one of the MUSTS for page titles:
"On a so-called single-page application — in which AJAX is used to bring in new content without refreshing or loading the entire web page — any time that the URL changes, the page title should be updated accordingly"
I always thought that if you used ajax to change part of a page the page itself would remain static. That is why would a URL change with an AJAX call. Are there any examples showing what this looks like?
AJAX requests don’t change the browser URL or history. To mimic the behaviour of traditional websites, single page apps will modify the URL and history and use the URL to change the view.
For more on changing the browser history, see Adding and modifying History entries.

NodeJS MVC framework with partial ajax-loaded views

I'm looking for a NodeJS MVC framework that allows rendering partial views on client side using Ajax (without whole page refreshing).
As far as I know, you can declare partial views on server with almost any Framework (Express, Sails...), but this will lead to refresh the whole page even if there's only a small portion of your page that really changes. This problem doesn't exist with a SPA Framework (this one would just load the partial html file in a container via ajax).
I believe Microsoft ASP.NET MVC was able to handle this case, by comparing the previous version of the page with the new requested page, and just returning the portion of the page that really changed. This may be time and CPU-consuming but it just works.
Is any Node MVC Framework managing a similar thing today ? Or is it mandatory to use an SPA Framework when a reactive user interface is required (without any whole page refresh) ?
Thanks in advance !
sails.js! It supports partials as you requested. You don't need to refresh the page, if you send ajax-request or handle the stuff via websockets dynamically.

What are my options for changing the querystring on a URL, and updating browser history?

Is there any way I can change the URL or add more history to the "back button" without having to refresh the entire page?
My application is AJAX based and I'd like to add some "undo" events to history so that the user can simply hit back and retain the old values.
What's possible today? I hear some of this may be in HTML5 but haven't checked whats supported in current browsers.
I think you can use window.location.hash to track the #part of the page, in your case, #state1, #state2 and so on.
window.location.hash = '#state' + (++ stateN) to set and
stateN = parseInt(window.location.hash.match(/\d+$/)[0])
See On - window.location.hash - Change? for more details about how to detect location hash changes.
You could use 301 redirection. Personally, I would use cookies on the client side, or sessions in your back end, to store the breadcrumbs. Storing state information in the URL is a bad idea for AJAX applications, because people might return to a url that the server side is not in the right state to respond to.
Another option would be to provide your own Back button that knows which page to go back to.
The answer for this question will be more or less the same as my answers for these questions:
How to show Ajax requests in URL?
How does Gmail handle back/forward in rich JavaScript?
In summary, two projects that you'll probably want to look at which explain the whole hashchange process and using it with ajax are:
jQuery History (using hashes to manage your pages state and bind to changes to update your page).
jQuery Ajaxy (ajax extension for jQuery History, to allow for complete ajax websites while being completely unobtrusive and gracefully degradable).
It is possible to use ASP.NET's built in Script Manager to update the browser's history. A full how-to to do this is located here:
http://www.asp.net/aspnet-in-net-35-sp1/videos/introduction-to-aspnet-ajax-history

MVC - How to develop a wizard like control functionality in ajax

I thought I would ask here about how to develop a wiazrd like control
in ajax for MVC before I started it.
The control will have pages that have data entry controls and
on each page submission insert data into a database.
Any ideas how to go about this?
Malcolm
Are you using a JavaScript library? I know dojo has a wizard in dojox namespace, and I'm pretty sure there are wizards for jquery. If not, I'd do a wizard just like I would tabs, without the tab bar, and each view contains a form and next, previous, cancel and or restart buttons, all of which would handle the ajax calls/state management and change the view.
If it's a wizard you probably actually should use separate web pages rather than having a single page that's updated through ajax calls, that way the forward and back buttons can work as nature intended in the browser for moving forward and back through the wizard.
Of course you can use fragment tricks to get forward / back working in a single page but in my experience this is tricky.

Resources