473,385 Members | 1,872 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,385 software developers and data experts.

Modify Script to change background color based on Array Index

Greetings

I don't know if this is possible, but this group should be able to tell me.

I have a webpage with a changing message board (I understand the problems
with having changing text, but this was a nice victory over the demand for
a scrolling message). The messages are updates on community information.
What I would like to be able to do is have the background color (and
possibly font color) change based on the Array Index. For example, if the
pool is going to be closed for chemical treatment, I would like the display
to have a red background. If there is going to be street cleaning
requiring the removal of vehicles, I would like a yellow background. For
all other messages, a default blue background.

The code I am working with and abbreviated CSS & HTML are:

<script type="text/javascript">
var msgIX = 0;
var msgs = [
" Notices and Messages", \* RED *\
"Chemical Treatment August 21", \* RED *\
"Pool Closed", \* RED *\
" Notices and Messages", \* YELLOW *\
"Street Cleaning August 12 ", \* YELLOW *\
"Remove your Vehicles from the street ", \* YELLOW *\
" Notices and Messages", \* BLUE here down *\
"Family Movie Night is ",
"August 25 ",
" Notices and Messages",
"Pub Night and Quiz is ",
"October 14 or 21 (TBD) ",
" Notices and Messages",
"Pot Luck Dinner is ",
"November--Exact Date TBD ",
" Notices and Messages",];
function scrollMessages(milliseconds) {
window.setInterval ("displayMessage()", milliseconds);
}
function displayMessage() {
if (document.getElementById != null) {
var heading = document.getElementById("scrollme");
heading.firstChild.nodeValue = msgs[msgIX];
}else{
if(navigator.appName == "Microsoft Internet Explorer") {
var heading = document.all.item("scrollme");
heading.innerText = msgs[msgIX];
}
}
++msgIX;
msgIX %= msgs.length;
}
</script>
<style>
#noticebox {
padding:"2px";
border-width:"2px";
border-style:inset;
border-color:blue;
width:174px;
height:"42px";
z-index:30;
text-align:-moz-center;
text-align:center;
background-color:"#ceg";
left:0;
}
</style>
<body onload="scrollMessge(2000)">

<div id="noticebox">
<p class="mainbold" id="scrollme"Notices and Messages</p>
</div>
Any assistance would appreciated--even if it entails telling me it cant be
done.

Thanks
DB.
..
Aug 3 '06 #1
5 3421
DavidB wrote:
I have a webpage with a changing message board (I understand the problems
with having changing text, but this was a nice victory over the demand for
a scrolling message). The messages are updates on community information.
What I would like to be able to do is have the background color (and
possibly font color) change based on the Array Index. For example, if the
pool is going to be closed for chemical treatment, I would like the display
to have a red background. If there is going to be street cleaning
requiring the removal of vehicles, I would like a yellow background. For
all other messages, a default blue background.
With a slight mofication to the messages

var msgs = [
[ " Notices and Messages", "red" ], \* RED *\
....
[ " Notices and Messages", "yellow" ], \* YELLOW *\
....
];

it's easy enough in your function to get the message and color:
var msg = msgs[ msgIndex ][ 0 ];
var color = msgs[ msgIndex ][ 1 ];

to style an element's background color:
document.getElementById( '...' ).style.backgroundColor = color;

or body:
document.body.style.backgroundColor = color;

Aug 3 '06 #2
"bobzimuta" <ej******@gmail.comwrote in
news:11**********************@i3g2000cwc.googlegro ups.com:
DavidB wrote:
>I have a webpage with a changing message board (I understand the
problems with having changing text, but this was a nice victory over
the demand for a scrolling message). The messages are updates on
community information. What I would like to be able to do is have the
background color (and possibly font color) change based on the Array
Index. For example, if the pool is going to be closed for chemical
treatment, I would like the display to have a red background. If
there is going to be street cleaning requiring the removal of
vehicles, I would like a yellow background. For all other messages,
a default blue background.

With a slight mofication to the messages

var msgs = [
[ " Notices and Messages", "red" ], \* RED *\
...
[ " Notices and Messages", "yellow" ], \* YELLOW *\
...
];

it's easy enough in your function to get the message and color:
var msg = msgs[ msgIndex ][ 0 ];
var color = msgs[ msgIndex ][ 1 ];

to style an element's background color:
document.getElementById( '...' ).style.backgroundColor = color;

or body:
document.body.style.backgroundColor = color;
Just exactly what I wanted. Thanks for the help!

DB
Aug 4 '06 #3
DavidB <ju******@david-baker.comwrote in
news:Xn*******************************@64.59.135.1 59:
Greetings

I don't know if this is possible, but this group should be able to
tell me.

I have a webpage with a changing message board (I understand the
problems with having changing text, but this was a nice victory over
the demand for a scrolling message). The messages are updates on
community information. What I would like to be able to do is have the
background color (and possibly font color) change based on the Array
Index. For example, if the pool is going to be closed for chemical
treatment, I would like the display to have a red background. If
there is going to be street cleaning requiring the removal of
vehicles, I would like a yellow background. For all other messages, a
default blue background.

The code I am working with and abbreviated CSS & HTML are:

