Connecting Tech Pros Worldwide Forums | Help | Site Map

help with function

Thunder
Guest
 
Posts: n/a
#1: Feb 23 '06

Hi,
I use this function to center a DIV both horizonatally and vertically.

function divCenterer(id, w, h){
if (document.layers){
document.layers[id].width=w;
document.layers[id].pageX
=(window.innerWidth/2)-(document.layers[id].width/2);
document.layers[id].height=h;
document.layers[id].pageY
=(window.innerHeight/2)-(document.layers[id].height/2);
}
else if (document.all){
document.all[id].width=w;
document.all[id].style.posLeft
=(document.body.clientWidth/2)-(document.all[id].width/2);
document.all[id].height=h;
document.all[id].style.posTop
=(document.body.clientHeight/2)-(document.all[id].height/2);
}
else if(document.getElementById){
document.getElementById(id).width=w;
document.getElementById(id).style.left
=(window.innerWidth/2)-(document.getElementById(id).width/2);
document.getElementById(id).height=h;
document.getElementById(id).style.top
=(window.innerHeight/2)-(document.getElementById(id).height/2);
}
}

I apply it this way:
<body onLoad="divCenterer('container', 100, 100);"
onResize="location.reload()">
<div class="b" id="container"
style="position:absolute;height:100;width:100;"> etc.

I must say it perfectly works on IE6, every version of Firefox and some
old browser but they have told me that this function is not as good as I
thought. Could you help me improve it? They suggested me to change the
position of document.getElementById with that of document.layers but
this way it doesn't work. They have also told me I could avoid passing w
and h but I don't know why and how.

Any idea or practical code sample to get this function better?

Thanks,
Thunder

Jonas Raoni
Guest
 
Posts: n/a
#2: Feb 23 '06

re: help with function


Thunder > wrote:[color=blue]
>
> Hi,
> I use this function to center a DIV both horizonatally and vertically.
>
> function divCenterer(id, w, h){
> if (document.layers){
> document.layers[id].width=w;[/color]


Try this =]

window.onload = function(){
centerElement('container');
};


function gebi(id){
var i, d = document;
for(i in {getElementById: 0, all: 0, layers: 0})
if(i in d) break;
return d[i] ? d[i][id] || d[i](id) : null;
}

function centerElement(id){
var o = gebi(id) || {}, w = window, b = document.body;
o.style.left = (o.pageX = (w.innerWidth || b.clientWidth || 0) -
(o.offsetWidth || o.width || 0) >> 1) + "px";
o.style.top = (o.pageY = (w.innerHeight || b.clientHeight || 0) -
(o.offsetHeight || o.height || 0) >> 1) + "px";
}


--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Thunder
Guest
 
Posts: n/a
#3: Feb 24 '06

re: help with function


[color=blue]
> Try this =][/color]

madre de Dios...
Thunder
Guest
 
Posts: n/a
#4: Feb 24 '06

re: help with function



another little question:
will this work specifing a DOCTYPE?

Jonas Raoni
Guest
 
Posts: n/a
#5: Feb 24 '06

re: help with function


Thunder wrote:[color=blue]
>[color=green]
>> Try this =][/color]
>
> madre de Dios...[/color]

\o/ mãe de deus hahaha

I don't like to send corrections for little mistakes, but as you
answered, change the first line to:

var o = gebi(id) || {style: ""}, w = window, b = document.body;

Or add a line with "if(!o) return;"

And it will work the way I planned (if you send an invalid id).


--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Jonas Raoni
Guest
 
Posts: n/a
#6: Feb 24 '06

re: help with function


Thunder wrote:[color=blue]
> another little question:
> will this work specifing a DOCTYPE?[/color]

My test file is setted up by a XHTML doctype and it worked fine.


--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Thunder
Guest
 
Posts: n/a
#7: Feb 24 '06

re: help with function


Jonas,

where can I learn this dom coding style?

This
if(i in d) break;
sounds really new to me

and I did not perfectly understand the bitwise operator use.
And the correction {style: ""} too...



Jonas Raoni
Guest
 
Posts: n/a
#8: Feb 24 '06

re: help with function


Thunder wrote:[color=blue]
> Jonas,
> where can I learn this dom coding style?[/color]

I don't know, every programmer has it's own programming style and
techniques :)
[color=blue]
> This
> if(i in d) break;
> sounds really new to me[/color]

Ok, but it's simple, it just checks if a property exists in an object.

var o = {abc: 123};
alert("abc" in o);
alert("cde" in o);

[color=blue]
> and I did not perfectly understand the bitwise operator use.[/color]

"x >> 1" is the same as dividing x by 2 and rounding the value down,
there are a lot of tricks related to those bitwise operators hehe.
[color=blue]
> And the correction {style: ""} too...[/color]

Just to avoid accessing "undefined.undefined", which gives an error, but
"object.undefined" is ok, anyway the right way is to add "if(!o) return". :)


--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Thunder
Guest
 
Posts: n/a
#9: Feb 24 '06

re: help with function


HI,
when you put any xhtml doctype, IE puts the DIV half outside the
screen... no problems with FF...
.... maybe this script should be used in quirks



Jonas Raoni ha scritto:[color=blue]
> Thunder wrote:[color=green]
>> another little question:
>> will this work specifing a DOCTYPE?[/color]
>
> My test file is setted up by a XHTML doctype and it worked fine.
>
>[/color]
Jonas Raoni
Guest
 
Posts: n/a
#10: Feb 24 '06

re: help with function


Thunder wrote:[color=blue]
> HI,
> when you put any xhtml doctype, IE puts the DIV half outside the
> screen... no problems with FF...
> ... maybe this script should be used in quirks[/color]

That's bad haha, I'm not a expert on these details, but can't you do
something like:

html{
height: 100%;
}
body{
display: block;
min-height: 100%;
}

Here it worked fine on Opera, IE and FF (I mean, the area is totally
covered)


--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Closed Thread