Peter Johnson
03-05-2007, 06:12 PM
I am trying to create a link that will go to one location on a page by default, and another if a particular div is present.
The script below works. But have I missed anything? Or is this really the right way to do it?
Thanks in advance.
<html>
<body>
<div>
<div id="special"></div>
<a name="special">Special location</a>
<p>
<a name="default">Default</a>
<p>
<script>
var special= document.getElementById('special');
</script>
<a href="#default" onclick='if (special) {window.scrollTo(0,special); return false;}'>click here</a>
<p>
<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum
<script>
var special = document.getElementById('special').offsetTop;
</script>
</div>
</body>
</html>
The script below works. But have I missed anything? Or is this really the right way to do it?
Thanks in advance.
<html>
<body>
<div>
<div id="special"></div>
<a name="special">Special location</a>
<p>
<a name="default">Default</a>
<p>
<script>
var special= document.getElementById('special');
</script>
<a href="#default" onclick='if (special) {window.scrollTo(0,special); return false;}'>click here</a>
<p>
<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum<p>lorumipsum
<script>
var special = document.getElementById('special').offsetTop;
</script>
</div>
</body>
</html>