johnlikesit
johnlikesit

Reputation: 141

Accessing URL executes JavaScript

I've coded an HTML page using jQuery for loading content. Now if I want to link directly to a submenu, is this possible to do with JavaScript?

So for example if someone goes to www.mydomain.com/submenu1/ then some JavaScript code will execute and load the needed contents?

Thanks a lot :)

Is it possible to realize that with htaccess?

Upvotes: 2

Views: 155

Answers (1)

Blixt
Blixt

Reputation: 50169

You will more likely want to have a URL structure that only needs a page to load from the server once, then the server is only queried by JavaScript XMLHttpRequests. Loading content based on a "hard" URL would be pointless, since you're doing a server request anyways and might as well return the content in the response.

For keeping addresses unique while still keeping the "hard" URL the same (preventing multiple server requests), you can use the hash/anchor part of the URL. This means your address might look something like this: http://www.example.com/#/submenu1/

The #/submenu1/ part stays on the client, so only / on www.example.com is requested. Then it's up to your JavaScript to load the content relevant to /submenu1/. See a page of mine for an example of this: http://blixt.org/js#project/hash?view=code

Also have a look at this question: Keeping history of hash/anchor changes in JavaScript

Upvotes: 1

Related Questions