document.body.onclick not working in script

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to build a custom control to wrap my smart navigation
implimention (not microsofts 'cause it has problems)

The follow code works fine when the onclick and onload events are defined in
the Body tag. However, when I try to set them in Javascript code, I get
errors. What am I doing wrong???


**********THIS WORKS ************
<html>
<head>
<script>
function doOnClickBody() {
document.getElementById("hdnScrollTop").value =
document.body.scrollTop;
}

function doOnLoadBody() {
document.body.scrollTop = document.getElementById("hdnScrollTop").value;
}
</script>
</head>
<body onClick="doOnClickBody();" onLoad="doOnLoadBody();">

<form id="myFromId" runat="server">
<input type="hidden" id="hdnScrollTop" runat="server" />
</form>
</body>
</html>


***********THIS DOES NOT WORK ***************

<script language="javascript">
function doOnClickBody() {
document.getElementById("<%=Me.hdnScrollTop.ClientId%>").value =
document.body.scrollTop;
}

function doOnLoadBody() {
document.body.scrollTop =
document.getElementById("<%=Me.hdnScrollTop.ClientId%>").value;
}
document.body.onclick = doOnClickBody(); //CAUSES ERROR
document.body.onload = doOnLoadBody(); //CAUSES ERROR
</script>

<body MS_POSITIONING="FlowLayout" >
<form id="Form1" method="post" runat="server">
<input type="hidden" id="hdnScrollTop" runat="server" NAME="hdnScrollTop">
</form>
</body>
 
<script language="javascript">
function doOnClickBody() {
//do something
}

function doOnLoadBody() {
//do something
}
document.onclick = doOnClickBody;
window.onload =doOnLoadBody;
</script>

should work in both IE and mozilla.

Karl
 
This ended up being strictly a timing issue. Objects where not created or at
leasts finished being created when I was tring accessing them. Using the
window.setTimeout functionfixed all my problems...

Earl
 
Back
Top