Connecting Tech Pros Worldwide Forums | Help | Site Map

centerDiv

Asker
Guest
 
Posts: n/a
#1: May 13 '06

Hi I found these functions reading old posts of this newsgroup:

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 centerDiv(id){
var o = gebi(id) || {}, w = window, b = document.body;
//var o = gebi(id) || {style: ""}, 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";
}

I call it this way:

<body onload="centerDiv('divID')">

In FireFox I get no problems...

In Internet Explorer (6, SP2, WinXP), I get the yellow triangle: "Object
required". If I only write <body> (without calling the function) I get
no error. The div exists and its id is divID... what could be wrong here?

Bye, thanks.

Evertjan.
Guest
 
Posts: n/a
#2: May 13 '06

re: centerDiv


Asker wrote on 13 mei 2006 in comp.lang.javascript:
[color=blue]
> Hi I found these functions reading old posts of this newsgroup:
>[/color]
[.. see below ..][color=blue]
>
> In FireFox I get no problems...
>
> In Internet Explorer (6, SP2, WinXP), I get the yellow triangle:
> "Object required". If I only write <body> (without calling the
> function) I get no error. The div exists and its id is divID... what
> could be wrong here?
>[/color]

Works nicely in IE6, perhaps you made an error in the part you do not
show? Try this as a html file:

=====================

<script type='text/jscript'>

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 centerDiv(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";
}

</script>

<body onload="centerDiv('divID')">

<div id=divID
style='position:absolute;width:200px;border:red 1px solid;'>
Blah</div>

=====================

Please always show error code linenumber with pointer.

I like this code,
but why would you use code you, I presume, do not completely understand?

Or learn by seperate testing:

alert(typeof {});
alert(typeof {}.offsetWidth);
alert(5 >> 1); // bin 101 -> bin 10 -> 2 [or integer devide by 2]

and in the end of the centerDiv() insert:

alert(o.style.left);
alert(o.pageX);

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Asker
Guest
 
Posts: n/a
#3: May 13 '06

re: centerDiv



Hello, and thanks.

Anything works with your basic example.
BUT not using it the way I use it:
that is, my DIV contains a movie .swf:
all the code to show it is written by javascript:

http://www.bobbyvandersluis.com/ufo/
(I use it mainly to avoid Microsoft/EOLAS/active-content issues)

So I have this external JS that creates the Flash code, and (when
preloader has finished and body onload is fired) another JS should
exactly center the DIV.

The code is this:

<html>
<head>

<script type="text/javascript" src="center.js"></script>
<script type="text/javascript" src="ufo.js"></script>
<script type="text/javascript">
var myUfoObject = {movie:"login.swf", width:"300", height:"400",
majorversion:"7", build:"0"};
UFO.create(myUfoObject, "container");
</script>

<title>Login page</title>
<style type="text/css">
body
{margin: 0px;
background-image: url(pattern_3.gif);
background-color: #000000;
}
..bordered {
border-style: solid;
border-width: 15px 15px 15px 15px;
background-color: #fff;
border-color: #fff;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="shortcut icon" href="prefs.ico" />
</head>

<body onload="centerDiv('container')">

<div class="bordered" id="container"
style="position:absolute;height:400;width:300;">
<p>...replacement...</p>
</div>
</body>
</html>

IE 6 fires a "permission denied" error on line 6.
No problem with FF.

Thanks
Asker
Guest
 
Posts: n/a
#4: May 13 '06

re: centerDiv



a little note:

it happens only the first time you load the page:
if you refresh, you get no error and the div perfectly centers
Evertjan.
Guest
 
Posts: n/a
#5: May 13 '06

re: centerDiv


Asker wrote on 13 mei 2006 in comp.lang.javascript:
[color=blue]
> IE 6 fires a "permission denied" error on line 6.
> No problem with FF.
>[/color]

Permissions have nothing to do with the syntax of javascript.

It just says some security setting is trying to prevent an dangerous
action.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Evertjan.
Guest
 
Posts: n/a
#6: May 13 '06

re: centerDiv


Asker wrote on 13 mei 2006 in comp.lang.javascript:
[color=blue]
>
> a little note:
>
> it happens only the first time you load the page:
> if you refresh, you get no error and the div perfectly centers[/color]

Please quote what you are replying to.

this is not email. Others could want to follow our ramblings too.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Asker
Guest
 
Posts: n/a
#7: May 13 '06

re: centerDiv


[color=blue]
> Permissions have nothing to do with the syntax of javascript.
>
> It just says some security setting is trying to prevent an dangerous
> action.[/color]



I only know I get this yellow triangle here in the left corner and that
after this alert appears, the code to center the div fails. That said,
the second time I load the page I get no error and the div centers: this
is a little crazy but...
Evertjan.
Guest
 
Posts: n/a
#8: May 13 '06

re: centerDiv


Asker wrote on 13 mei 2006 in comp.lang.javascript:
[color=blue]
>[color=green]
>> Permissions have nothing to do with the syntax of javascript.
>>
>> It just says some security setting is trying to prevent an dangerous
>> action.[/color]
>
>
>
> I only know I get this yellow triangle here in the left corner and that
> after this alert appears, the code to center the div fails. That said,
> the second time I load the page I get no error and the div centers: this
> is a little crazy but...[/color]

Not crasy at all. But not Javascript either, methinks.


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Closed Thread