homepage Welcome to WebmasterWorld Guest from 54.167.75.155
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Move page to id using javascript
itledi




msg:3589088
 3:10 am on Mar 2, 2008 (gmt 0)

I have a lot of links to different sections id's on one of my web pages.

I'm using the the following format: index.html#hash1, index.html#hash2, etc...

My only complaint is that it changes the address bar to a different (longer) url.

I don't want people to be confused by this, and I don't want people to copy this longer address to link to me by either.

What I would like to do is possibly use javascript on these links, to return false, but for the browser to still move the page down to the right id.

It would graciously default for users who don't have javascript, by working how it's working now.

Is this possible?

 

gergoe




msg:3591790
 11:26 am on Mar 5, 2008 (gmt 0)

You can put a javascript code into the onclick event handlers for these links, and that code would scroll the page to the right position. To be an easy one, I'd suggest you to assign the same names to the ID attributes as you use in the NAME attributes already, then you only need something like document.getElementById('LinkName').scrollTop (if I'm not mistaken) to get the absolute top position of that anchor.

fside




msg:3592464
 12:50 am on Mar 6, 2008 (gmt 0)

I'm not sure that works in IE 6. You might just have to stick with an anchor NAME, in that case.

penders




msg:3592980
 2:43 pm on Mar 6, 2008 (gmt 0)

You can get this to work with JS, although you'll have to write a bit more code to get this to work cross browser. Find your current Y position, find the position of your destination element and scrollTo(). With this method you can then animate the scrolling - make it smooth, which can potentially improve accessibility in providing a visual cue as to how far away the link actually is.

Google: "Make Internal Links Scroll Smoothly with JavaScript"

I don't want people to be confused by this, and I don't want people to copy this longer address to link to me by either.

If your hash's are reasonably short (which I suspect they are if they are internal links?) then I'd question whether this would confuse your users? Afterall this is standard browser behaviour. I would have thought that the ability to bookmark parts of a page is a feature since you are already providing a link to it? If I want to direct someone to a particular part of a page I would often view the source and look for a suitable ID to construct the URL - very handy.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved