2

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?

kangax
  • 38,898
  • 13
  • 99
  • 135
johnlikesit
  • 141
  • 1
  • 3
  • 10
  • Short answer yes; long answer, you need to explain what you what in more detail. – ijw Sep 15 '09 at 08:43
  • my page does load content with jquery ajax load(). SO i've got an index page with a div container in which all content is loaded from an external file. If i want to go directly into one menupoint i have to call a js function to load the content. ANd i want to go directly to an menupoint by accessing it through www.mydomain.com/submenu1 (for example) :) – johnlikesit Sep 15 '09 at 09:05

1 Answers1

1

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

Community
  • 1
  • 1
Blixt
  • 49,547
  • 13
  • 120
  • 153