Jump to content

'#' In Urls?


MrFish
 Share

Recommended Posts

I like to include ajax in my site whenever possible. This has its benefits but I've realized a major problem it causes. If you include a variety of pages into one page, you disable the back button and the back button is one of the most used features of the browser. I've seen some sites using the pound symbol (#) to automatically move to anchor points, can it be used for javascript and php?

Edited by MrFish
Link to comment
Share on other sites

The # symbol is used in links to refer to an element with a specific id. For example:<a href="#main">main</a>Will take you to,<div id="main"> ... </div>I don't know about implementing it with plain JavaScript or PHP, but you can easily use jQuery to import only specific elements by referencing their ids (or classes, actually). For example, to load the main div from one page and import it into the main div in the current page when you click on a button, you could do:

$(document).ready(function() {$('#id_of_button').click(function() {$('#main_div_of_local_document').load('filename.html #main_div_of_remove_document');return false; //this line only if you use an anchor to start things off, to stop the link going anywhere});});

The thing about this is it will end up with two divs with id 'main'. You would probably change the selector for the load function to #main #content, or some other wrapper for the content in the main id.

Link to comment
Share on other sites

Note - in this case it is the "hash" symbol, and yes, going to page#someid will cause the browser to scroll to the element with id="someid" (however changing it after the page loads won't cause a jump). By the way, you can also artificially insert entries into the browser's history.

Edited by Synook
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...