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

Display of the image from JavaScript

Hi,

I have problem with JavaScript for gallery of images.
From the overview page with thumbnails, after a click on the small
picture,
the subpage 'slide_show.html' is called, with a number of picture as
parameter
for example: <a href=slide_show.html?nb=2>
(The code is at the end of this post)

The page 'slide_show.html' should display the appropriate picture
and give the possibility to navigate previous/next.

Result: some browsers don't display the first picture. It looks like a
browser
finished to display the picture before it resolved its name.
Some browsers show the first picture ok.
The navigation previous/next works always ok.

What is wrong? I tried to move the code inside the page but it didn't
help.
With the help of document.write I can see, that the parsing of
parameter
and finding the name of the picture was ok, but it wasn't displayd.

Do you have any ideas?

Best regards,
K.

<HTML>

<HEAD>

<SCRIPT LANGUAGE="JavaScript">

NewImg = new Array (
"P0000284.jpg",
"P0000286.jpg",
"P0000287.jpg",
"P0000288.jpg",
"P0000291.jpg"
);

var current = 0;
var len = NewImg.length - 1;
var param = location.search;

param = param.substr(4,param.length);
current = eval(param);

document.write( "location.href: ", location.href, "<BR>" );
document.write( "location.search: ", location.search, "<BR>" );
document.write( "param: ", param, "<BR>" );
document.write( "current: ", current, "<BR>" );

function showImg()
{
document.slideshow.src = NewImg[current];
}

function chgImg(direction)
{
if (document.images)
{
current = current + direction;
if (current len)
{
current = 0;
}
if (current < 0)
{
current = len;
}
document.slideshow.src = NewImg[current];
}
}
</script>

</HEAD>

<BODY>

<a href="javascript:chgImg(-1)">Previous</a>
<a href="javascript:chgImg(1)">Next</a>
<br><br>
<img src="javascript:showImg()" name="slideshow">
<br><br>
<a href="javascript:chgImg(-1)">Previous</a>
<a href="javascript:chgImg(1)">Next</a>

</BODY>

</HTML>
Nov 14 '08 #1
3 3830
Hello Krzysztof,

the problem is, that the call of showImg() takes place to early in many
cases, so some browsers get confused about establishing the image object and
dynamically creating its source. To solve this, use first a placeholder
image (e. g. a transparent GIF called space.gif) and second put the
showImg() call into the onload event handler of the body tag.

So your body tag then looks like
<BODY onload="showImg()">
and your image tag looks like
<img src="space.gif" name="slideshow">

This guarantees that the javascript function gets called AFTER the image tag
object has been loaded.
Instead of using such a placeholder image you can also place the right first
image, of course, which you generate dynamically. For example:
<SCRIPT LANGUAGE="JavaScript">
document.write('<img src="' + NewImg[current] + '" name="slideshow">');
</script>
It depends on you, which image tag you prefer.

I hope that helps you.

Nice greetings from
Codaholic
Nov 16 '08 #2
SAM
Le 11/14/08 3:43 PM, kr*****************@gmail.com a écrit :
Hi,

I have problem with JavaScript for gallery of images.
From the overview page with thumbnails, after a click on the small
picture,
the subpage 'slide_show.html' is called, with a number of picture as
parameter
for example: <a href=slide_show.html?nb=2>
(The code is at the end of this post)

The page 'slide_show.html' should display the appropriate picture
and give the possibility to navigate previous/next.

Result: some browsers don't display the first picture. It looks like a
browser
finished to display the picture before it resolved its name.
Some browsers show the first picture ok.
The navigation previous/next works always ok.

What is wrong?
that :
<img src="javascript:showImg()" name="slideshow">
can't work
<html>
<head>
<title>My Gallery</title>
<script type="text/javascript">

NewImg = [
"P0000284.jpg",
"P0000286.jpg",
"P0000287.jpg",
"P0000288.jpg",
"P0000291.jpg"
];

var current = 0;
if(self.location.search.length>1)
current = self.location.toString().split('=')[1]*1;
var len = NewImg.length;

function showImg()
{
if (document.images)
document.slideshow.src = NewImg[current];
document.getElementById('comment').innerHTML = NewImg[current];
window.status = 'View #'+(current+1); return true;
}

function chgImg(direction)
{
if(typeof direction == 'undefined') direction=0;
current = current + direction;
current = current>=len? 0 :
current<0? len :
current;
showImg();
}

window.onload = function() { chgImg(); };
</script>

</head>
<body style="text-align:center">
<p>
<button onclick="chgImg(-1);">Previous</button>
<button onclick="chgImg(1);">Next</button>
<br><br>
<img src="" name="slideshow" alt="viewer"><br>
<span id="comment">legend</span><br>
<a href="javascript:chgImg(-1)">Previous</a>
<a href="javascript:chgImg(1)">Next</a>
</p>
</body>
</html>

--
sm
Nov 16 '08 #3
Hi Codaholic,

thanks a lot for your tips.

I've found next possibility:
<img name="slideshow" src="javascript:showImg()" onError="showImg()">

but your seems to be better!

Regards,
K.
Nov 17 '08 #4

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

Similar topics

6
by: Sandman | last post by:
I'm having some problem here... I have a javascript I've downloaded that goes through all PNG files and enables the transparency channel in them for IE5.5+ by converting them to SPAN layers with...
1
by: Qingdong Z. | last post by:
I have a web page to work as Video Web Viewer. It pulls image from IP camera using JavaScript. It works great in most of time. When users are in slow network, the Internet Explore begins to...
6
by: Ben | last post by:
I am trying to write codes in VB.net to display 3 images in series on a Webform with each image being displayed in the same box for 5 seconds. I am trying to use Timer and image button, don't...
4
by: DoomedLung | last post by:
I have been developing an image gallery which needed to be dynamic as possible. so I have placed the gallery in a div plus a div containing the large image to be displayed. It works by changing...
3
by: DoomedLung | last post by:
I have been developing an image gallery which needed to be dynamic as possible. so I have placed the gallery in a div plus a div containing the large image to be displayed. It works by changing...
6
by: yk | last post by:
Hi, Is it a technique available in html/javascript in order to display same image many many times on a same page? Because of a large page loading I am looking for a way not to have same...
4
by: ArrK | last post by:
I want to use a control frame to create and/or modify the content (images and text) of a display frame - all files and images are client side. . When I try to write to the display frame from the...
3
by: mylog | last post by:
I have a problem regarding open the enlarged image in a new window. What I am facing problem is I have to open put the image in the div from Javascript which I have done well and now what I need is...
2
by: Tim Streater | last post by:
The following test page is intended to allow the user to choose an image file, and then display it. It works as expected in Safari 3.1.1, FF 2.0.0.14 (Mac), and IE7 (XP). But, it fails in FF...
3
by: =?Utf-8?B?QmlsbHkgWmhhbmc=?= | last post by:
I have a asp.net app, in the page there is a scan activex which could scan and save a jpg file in client harddisk. How could we access and display this jpg file on the fly using js in the client...
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
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
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.