473,387 Members | 3,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Script to detect screen width and set dynamic CSS not working correctly

JJA
I'm trying this technique I found on Digital-Web.com in an attempt to
set different stylesheets based on the width of the screen. Even when I
resize the screen to my max of 1280x1024 I always get "Small" in the
alert box in either IE 6 or Firefox 1.5.0.7. Could someone please
comment on how to accomplish this?

<!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>
<title>Test Dynamic CSS</title>
<link rel=STYLESHEET href=styleHuge.css type=text/css>
<link rel=STYLESHEET href=styleBig.css type=text/css>
<link rel=STYLESHEET href=styleSmall.css type=text/css>
<script language="JavaScript">
<!--
function dynamicStyle() {
var screenW = (document.body) ? document.body.clientWidth : innerWidth
var theStyle = (screenW 1248) ? 'Huge' : (screenW 1000) ? 'Big' :
'Small';
alert(theStyle + ' ' + screenW + ' ' + document.body.clientWidth)
var i, a;
for (i = 0; a = document.getElementsByTagName('link')[i]; i++) {
if (a.getAttribute('href').indexOf(theStyle) != -1) {
a.disabled = false;
} else {
a.disabled = true;
}
}
}
window.onload = window.onresize = dynamicStyle;
//-->
</script>
</head>

<body>

Oct 27 '06 #1
2 1926
ASM
JJA a écrit :
I'm trying this technique I found on Digital-Web.com in an attempt to
set different stylesheets based on the width of the screen. Even when I
resize the screen to my max of 1280x1024 I always get "Small" in the
alert box in either IE 6 or Firefox 1.5.0.7. Could someone please
comment on how to accomplish this?
There is no interest at all to know screen size in css
Why ?
Because I do open my browsers in a window sized by 900/650
and my screen is by 1200/900

The script bellow tries to get the window size.
<!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>
<title>Test Dynamic CSS</title>
<link rel=STYLESHEET href=styleHuge.css type=text/css>
<link rel=STYLESHEET href=styleBig.css type=text/css>
<link rel=STYLESHEET href=styleSmall.css type=text/css>
<script language="JavaScript">
<!--
function dynamicStyle() {
var screenW = (document.body) ? document.body.clientWidth : innerWidth
var screenW = (document.body) ? document.body.clientWidth : innerWidth;

and that works fine with my FireFox and my Opera.
var theStyle = (screenW 1248) ? 'Huge' : (screenW 1000) ? 'Big' :
'Small';
alert(theStyle + ' ' + screenW + ' ' + document.body.clientWidth)
var i, a;
for (i = 0; a = document.getElementsByTagName('link')[i]; i++) {
if (a.getAttribute('href').indexOf(theStyle) != -1) {
a.disabled = false;
} else {
a.disabled = true;
}
}
}
window.onload = window.onresize = dynamicStyle;
//-->
</script>
</head>

<body>
Oct 27 '06 #2
In message <11*********************@i42g2000cwa.googlegroups. com>, Fri,
27 Oct 2006 08:19:44, JJA <jo***@cbmiweb.comwrites
if (a.getAttribute('href').indexOf(theStyle) != -1) {
a.disabled = false;
} else {
a.disabled = true;
}
Something like
a.disabled = ! /theStyle/.test(a.getAttribute('href'))
seems better.

It's a good idea to read the newsgroup and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Andy R. | last post by:
Hello everyone, I've spent quite some time now, looking for some information on how to get this done, sadly none has helped me much, though. I have a bit of java scrpt on a webpage (.php) to...
0
by: Henk Verhoeven | last post by:
(Reply on this newsgroup to an email - reply by email did not work) Wolfman wrote: > > Hi, > I found your Email in the php Newsgroup. > I was looking for some way to resize some graphiks on my...
11
by: Wentink | last post by:
Does anybody have a simple script that let's me popup a picture from a thumbnail? The ones i found are all very very complicated and messy in the source... Thanks, Tintin
16
by: Luca | last post by:
About one month ago I have inserted in my web site a clickcounter who controls the access to almost every link, but this script who changed the path in every link so the search engines couldn't...
3
by: Keith Smith | last post by:
How do most people deal with different screen sizes? I was thinking about creating certain Forms that are displayed when certain screen sizes are used, but that sure does seem like an awful lot of...
0
by: Bev | last post by:
Hello :-), I am not sure if I am in the right forum so please forgive me if I err. My Ops is XPPro, SP2 installed, IE6 and Outlook Express6, Office 2002. I am using ready made scripts in htm...
1
by: dragze | last post by:
Hi, On one of the pages of my site i use two javascripts, one makes transparency of png's work in IE, and the other embeds a flash player. Now use one of the scripts it works fine, use both and...
7
by: jeddiki | last post by:
Hi, As I am in Turkey at present, I can not see vidoes on youtube. So I have tried a few proxies but keep finding them slow or not working. So I have installed myphpProxy on my server under...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.