472,971 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Help to Modify Script

I really like this javascript for an image slideshow. The only thing I woudl
like to do differently is have a cell above the image and navigation that
would allow text content to be placed.

Does anyone have an idea of how that might be acomplished?

Header....

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
NewImg = new Array (
"http://pic1.jpeg"
,"http://pic2.jpeg"
,"http://pic3.jpeg"
,"http://pic4.jpeg"
);
var ImgNum = 0;
var ImgLength = NewImg.length - 1;

//Time delay between Slides in milliseconds
var delay = 3000;

var lock = false;
var run;
function chgImg(direction) {
if (document.images) {
ImgNum = ImgNum + direction;
if (ImgNum > ImgLength) {
ImgNum = 0;
}
if (ImgNum < 0) {
ImgNum = ImgLength;
}
document.slideshow.src = NewImg[ImgNum];
}
}
function auto() {
if (lock == true) {
lock = false;
window.clearInterval(run);
}
else if (lock == false) {
lock = true;
run = setInterval("chgImg(1)", delay);
}
}
// End -->
</script>
in body....

<img src="pic1.jpeg" name="slideshow">
<table>
<tr>
<td align="right"><a href="javascript:chgImg(-1)">Previous</a></td>
<td align="center"><a href="javascript:auto()">Auto/Stop</a></td>
<td align="left"><a href="javascript:chgImg(1)">Next</a></td>
</tr>
</table>
Jul 23 '05 #1
1 1486
scorpion53061 wrote:
I really like this javascript for an image slideshow.
No, you don't want that. It is badly written and error-prone.
The only thing I woudl like to do differently is have
a cell above the image and navigation that
would allow text content to be placed.

Does anyone have an idea of how that might be acomplished?
Make your array an array of objects, then use the
usual DOM methods to change the text node's value
according to the current object's property value.
Header....

<SCRIPT LANGUAGE="JavaScript">
<script type="text/javascript">
<!-- Begin
Nonsense, ask Google Groups.
NewImg = new Array (
The call operator should not be separated by whitespace from the
called function/method; this should be reserved for other operators
and operands.
"http://pic1.jpeg"
,"http://pic2.jpeg"
,"http://pic3.jpeg"
,"http://pic4.jpeg"
);
Why, are relative URIs not good enough?
var ImgNum = 0;
Only constructor identifiers should start uppercase.
var ImgLength = NewImg.length - 1;

//Time delay between Slides in milliseconds
var delay = 3000;

