0

Clicking on a div inside a sidebar I load a specific file inside a div.

<div id="menuLeft">
<div id="file01">File 01</div>
<div id="file02">File 02</div>
<div id="file03">File 03</div>
</div>

js

$("#menuLeft div").click(function () {
var id = "chapters/" + $(this).attr('id') + ".php";
$('#divRight').load(id);
});

This all works, but url is always the same - for example www.something.com

What I need by clicking on the sidebar is to get something like www.something.com#file01, or any other content, so when this url is entered in another window, file01 is automatically loaded inside divRight.

Any help?

4
  • When the page loads, check for location.hash. If it's set, then strip off the leading # and call .load(). Commented Feb 11, 2014 at 17:06
  • @RocketHazmat, thanks, I will try. Just a question: If I have another div with the same procedure, can I have two or three hashs inside url ? Commented Feb 11, 2014 at 17:21
  • You can make the hash value whatever you want. For example you can do #file01,file02. Then just remove the # and use .split(','). Commented Feb 11, 2014 at 17:22
  • @RocketHazmat, thanks once more. Solved, probably. Commented Feb 11, 2014 at 17:26

1 Answer 1

1

There are two things you need to do.

First, you need to change the hash:

document.location.hash = "file01";

And then secondly on page load, check the hash and load the appropriate file.

However, what's probably better is using HistoryAPI or a similar implementation. For instance HistoryJS will allow you to create states, so that people can press Back and Forward in the browser and switch between these pages.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.