views:

44

answers:

2

hi guys, i wonder if it is possible to create a bookmarklet to click on and the current webpage scrolls to the bottom!

javascript:function%20scrollme(){dh=document.body.scrollHeight;ch=document.body.clientHeight;if(dh>ch){moveme=dh-ch;window.scrollTo(0,moveme);}}

if i create a new bookmark and paste this as address nothing happens. I actually have no idea how to run javascript within a bookmarklet, however i just bookmarked the css-tricks Printliminator

maybe you could help, i would love to have a bookmarklet like this!

A: 

you can simply use an anchor with this syntax

<a name="label">Any content</a> 
and
<a href="#label">Any content</a> 
aauser
I'm pretty sure this is not what the original poster wants. He wants a bookmarklet that will jump to the body of any page.
Oren
I think the OP is asking for a JS solution if it will be used as a bookmarklet; he won't have control over the webpage.
Kevin Sedgley
right, i want a bookmarklet so i can jump to the bottom of every webpage! i can't place an anchor inside of website i visit. i just want to click a bookmarklet and jump to the bottom of the page.
A: 

First, your JavaScript only defines a function and does nothing else.

Second, you need to use document.documentElement (which represents the <html> element) instead of document.body:

javascript:dh=document.documentElement.scrollHeight;ch=document.documentElement.clientHeight;if(dh>ch){moveme=dh-ch;window.scrollTo(0,moveme);}

or, simply

javascript:window.scrollTo(0,document.documentElement.scrollHeight)

(apparently it doesn't matter if y-coord of window.scrollTo is greater than the maximum position).

Update: In case you have to deal with IE in quirks mode, the root element is indeed document.body. Other browsers let document.documentElement.clientHeight represent the document's height (see Finding the size of the browser window, which deals with the window's height, but contains a nice table). Anyway, you want to set the position of the scroller to whatever is the greatest of the three:

javascript:window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight))
Marcel Korpel
awesome thank you