Oskar Kjellin
Oskar Kjellin

Reputation: 21880

Asp.net MVC with Ajax history (jquery address), how to load from the URL?

I am using asp.net mvc with ajax navigation. I use jquery address and I can change the address bar to be like "MYPage.Com/#/Url", but how can I invoke my route when the user enters that link?

This has probably been asked before but I could not find it, so please point me to it if you find it.

Upvotes: 0

Views: 1187

Answers (2)

nsdiv
nsdiv

Reputation: 983

You need to use the window.onHashChange event of the window element. It is best to use javascript libraries like jquery bbq to handle the hash change.

If you still want to do it without using a library, then on page load you should make a call to the function that handles the onHashChange even.

Upvotes: 2

Alex
Alex

Reputation: 14618

There is no event for that (at least not the last time I've checked). You need to make a checker function in JS that will run once every 100ms for example (or more often).

var currentHash="";
function CheckHash()
{
    if(currentHash!=window.location.hash)
    {
        currentHash=window.location.hash;
        NavigateTo(currentHash); //or whatever code to execute when address behind `#` changes
    }
}

CheckHash(); //Initial Run, for fast reaction on load
window.setInterval(CheckHash,100); //schedules the function to run once every 100ms

Upvotes: 1

Related Questions