By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,695 Members | 1,601 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,695 IT Pros & Developers. It's quick & easy.

js code execution causes scrolling back to top?

P: n/a
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>
----------------------------------------------------------------------------
----------------------------------

Any help highly appreciated

peppeprof
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
peppeprof wrote:
Hi-
<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>

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/
Jul 23 '05 #2

P: n/a
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
Jul 23 '05 #3

P: n/a
Lee
Ivo said:

"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.


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.

Jul 23 '05 #4

P: n/a
> > <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>
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
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.