473,769 Members | 2,643 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there a small change that will make this work differently?

Hi folks,

this is a resolution-detect script that I used on a site. As you can see it
is designed to detect when the screen resolution falls below a certain level
then load an alternative style sheet HOWEVER, is it possible to make a
change to the code that will not only make it respond to windows with widths
of less than 800 pixels but ALSO heights of less than 600 pixels.

I'm a complete novice when it comes to .js so and help would be greatly
appreciated.
Many thanks!

Tim
checkBrowserWid th();

attachEventList ener(window, "resize", checkBrowserWid th, false);


function checkBrowserWid th()
{
var theWidth = getBrowserWidth ();

if (theWidth == 0)
{
var resolutionCooki e =
document.cookie .match(/(^|;)res_layout[^;]*(;|$)/);

if (resolutionCook ie != null)
{
setStylesheet(u nescape(resolut ionCookie[0].split("=")[1]));
}

addLoadListener (checkBrowserWi dth);

return false;
}

if (theWidth > 800)

{
setStylesheet(" hires");
document.cookie = "res_layout =" + escape("hires") ;
}
else
{
setStylesheet(" ");
document.cookie = "res_layout =";
}

return true;
};


function getBrowserWidth ()
{
if (window.innerWi dth)
{
return window.innerWid th;
}
else if (document.docum entElement &&
document.docume ntElement.clien tWidth != 0)
{
return document.docume ntElement.clien tWidth;
}
else if (document.body)
{
return document.body.c lientWidth;
}

return 0;
};


function setStylesheet(s tyleTitle)
{
var currTag;

if (document.getEl ementsByTagName )
{
for (var i = 0; (currTag =
document.getEle mentsByTagName( "link")[i]); i++)
{
if (currTag.getAtt ribute("rel").i ndexOf("style") != -1 &&
currTag.getAttr ibute("title"))
{
currTag.disable d = true;

if(currTag.getA ttribute("title ") == styleTitle)
{
currTag.disable d = false;
}
}
}
}

return true;
};

Apr 11 '06 #1
7 2182
Tim Rogers wrote:
this is a resolution-detect script [...]


No, it is not. The fact that there is no such thing aside,
it would not serve anything good if there was one.

news:19******** *********@Point edEars.de
PointedEars
Apr 12 '06 #2
Tim Rogers said on 12/04/2006 8:22 AM AEST:
Hi folks,

this is a resolution-detect script that I used on a site. As you can see it
is designed to detect when the screen resolution falls below a certain level
It doesn't detect screen resolution (which would be quite pointless
anyway), it looks at the width of the browser window in a manner
suitable only for IE and browsers that copy its event model and window
object API.

To detect the width of the browser window using script, read about
viewport properties at quirksmode:

<URL:http://www.quirksmode. org/viewport/compatibility.h tml>
Note that what works best is dependent on the DTD you use.

Using browser window size to determine the stylesheet to load is a
flawed strategy - partly because determining the size of the browser
window reliably across all browsers is difficult and the size of the
window is not necessarily related to the size of the viewport.

You must also consider what style rules will be active if scripting is
disabled or your script doesn't work in a particular browser.

<URL:
http://groups.google.co.uk/group/com...11631f5012ebf0
Have a look at W3C CSS 2 Media Types - no script required:

<URL:http://www.w3.org/TR/REC-CSS2/media.html>
You can then include stylesheets for a variety of devices (seems you may
want 'screen' and 'handheld', maybe 'print' also) and the client can
decide which one to use. Follow-up in a CSS-related group:

news:comp.infos ystems.www.authoring.stylesheets
The script you posted relies on multiple stylesheets attached using link
elements, it then uses script and the title attribute to enable/disable
them using getElementsByTa gName and sifting through the link elements.
That is inefficient, much better to use the stylesheets collection:

document.styleS heets[i].disabled = true; // or false
Below is a small sample based on similar principles, however I must
stress that using media types is much preferred and doesn't require any
script.
[...]

checkBrowserWid th();

attachEventList ener(window, "resize", checkBrowserWid th, false);
attachEventList ener is a method of a DOM object, the syntax is

bSuccess = object.attachEv entListener(sEv ent, fpNotify)

<URL:http://msdn.microsoft. com/workshop/author/dhtml/reference/methods/attachevent.asp >
It is also IE specific, use feature detection and addEventListene r to
support other browsers. Search the archives for 'addEventListen er
attachEvent'.

Anyway, it seems much simpler to use:

window.onresize = checkBrowserWid th;
But it will replace other resize handlers you might have.

