/    Sign up×
Community /Pin to ProfileBookmark

Difficulty positioning div element dynamically

Hi Everyone,

I am trying to develop a script that will position html elements dynamically within various browsers regardless of screen resolution and hence available screen display area.

The script and test page I am developing looks like this:

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
<head>
<title>Dynamic Element Positioning Script – Phase 1</title>
<script type=”text/javascript”>
//No need to call the browserType() function because a variable of this type is created
//below.
function browserType() {
agent = navigator.userAgent.toLowerCase();
this.major = parseInt(navigator.appVersion);
this.minor = parseFloat(navigator.appVersion);
this.ns = ((agent.indexOf(‘mozilla’) != -1) &&
((agent.indexOf(‘spoofer’) == -1) && (agent.indexOf(‘compatible’) == -1)));
this.ns4 = (this.ns && (this.major == 4));
this.ns6 = (this.ns && (this.major >= 5));
this.ie = (agent.indexOf(“msie”) != -1);
this.ie3 = (this.ie && (this.major < 4));
this.ie4 = (this.ie && (this.major == 4) && (agent.indexOf(“msie 5.0”) == -1));
this.ie5 = (this.ie && (this.major == 4) && (agent.indexOf(“msie 5.0”) != -1));
this.ie55 = (this.ie && (this.major == 4) && (agent.indexOf(“msie 5.5”) != -1));
this.ie6 = (this.ie && (agent.indexOf(“msie 6.0”)!=-1) );
}

var is = new browserType();

function displayProperties()
{
if(is.ns6) {
page_width=innerWidth;
page_height=innerHeight;
} else if(is.ie5 || is.ie55 ||is.ie6) {
page_width=document.body.clientWidth;
page_height=document.body.clientHeight;
}

function elementObject(id,position,left,top,visibility) {
if (is.ie5|| is.ie55||is.ie6|| is.ns6){
this.obj = document.getElementById(id).style;
this.obj.position = position;
this.obj.left = left;
this.obj.top = top;
this.obj.visibility = visibility;
return this.obj;
}
}
function elementSetup(id, position, left, top, visibility) {
positionElement = new elementObject(id, position, left, top, visiblity);
}
</script>
<style type=”text/css”>
<!–
.main {
font-family: Georgia, “Times New Roman”, Times, serif;
font-size: 16px;
color: #FBEED5;
text-decoration: none
}
–>
</style>
</head>
<body onLoad=”displayProperties()”>

<div id=”centerLayer” style=”position: absolute; width:200px;
height:24px; left: 0px; top: 0px; z-index: 6; visibility: hidden;” onload=”elementSetup(‘centerLayer’, ‘absolute’, page_width/2-100, page_height/2-12,’visible’)”>
<span class=”main”>This is a Layer Centered By Screen Resolution</span>
</div>
</body>
</html>

The idea behind this script is that when it loads it creates a new object called ‘is’ which holds details of the browser type (to the best of my knowledge this object is created once the web page is loaded. Please correct me if I am wrong here). Once the body of the document is reached the ‘displayProperties()’ function is called which effectively stores the available screen area of the current browser (regardless of browser type, screen resolution, etc). Finally I am trying to center a div in the middle of the screen with the text “This is a Layer Centered By Screen Resolution”.

Unfortunately I am getting various error messages from different browsers. As I am a beginner with JavaScript I don’t really understand the error messages.

Any help with fixing this script up will be greatly appreciated.

Thanks for your time.

Regards

David?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@PittimannJun 18.2004 — Hi!

  • 1. This function is missing the [COLOR=red]}[/COLOR]:

    function displayProperties()

    {

    if(is.ns6) {

    page_width=innerWidth;

    page_height=innerHeight;

    } else if(is.ie5 || is.ie55 ||is.ie6) {

    page_width=document.body.clientWidth;

    page_height=document.body.clientHeight;

    }

    [COLOR=red]}[/COLOR]


  • 2. This function is missing the [COLOR=red]i[/COLOR]:

    function elementSetup(id, position, left, top, visibility) {

    positionElement = new elementObject(id, position, left, top, visib[COLOR=red]i[/COLOR]lity);

    }


  • 3. The onload stuff in the div tag has to go to the body tag:

    <body onLoad="displayProperties()[COLOR=red];elementSetup('centerLayer', 'absolute', page_width/2-100, page_height/2-12,'visible')[/COLOR]">


  • Then it will work...

    Cheers - Pit
    ×

    Success!

    Help @3D_Dog_Man spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.20,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...