473,385 Members | 1,320 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.

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 1500
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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...

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.