script question

inzo21

Regular
Joined
Nov 5, 2003
Messages
74
Hello all,

I'm trying to center a <DIV> element in a user control that I created as a header for a website. I was able to do this by using the following script code that I found on MSDN...

<SCRIPT>
function center(oNode){
oNode.style.setExpression("left","getCenter(this)");
}

function getCenter(oNode){
var oParent=oNode.parentElement;
return (oParent.offsetWidth/2 - oNode.offsetWidth/2);
}

</SCRIPT>

and then calling this function in the <DIV> element using onclick="center(this)". This works!

The issue I have is that I want to have this called automatically on page load rather then through user intervention. The <DIV> tag seems to lack the onload event that I was planning to use.

Any ideas? Thanx in advance!

inzo
 
Put it inthe body onload or use RegisterStartup script function in asp.net

document.getElementById(oNode.name).style.setExpression("left","getCenter(this)");
 
tried in body

Hey Kahlua,

thanks! I tried it in the <BODY> tag in both the user control and the main form and I get a runtime error stating that it can't recognize style.

I'm researching the registerstartuoscript method now and will give that a whirl. I can't seem to find concrete examples using JavaScript though - only VB/C#.

do you know of any links?

inzo
 
sorry, here is a quick example

<code>

<html>

<script language="javascript">

function do_something(myObject) {

document.getElementById(myObject).style.visibility = "hidden";

}


</script>

<body onLoad="do_something('test')">


<div id="test" style="visiblity:visible">

Testing

</div>


<script language="javascript">

</script>
</body>

</html>

</code>
 
hey kahlua...

again, thank you so much for your feedback. One day I'll get a handle on this thing called programming! ;-)

I tried using the RegisterStartup commands and had a problem passing the object into the script. Thus, I then tried the following:

<script language="javascript">
function center(oNode){
document.getElementById(oNode).style.setExpression("left","getCenter(document.getElementById(oNode)");
}
function getCenter(oNode){
var oParent=oNode.parentElement;
return (oParent.offsetWidth/2 - oNode.offsetWidth/2);
}
</script>

<body onload="center('btlogo')" >
</body>

...no luck. It compiles fine but runs with errors on the page - which I can't find out. I'm still working on it - but trying not to smash the keyboard in the process.

if you see a glaring mistake - let me know.

thanks and have a great day.

inzo
 
Got it!

Apparently, the ordering of the functions and two quotation marks made a difference in the script. For those following, I used the following in my user control to center an element inside <DIV> element within the browser window.
<script>

function getCenter(oNode)
{
var oParent = oNode.parentElement.parentElement;
return (oParent.offsetWidth/2 - oNode.offsetWidth/2);
}
function center(tagid)
{
var oNode = document.getElementById(tagid);
oNode.style.setExpression("left",getCenter(oNode));
}

</script>

For some reason, even though I am using the setexpression function, it is not adjusting the page dynamically like the first example I gave (i.e. whena user resizes the screen). But it will do as I am worried about the initial load of the page - and I have just about had it with programming today! ;-0

Thanks again Kahlua for your help and have a great evening.

inzo
 
Back
Top