/    Sign up×
Community /Pin to ProfileBookmark

Div Width Setting Function Dependant On Client Screen Width JavaScript

I am somewhat of a beginner/ amateur JavaScript developer and have created this function by myself which onload should calculate the client screen dimensions and adjust the width of the div(banner) accordingly leaving only 5px marginleft and 5px from the right edge of the screen. As default, I have set the div width as 1177px but this should change dynamically onload.

Markup below:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Untitled Document</title>

<style type=”text/css”>

#banner{border:1px solid #CCCCCC; margin-left:5px; margin-top:35px; height:150px; width:1177px;}

</style>

<script>

var banr = document.getElementById(“banner”);

function setScreenVariableElemWidths() {
var wind = 0;
var body = document.window.body;
if (document.body.clientWidth) {
wind = document.body.clientWidth;
if (document.getElementById) {
getWidths(0);
var wdban = banr_wd;
var areaoDisp = wind – (parseInt(banr.style.marginLeft, 10) * 2) + “px”;
if (element.setAttribute) {
saBan = wind – (parseInt(banr.style.marginLeft, 10) *
2) + “px”;
banr.setAttribute(“style”, saBan);
}else if(wdban < areaoDisp){
diffBan = areaoDisp – banr.style.width;
banr.style.width = banr.style.width + diffBan;
}else{
diffBan = banr.style.width – areaoDisp;
banr.style.width = banr.style.width – diffBan;
}

}
}else if(document.documentElement.clientWidth){
wind = document.documentElement.clientWidth;
}else{wind = window.innerWidth;}
}

function getWidths(ban_width){
var banr_wd = banr.style.width;
return banr_wd;
}

</script>

</head>
<body onload=”setScreenVariableElemWidths()”>

<div id=”banner”>

</div>

</body>
</html>

Javascript as follows:

var banr = document.getElementById(“banner”);

function setScreenVariableElemWidths() {
var wind = 0;
var body = document.window.body;
if (document.body.clientWidth) {
wind = document.body.clientWidth;
if (document.getElementById) {
getWidths(0);
var wdban = banr_wd;
var areaoDisp = wind – (parseInt(banr.style.marginLeft, 10) * 2) + “px”;
if (element.setAttribute) {
saBan = wind – (parseInt(banr.style.marginLeft, 10) *
2) + “px”;
banr.setAttribute(“style”, saBan);
}else if(wdban < areaoDisp){
diffBan = areaoDisp – banr.style.width;
banr.style.width = banr.style.width + diffBan;
}else{
diffBan = banr.style.width – areaoDisp;
banr.style.width = banr.style.width – diffBan;
}

}
}else if(document.documentElement.clientWidth){
wind = document.documentElement.clientWidth;
}else{}
}

function getWidths(ban_width){
var banr_wd = banr.style.width;
return banr_wd;
}

Running the script using JSFiddle, It runs perfectly with no errors at all. However embedded within the <head> tags of a HTML document, I get the following results:

Internet Explorer 11.0.9600.16476 Div width is set as set via the style.
Mozilla Firefox 26.0 Div width is set as set via the style. (However using the web console and manually calling the function – the console yields the result: TypeError: document.window is undefined)

Does any one understand this and how to resolve this error? Any tips, pointers, or resolutions will be much appreciated. One idea of mine was to place the entire script within a try/ catch block but I became lost upon deciding what exactly I would be catching and how the catch clause would be coded. Any pointers towards errors/ error handling would also be appreciated.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@PadonakDec 27.2013 — <i>
</i>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="utf-8" /&gt;
&lt;title&gt;123&lt;/title&gt;
&lt;style type="text/css"&gt;
body{padding:0px 0px;margin:0px 0px;}
#banner{border:1px solid #CCCCCC; margin-top:35px; height:150px; width:1177px; text-align:center;}
#result{margin-top:10px;text-align:center;}
&lt;/style&gt;
&lt;script type="text/javascript"&gt;
var marg=5;
function doc(id){return document.getElementById(id);}
function banrResize(id){
var wd=document.body.offsetWidth;
doc('banner').style.width=(wd-2-marg*2)+'px'; // the 2 is twice border width
doc('banner').style.marginLeft=marg+'px';
doc('result').innerHTML='&lt;div&gt;document.body.offsetWidth = '+wd+';&lt;br /&gt;marg = '+marg+';&lt;br /&gt;document.getElementById("banner").offsetWidth = '+doc('banner').offsetWidth+'&lt;/div&gt;';
}

window.onload=window.onresize=banrResize;
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="banner"&gt;&lt;img src="http://www.webdeveloper.com/forum/images/webdev-logo2.gif" /&gt;&lt;h1&gt;BANNER&lt;/h1&gt;&lt;/div&gt;
&lt;div id="result"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@nsathauthorDec 27.2013 — Thanks padonak, that works perfectly. I was going to ask if there is any way that you know of that would enable me to fix the error on my code. Being that error is TypeError: document.window is undefined. Or if there any sources you could point me to where I could find myself. I have searched document.window with very little luck and still in the process of learning try/ catch and Javascript errors. Any books or websites?

much appreciated,

Thanks
Copy linkTweet thisAlerts:
@PadonakDec 27.2013 — don't try searching 'document.window' because you'll never find it. it doesn't exist. if you want to refer the body element, use [B]document.body[/B]
Copy linkTweet thisAlerts:
@nsathauthorDec 27.2013 — O sweet thanks
Copy linkTweet thisAlerts:
@tracknutDec 27.2013 — Just in case this is useful - html/css version of it:
<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Box Sizing&lt;/title&gt;
&lt;meta charset="utf-8"&gt;
&lt;style type="text/css"&gt;
#banner {
-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box; /* Opera/IE 8+ */
border:1px solid #CCCCCC; margin-top:35px; height:150px;text-align:center;
padding:5px;
}
&lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div id="banner"&gt;
&lt;img src="http://www.webdeveloper.com/forum/images/webdev-logo2.gif"&gt;
&lt;h1&gt;BANNER&lt;/h1&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @nsath 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.11,
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,
)...