var lock = false;
var run;
function chgImg(direction) {
if (document.images) {
ImgNum = ImgNum + direction;
if (ImgNum > ImgLength) {
ImgNum = 0;
}
if (ImgNum < 0) {
ImgNum = ImgLength;
}
Hardly legible.
document.slideshow.src = NewImg[ImgNum];
This is proprietary referencing. As "slideshow" is the name
of an "img" element, use the document.images collection as
of DOM Level 2 and below:

document.images['slideshow'].src = ...;
}
}
function auto() {
if (lock == true) {
If `lock' is of type "boolean", no further comparison is required:

if (lock)

But you want to avoid globals.
lock = false;
window.clearInterval(run);
}
else if (lock == false) {
if (!lock)

But since you invert the value of lock every time, it
would be much easier to write

lock = !lock;
if (!lock)
{
// if lock *was* true
}
else
{
// if lock *was* false
}

and since `lock' only reflects `run' converted to boolean,
`run' can serve as condition here, making the following
clearInterval() call more reliable and `lock' unnecessary.
// End -->
Nonsense.
</script>
in body....

<img src="pic1.jpeg" name="slideshow">
<table>
<tr>
<td align="right"><a href="javascript:chgImg(-1)">Previous</a></td>
"javascript:" is nonsense here, see the FAQ. And you do not want to
use links that only work with JavaScript. Even if you do, write them
dynamically so that users without scripting do not encounter those
"malfunctioning" links.
<td align="center"><a href="javascript:auto()">Auto/Stop</a></td>
<td align="left"><a href="javascript:chgImg(1)">Next</a></td>
</tr>
</table>


And do not use tables solely for layout purposes.

This said, a better (yet suboptimal as e.g. not working
without client-side scripting) way for doing this is this
untested quick-hack:

<head>
...
<script type="text/javascript">
var aImg = new Array(
{url: "http://pic1.jpeg",
descr: "pic1"},
{url: "http://pic2.jpeg",
descr: "pic2"},
{url: "http://pic3.jpeg",
descr: "pic3"},
{url: "http://pic4.jpeg",
descr: "pic4"});
var num = 0;

function chgImg(direction)
{
var imgs;
if ((imgs = document.images))
{
num += direction;
var maxIdx;
if (num > (maxIdx = aImg.length))
{
num = 0;
}
else if (num < 0)
{
num = maxIdx;
}

var curImg = aImg[num];
imgs['slideshow'].src = curImg.url;

switch (domLevel)
{
case 3: // newer Mozilla/5.0, among others
descr.textContent = curImg.descr;
break;

case 2: // older Mozilla/5.0, among others
descr.firstChild.nodeValue = curImg.descr;
break;

case 0: // IE4, among others
descr.innerHTML = curImg.descr;
break;

case -1: // NN4
d.open("text/html");
d.write(curImg.descr);
d.close();
}
}

return false;
}

var run;

function auto(delay)
{
if (run)
{
window.clearInterval(run);
}
else
{
// setInterval() will not work everywhere;
// consider to setTimeout() a method
// that setTimeout()s itself instead
run = setInterval("chgImg(1)", delay);
}

return false;
}
</script>
...
</head>

<body>
<div><img src="pic1.jpeg" alt="..." name="slideshow"><br>
<span id="descr" style="position:relative; top:0"></span></div>

<script type="text/javascript">
// DOM feature detection
var descr;
if (document.getElementById) // DOM Levels 2 & 3
{
descr = document.getElementById("descr");
}
else if (document.all) // IE4 DOM
{
descr = document.all("descr");
}
else if (document.layers) // NN4 DOM
{
descr = document.layers[0];
}

var domLevel = -2, d;
if (descr)
{
if (typeof descr.textContent != "undefined")
{
domLevel = 3;
}
else if (typeof descr.firstChild
&& typeof descr.firstChild.nodeValue != "undefined")
{
domLevel = 2;
}
else if (typeof descr.innerHTML != "undefined")
{
domLevel = 0;
}
// omit this if you do not want to support NN4 anymore
else if ((d = descr.document)
&& d.open
&& d.write
&& d.close)
{
domLevel = -1;
}
}

document.write(
'<div style="text-align:center">'
+ '<a href="#" onclick="return chgImg(-1);">Previous<\/a>'
+ ' <a href="#" onclick="return auto(3000);">Auto/Stop<\/a>'
+ ' <a href="#" onclick="return chgImg(1);">Next<\/a><\/div>');
</script>
HTH

PointedEars

P.S.
Your From address is invalid which violates Netiquette and
Internet/Usenet standards. Change it to avoid to be killfiled.
Jul 23 '05 #2

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

Similar topics

2
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at...
4
by: Adrienne | last post by:
I am the first to admit that I know bupkis about javascript, except that sometimes I need it to do something client side that I can't do server side. Anyway, here's my problem: <input...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
3
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
11
by: tlyczko | last post by:
Hello Rob B posted this wonderful code in another thread, http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/c84d8538025980dd/6ead9d5e61be85f0#6ead9d5e61be85f0 I could not...
7
by: Novice Computer User | last post by:
Hi. Can somebody PLEASE help. I have spent hours on this.. but I am a total novice and can't seem to figure it out. Here is a .php script. Right now, the minimum amount of time (i.e. duration)...
2
by: Benjamin Rutt | last post by:
I often execute a long-running python script which is a "driver" for my application; it may run for several hours on a large input. Under CPython, is it safe for me to modify the Python script...
36
by: aljamala | last post by:
Hi, I keep getting this warning on a page, but I do not know what the problem is...does anyone have an idea about what could be wrong? line 88 column 7 - Warning: missing </formbefore <td> it...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
1
by: alibaaba | last post by:
HI All, i am a 4th year business student and i took web design online course for fun however i did not see that last 2 chapters were python programming.This has no relevance to my major nor does...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.