function checkBrowserWid th()
{
var theWidth = getBrowserWidth ();

if (theWidth == 0)
{
var resolutionCooki e =
document.cookie .match(/(^|;)res_layout[^;]*(;|$)/);
The cookie seems to be here as a fallback if the script can't detect the
window size. Is there a button somewhere to set it to some value? If
you aren't using it, remove the code.

If the script can't detect the window size and can't find a cookie, it
does nothing, which is good.

if (resolutionCook ie != null)
{
setStylesheet(u nescape(resolut ionCookie[0].split("=")[1]));
}

addLoadListener (checkBrowserWi dth);
Where is addLoadListener defined?
[...]
function setStylesheet(s tyleTitle)
{
var currTag;

if (document.getEl ementsByTagName )
{
for (var i = 0; (currTag =
document.getEle mentsByTagName( "link")[i]); i++)


This sifts through the link elements, but that's not necessary. If you
load a style sheet using a link element, just put the title in the link:

<link rel="stylesheet " href="blah.css" title="bigText" >

It will become a property of the related stylesheet object, searching
the stylesheets should be more efficient.
Here's a quick 'n dirty example (works in Fx and IE):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Styleshe et play</title>

<style type="text/css" title="bigText" >
p {color: red; font-size: 220%;}
</style>

<!-- Could also use:
<link rel="stylesheet " href="blah.css" title="bigText" >
-->

<style type="text/css" title="smallTex t">
p {color: blue; font-size: 70%;}
</style>

<style type="text/css">
#xx {color: green; font-size: 100%;}
</style>

<script type="text/javascript">

/* Turn stylesheets on or off depending on the
* size of the browser window
*/
function checkSize()
{
var docEl = document.docume ntElement || document.body;
var bodyX, bodyY;
if ( docEl
&& (bodyX = docEl.clientWid th)
&& (bodyY = docEl.clientHei ght) ){

if (bodyX < 801 || bodyY < 601){
setStyleSheet(' smallText');
} else {
setStyleSheet(' bigText');
}
}
}

/* Use the style sheet title attribute to toggle it on/off
* Matching titles are turned on
* Non-matching titles are turned off
* Stylesheets without a title aren't modified
*/
function setStyleSheet(t )
{
var s, ss = document.styleS heets;
for (var i=0, len=ss.length; i<len; ++i){
s = ss[i];
if (s.title){
s.disabled = !(s.title == t);
}
}
}

/* Call checkSize() on window resize and load
*/
window.onresize = checkSize;
window.onload = checkSize;

</script>
</head>
<body>
<p>Plain paragraph - toggles big/small</p>
<p id="xx">Paragra ph xx - controlled by separate stylesheet</p>
</body>
</html>

--
Rob
Group FAQ: <URL:http://www.jibbering.c om/FAQ>
Apr 12 '06 #3
What a helpful comment! Thank you for taking the time to point out that I
called it the wrong name.

But it DOES do as described.
in article 32************* ***@PointedEars .de, Thomas 'PointedEars' Lahn at
Po*********@web .de wrote on 12/4/06 00:48:
Tim Rogers wrote:
this is a resolution-detect script [...]


No, it is not. The fact that there is no such thing aside,
it would not serve anything good if there was one.

news:19******** *********@Point edEars.de
PointedEars


Apr 12 '06 #4
>
Note that what works best is dependent on the DTD you use.

Using browser window size to determine the stylesheet to load is a
flawed strategy - partly because determining the size of the browser
window reliably across all browsers is difficult and the size of the
window is not necessarily related to the size of the viewport.

[...]

Thanks this is very helpful! I do use media types where necessary but the
reason I'm using style switching is to turn off a 'single' formatting
element: one that uses negative widths to centre a page on screen:

div.content {margin-top:-340px;margin-left:-400px;position: absolute;top:
50%;left: 50%;width: 800px;height: 680px;visibilit y:
visible;;backgr ound-color:white}

This works well unless the user has a low screen resolution, or a small
browser window because the negative value then pushes the page off the
screen.

Now I don't agree with the way the page has been designed, I have tried to
explain the limitations to the client but I'm not about to call and tell him
to use another web designer - I just need to find the best solution.
Below is a small sample based on similar principles, however I must
stress that using media types is much preferred and doesn't require any
script.


[...]
Again thanks - Ill spend a couple of hours trying to figure this out and how
to apply it to what I'm doing.

Thanks for your time.

Tim

Apr 12 '06 #5
Tim Rogers wrote:
What a helpful comment! Thank you for taking the time to point out that I
called it the wrong name.
As you probably have recognized by now, it is not just that.
But it DOES do as described.
No, it does not.
[Top post]


<URL:http://jibbering.com/faq/faq_notes/pots1.html>
PointedEars
Apr 12 '06 #6
Tim Rogers said on 12/04/2006 8:01 PM AEST:
Note that what works best is dependent on the DTD you use.

Using browser window size to determine the stylesheet to load is a
flawed strategy - partly because determining the size of the browser
window reliably across all browsers is difficult and the size of the
window is not necessarily related to the size of the viewport.
[...]

Thanks this is very helpful! I do use media types where necessary but the
reason I'm using style switching is to turn off a 'single' formatting
element: one that uses negative widths to centre a page on screen:

div.content {margin-top:-340px;margin-left:-400px;position: absolute;top:
50%;left: 50%;width: 800px;height: 680px;visibilit y:
visible;;backgr ound-color:white}


In that case you can directly modify the style rule rather than mess
with entire style sheets. There may be other solutions, this is really
a CSS issue so ask in news:comp.infos ystems.www.authoring.stylesheets.

In regard to modifying style rules via script, this post may help:

<URL:
http://groups.google.co.uk/group/com...a4d950ffae0440
I'm sure there are a few others around with similar (likely better)
code. The idea is to look in the style sheet for a specific rule then
modify it. But what will you do for non-scripted browsers? They are
likely the ones that will be most affected by small screens (e.g.
phones, PDAs) and have least DOM support.

It has been an issue here before where a particular device's browser
claimed to be JavaScript 1.5 compliant and was missing many DOM
interfaces. The statement regarding compliance may have been
technically correct but because the difference between DOM and script is
not always understood (even by those who claim to be programmers) it was
seen as misleading.

This works well unless the user has a low screen resolution, or a small
browser window because the negative value then pushes the page off the
screen.

Now I don't agree with the way the page has been designed, I have tried to
explain the limitations to the client but I'm not about to call and tell him
to use another web designer - I just need to find the best solution.


As above, if you have access to the CSS you may be able to fix it there.

--
Rob
Group FAQ: <URL:http://www.jibbering.c om/FAQ>
Apr 13 '06 #7
<URL:
http://groups.google.co.uk/group/com.../thread/10710c
afee18926d/32a4d950ffae044 0?q=modify+styl e+rule&rnum=5#3 2a4d950ffae0440
Thanks for the link - the hard part about researching a subject you are not
familiar with is understanding the language and therefore how to pose the
question.
I'm sure there are a few others around with similar (likely better)
code. The idea is to look in the style sheet for a specific rule then
modify it. But what will you do for non-scripted browsers? They are
likely the ones that will be most affected by small screens (e.g.
phones, PDAs) and have least DOM support.
This was my point to the guy - he insists that as his clients are media
departments and advertising agencies its not going to be a problem as they
will all be sitting in front of 20' monitors. What can you do?!
It has been an issue here before where a particular device's browser
claimed to be JavaScript 1.5 compliant and was missing many DOM
interfaces. The statement regarding compliance may have been
technically correct but because the difference between DOM and script is
not always understood (even by those who claim to be programmers) it was
seen as misleading.

This works well unless the user has a low screen resolution, or a small
browser window because the negative value then pushes the page off the
screen.

Now I don't agree with the way the page has been designed, I have tried to
explain the limitations to the client but I'm not about to call and tell him
to use another web designer - I just need to find the best solution.


As above, if you have access to the CSS you may be able to fix it there.

Thanks again for the advice - I appreciate your time.

Apr 13 '06 #8

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

Similar topics

2
1266
by: Christopher Benson-Manica | last post by:
Assuming the input to this program is 1 2 3 0 2 3 4 0
8
3087
by: Mike S | last post by:
Hi all, I noticed a very slight logic error in the solution to K&R Exercise 1-22 on the the CLC-Wiki, located at http://www.clc-wiki.net/wiki/KR2_Exercise_1-22 The exercise reads as follows: "Write a program to 'fold' long input lines into two or more shorter
13
3114
by: Alan Silver | last post by:
Hello, MSDN (amongst other places) is full of helpful advice on ways to do data access, but they all seem geared to wards enterprise applications. Maybe I'm in a minority, but I don't have those sorts of clients. Mine are all small businesses whose sites will never reach those sorts of scales. I deal with businesses whose sites get maybe a few hundred visitors per day (some not even that much) and get no more than ten orders per day....
169
9214
by: JohnQ | last post by:
(The "C++ Grammer" thread in comp.lang.c++.moderated prompted this post). It would be more than a little bit nice if C++ was much "cleaner" (less complex) so that it wasn't a major world wide untaking to create a toolchain for it. Way back when, there used to be something called "Small C". I wonder if the creator(s) of that would want to embark on creating a nice little Small C++ compiler devoid of C++ language features that make...
45
18901
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies of their elements? Why can't I change the element itself? class Program { private struct MyStruct
25
5953
by: mereba | last post by:
Hello My country Ghana is changing its currency. I want to write a small programme in C++ that can covert from the old currency into the new one. I would like this programme to run behind a simple calculator- looking interface. I'm quite new to C++, and I would need some help. Please any suggestion is welcome :) The following is what I have been able to do so far:
58
5774
by: Don Li | last post by:
I've tried <body style="font-family:Verdana,Arial,Helvetica;font-size:0.8em;"> or <body style="font-family:Verdana,Arial,Helvetica;font-size:11pct;"> or <body style="font-family:Verdana,Arial,Helvetica;font-size:80%;"> to no avail. In IE7 it shows up as Medium. What did I do wrong? Thanks.
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10212
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10047
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3962
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.