Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Onclick move to anchor 1

Status
Not open for further replies.

WebRic

Technical User
Sep 21, 2004
95
0
0
GB
Hi all,

Is there an onclick event I can use to move to an anchor?

I have a form that loads some innerhtml content to a div tag via ajax. I'd like to jump down to this point without using the href="#whatever", is this possible.

Regards,

Richard
 
is this possible

Yup, here's an example.
[small](note the onload function that alerts the page has been loaded - it does not retrigger after clicking the button which proves the page is not reloaded - I put it in cause I wasn't sure myself [smile])[/small]
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

function jumpToAnchor() {
   window.location = window.location + "#blah";
}

window.onload = function () {alert("page loaded")};

</script>
</head>
<body>
<input type="button" value="click me" onclick="jumpToAnchor()" />
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<a name="blah">
<p>This is a test</p>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
</body>
</html>

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
just use a sexy regexp to strip off the existing anchor declaration:
Code:
function jumpToAnchor() {
   window.location = window.location.[!]replace(/\#.*$/, "")[/!] + "#blah";
}

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
hmmm..... you'll probably wanna cast that as a string before running the replace method:
Code:
function jumpToAnchor() {
   window.location = [!]String([/!]window.location[!])[/!].replace(/\#.*$/, "") + "#blah";
}

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
This forums is great! Cheers Kaht

R
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top