How do you get the window width for all browsers?

P

Phil Powell

[JS]
<script type="text/javascript">
<!--

function getWinWidth() {
isNav = (document.all) ? false : true;
isIE = (document.all) ? true : false;
if (isNav && !isIE)
return(window.innerWidth);
else if (isIE && !isNav)
return(document.body.clientWidth);
else
return(100);
}

var width = getWinWidth();
alert("width=" . width);
document.writeln('<textarea rows="29" name="resume"
cols="108">');
//-->
</script>
[/JS]

This entire script fails in Mozilla Firefox, Konqueror and IE 6+ in
all instances. Customer has a simple request: they want me to produce
a textarea that is dynamic in width that fills the entire window
because the content itself is dynamic in width (people's resumes) and
I cannot think of any way to do this except Javascript.

I borrowed code thinking I could get the instance to work, to no
avail, I continually get "undefined" for the "width" variable when
using the function I borrowed.

How do you simply get the window's width?

Thanx
Phil
 
J

Janwillem Borleffs

Phil said:
I borrowed code thinking I could get the instance to work, to no
avail, I continually get "undefined" for the "width" variable when
using the function I borrowed.

The reason for this, is that document.body does not exist at the time you
are calling it.

One workaround would be the following:

....
<head>
<script type="text/javascript">
function getWinWidth() {
if (window.innerWidth) {
return window.innerWidth;
} else if (document.body.clientWidth) {
return document.body.clientWidth;
} else {
return 100;
}
}

window.onload = function () {
var width;
if (width = getWinWidth()) {
document.forms[0].elements['ta'].style.width
= width + 'px';
}
}
</script>
</head>
<body>
<form>
<textarea name="ta" rows="29" name="resume" cols="100"
style="width:100px">
</textarea>
</form>
</body>
</html>

Also note that I have changed the browser detection into property detection,
which is more reliable.


JW
 
M

Mark Preston

Phil said:
This entire script fails in Mozilla Firefox, Konqueror and IE 6+ in
all instances. Customer has a simple request: they want me to produce
a textarea that is dynamic in width that fills the entire window
because the content itself is dynamic in width (people's resumes) and
I cannot think of any way to do this except Javascript.
[snip]

I can see a couple of problems - so, working through them:-
function getWinWidth() {
isNav = (document.all) ? false : true;

isNav = true (for Firefox etc), false for MSIE (all)
isIE = (document.all) ? true : false;

isIE = true (for MSIE, Opera and other spoofers), false for Firefox etc.
if (isNav && !isIE)
return(window.innerWidth);

Is the window already open at this stage? Remember that it may even be
blocked from opening as well. Try it and see.
else if (isIE && !isNav)
return(document.body.clientWidth);

At this stage, surely there is no "document" to have a "body".
 
T

The Nordic One

Actually, as it turns out, there is no Javascript solution for this as
it is impossible to dynamically resize form elements throughout all
browsers.

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top