<script type="text/javascript">
var msgIX = 0;
var msgs = [
" Notices and Messages", \* RED *\
"Chemical Treatment August 21", \* RED *\
"Pool Closed", \* RED *\
" Notices and Messages", \* YELLOW *\
"Street Cleaning August 12 ", \* YELLOW *\
"Remove your Vehicles from the street ", \* YELLOW *\
" Notices and Messages", \* BLUE here down *\
"Family Movie Night is ",
"August 25 ",
" Notices and Messages",
"Pub Night and Quiz is ",
"October 14 or 21 (TBD) ",
" Notices and Messages",
"Pot Luck Dinner is ",
"November--Exact Date TBD ",
" Notices and Messages",];
function scrollMessages(milliseconds) {
window.setInterval ("displayMessage()", milliseconds);
}
function displayMessage() {
if (document.getElementById != null) {
var heading = document.getElementById("scrollme");
heading.firstChild.nodeValue = msgs[msgIX];
}else{
if(navigator.appName == "Microsoft Internet Explorer") {
var heading = document.all.item("scrollme");
heading.innerText = msgs[msgIX];
}
}
++msgIX;
msgIX %= msgs.length;
}
</script>
<style>
#noticebox {
padding:"2px";
border-width:"2px";
border-style:inset;
border-color:blue;
width:174px;
height:"42px";
z-index:30;
text-align:-moz-center;
text-align:center;
background-color:"#ceg";
left:0;
}
</style>
<body onload="scrollMessge(2000)">

<div id="noticebox">
<p class="mainbold" id="scrollme"Notices and Messages</p>
</div>
Any assistance would appreciated--even if it entails telling me it
cant be done.

Thanks
DB.
.
I have made the changes as suggested. In Opera and Firefox the messages
cycle through correctly. In IE, the messages run through once and throw
an error:

Line 29
Char 5
Error 'msgs[...].0' is null or not an object
Code 0

Any suggestions?

DB
Aug 6 '06 #4
DavidB wrote:
<snip>
> var msgIX = 0;
var msgs = [
" Notices and Messages", \* RED *\
<snip>
> " Notices and Messages",];
^
<snip>
I have made the changes as suggested.
If you think about it you should see that responding by saying that the
code resulting form your changes to your original does not work, without
posting that code so people can see what you have done, is likely to be
counterproductive.
In Opera and Firefox the messages cycle through correctly.
In IE, the messages run through once and throw an error:

Line 29
Char 5
Error 'msgs[...].0' is null or not an object
Code 0
If a comma appears as the last character in an Array literal IE's
JScript erroneously uses it to imply a final undefined element in the
array, which makes the Array one longer than it would be in a correct
ECMAScript implementation, and when you iterate over the elements the
final one is undefined and will error if treated as an object.

Richard.

Aug 7 '06 #5
"Richard Cornford" <Ri*****@litotes.demon.co.ukwrote in news:eb6psi$kqk$1
$8*******@news.demon.co.uk:
DavidB wrote:
<snip>
>> var msgIX = 0;
var msgs = [
" Notices and Messages", \* RED *\
<snip>
>> " Notices and Messages",];
^
<snip>
>I have made the changes as suggested.

If you think about it you should see that responding by saying that the
code resulting form your changes to your original does not work, without
posting that code so people can see what you have done, is likely to be
counterproductive.
>In Opera and Firefox the messages cycle through correctly.
In IE, the messages run through once and throw an error:

Line 29
Char 5
Error 'msgs[...].0' is null or not an object
Code 0

If a comma appears as the last character in an Array literal IE's
JScript erroneously uses it to imply a final undefined element in the
array, which makes the Array one longer than it would be in a correct
ECMAScript implementation, and when you iterate over the elements the
final one is undefined and will error if treated as an object.

Richard.

Thanks Richard. The extra comma was the culprit.

DB
Aug 9 '06 #6

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

Similar topics

1
by: Preston Crawford | last post by:
I'm looking to quickly get a photo album online. Very simple, thumbnails, a few pages, maybe a description, but hopefully a small script that's easy to edit and work into my existing site. I know...
1
by: Allen | last post by:
I am trying to add an additional photo/hyperlink to the company web site (I didn't create it) without any luck. The mouseover feature 'highlights' pics by swapping them with another pic using this...
11
by: Konrad Den Ende | last post by:
I have a function returning a string but the problem is that the color of it is blue which suits me well for some pages but not for others. Is it possible to "feel" what the color of the background...
3
by: P Wolpert | last post by:
This is my first post. I hope I don't sound stupid. I have a script conflict when I put two scripts on one page. Both scripts will work if I use one at a time but the menu button script will not...
19
by: deko | last post by:
Is it possible to modify the background-image for a certain web page with PHP? I'd like to but a few buttons on the top of a page so readers can select from 2 or 3 different background images (or...
3
by: Amos0907 | last post by:
Dear All, I am a learner of Javascript. I got an assignment as below. Could anyone give me a suggestion for fixing the function of "onChangeColumn4()". The original requirement of the start date of...
3
by: Angus | last post by:
I have a web page with a toolbar containing a Save button. The Save button can change contextually to be a Search button in some cases. Hence the button name searchsavechanges. The snippet of...
5
by: camphor | last post by:
hi, I have found an upload script in hotscripts and have implemented it into the website, I followed the installation steps to 'give write permissions to php on the upload folder (which is...
5
by: kidders | last post by:
Below is a script i need to modify to work and its driving me nuts. Essentially it fades the content of an array. The original script specified the object name, I tried to modify it to allow an...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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.