js code execution causes scrolling back to top?

P

peppeprof

Hi-

I need to use self.scrollTo(x,y) as the last action in a js function called
by a onClick event. It seems that the scrolling actually takes place, but
the document is then scrolling back to top when js code execution has ended.

In the following example I get this effect in IE6 with the third and fourth
calls, while the first two work fine.

-------------------------------------------------------------
<HTML><HEAD><TITLE>scrollTo</TITLE>
<script language="JavaScript" type="text/JavaScript">
<!--
function scrollit(s) { self.scrollTo(0,s) }
-->
</script>
</HEAD>
<BODY>
<FORM name=myForm><INPUT onclick=self.scrollTo(0,300) type=button
value=scroll_(works_fine)> </FORM>
<p onClick="self.scrollTo(0,300)">scroll with event in paragraph
(works_fine)</p>
<p><a href="#" onClick="self.scrollTo(0,300)">scroll on event onclick in
anchor (doesn't work)</a></p>
<p><a href="#" onClick="scrollit(300)">scroll on event onclick in anchor,
calling a js function (doesn't work)</a></p>
<p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p><p>7</p><p>8</p><p>9</p><p>1
0</p>
<p>11</p><p>12</p><p>13</p><p>14</p><p>15</p><p>16</p><p>17</p><p>18</p>
</BODY></HTML>
----------------------------------------------------------------------------
 
R

Randy Webb

peppeprof said:
<p><a href="#" onClick="self.scrollTo(0,300)">scroll on event onclick in
anchor (doesn't work)</a></p>

Yes it does, it just then immediately follows the href (#) which
navigates to the top of the page.

<a href="#" onclick="self.scrollTo(0,300);return false">Now it will
work, but why are you using js to do what a link can do?</a>
 
I

Ivo

"Randy Webb" asked
<a href="#" onclick="self.scrollTo(0,300);return false">Now it will
work, but why are you using js to do what a link can do?</a>

The way of the # would add to the window's history list. scrollTo has no
such effect.

Also, some find it better to use a totally empty href instead of the hash
for these onclick links. I don't remember the reason, but neither do I know
why the hash is there. Another, afaik equally workable idea is

<a href="javascript:void(' Descriptive statusbar message ')" onclick="...">

Buttons can be styled to look like links, and vice versa. Which are
generally to be preferred and why? Am I right in thinking links are easier
to render for a browser (both visually and in the DOM), that buttons take up
more resources?
Ivo
 
L

Lee

Ivo said:
"Randy Webb" asked

The way of the # would add to the window's history list. scrollTo has no
such effect.

Also, some find it better to use a totally empty href instead of the hash
for these onclick links. I don't remember the reason, but neither do I know
why the hash is there.

Some older browsers required that the value be a valid URL.
"#" is a valid URL. "" is not (or at least wasn't recognized as one)


Another, afaik equally workable idea is
<a href="javascript:void(' Descriptive statusbar message ')" onclick="...">

This method causes unpredictable results if Javascript is
not enabled.
 
P

peppeprof

Yes it does, it just then immediately follows the href (#) which
navigates to the top of the page.

<a href="#" onclick="self.scrollTo(0,300);return false">Now it will
work, but why are you using js to do what a link can do?</a>
Randy

Thanks a lot! I've been really stupid...
but why are you using js to do what a link can do?</a>
Randy

'cause I have a vertical Iframe with many img links, scroll enabled; on
click event I want to address a html document in another Iframe, turn
visible some layers and hidden some others and, finally, make the menu
scroll to have the selected img in vertical middle. If this was to be on the
internet I would have probably used php, but this is gonna be a cd.

Thank you again

peppeprof
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top