View Full Version : two questions
shashgo
09-24-2002, 04:18 AM
hav two questions
1) I need to know how I can use javascript to refer to pages that are linked to. I tried to display document.referrer on the new page but it doesnt return anything
so if I have a page (say one.htm) with a link
<a href="two.htm">two</a>
and in two.htm I write
<script language="javascript">
document.write("Referred to by: "+document.referrer);
</script>
then shouldnt h output be Referred to by: one.htm
I only get Referred to by:
Why is it not working? What am I doing wrong?
-----------------
2) I want to design a web page where I use javascript to load all the images on my hard drive without me having to specify each individual name. The images have filenames that are 1.jpg, 3.jpg, 4.jpg, 7.jpg, 10.jpg, 15.jpg, 30.jpg,... no order.
How can I accomplish this?
glenngv
09-24-2002, 04:28 AM
document.referrer returns empty when the page is running offline (runs locally), the page must be online (you must run it in a web server).
Q1: The referrer proprety only works on absolute url's - i.e. http://www.somedomain.net/page1.htm
Q2: This should work, assuming your images are all in a sub-directory called "images"
<html>
<head>
<title></title>
<script language="javascript" type="text/javascript">
<!--
function outputImages(){
var imgArray=new Array()
var pth="images/"
var printOut=""
var numberOfImages=2 //change this to the number of images you want to display
printOut+="<table width=\"40%\" align=\"center\" border=\"1\">"
for(i=0;i<numberOfImages;i++){
imgArray[i]=new Image()
imgArray[i].src=pth+(i+1)+".jpg"
printOut+="<tr><td><img src=\""+imgArray[i].src+"\" border=\"0\"></a></td></tr>"
}
printOut+="</table>"
document.write(printOut)
}
// -->
</script>
</head>
<body>
<div style="text-align:center;">
<h1>Image Collection</h1>
</div>
<script language="javascript" type="text/javascript">
outputImages()
</script>
</body>
</html>
shashgo
09-24-2002, 06:02 PM
Hey umm and glenngv,
Your answer to question #1 are really helpful. I didnt know that document.referrer doesnt work offline. I think I should upload my page to a server and then see if it works. Thanks for the tip.
umm,
Youre script for #2 works great, and it is exactly what I had when my directory contained images tht had sequential file names as in 1.jpg, 2.jpg,3.jpg,... But now the images are like 1.jpg,5.jpg,6.jpg,10.jpg,11.jpg,12.jpg20.jpg, since Ive deleted some of the bad pictures.
If there is no javascript that can read files off the server, then is there some javascript code that can make the server files become sequential so I can use your script?
by the way, i like your signature. Do nothing. why get stressed. theres always tomorrow
shashgo
09-24-2002, 09:52 PM
I was wondering: Is there a way to check whether an image exists or not, by comparing it to null or something?
Example:
for (i=0;i<5;i++)
{
image=new Image();
image.src=(i+1)+".jpg";
if (image==null)
document.write("Image "+(i+1)+".jpg not found");
else
document.write("<img src='image.src'><br>");
}
So if the image is not found, an erro message is displayed otherwise the image is displayed. Is something like this possible?
Give this a go. I worked OK in Moz1.0 and it didn't generate any empty table cells for unfound images. I suspect there is a better solution but I don't have it a hand.
<html>
<head>
<title></title>
<script language="javascript" type="text/javascript">
<!--
function outputImages(){
var imgArray=new Array()
var pth="images/"
var printOut=""
var numberOfImages=2 //change this to the number of images you want to display
printOut+="<table width=\"40%\" align=\"center\" border=\"1\">"
for(i=0;i<numberOfImages;i++){
imgArray[i]=new Image()
imgArray[i].src=pth+(i+1)+".jpg"
if(!imgArray[i].complete)
alert(imgArray[i].src+" NOT COMPLETE")
else
printOut+="<tr><td><img src=\""+imgArray[i].src+"\" border=\"0\"></a></td></tr>"
}
printOut+="</table>"
document.write(printOut)
}
// -->
</script>
</head>
<body>
<div style="text-align:center;">
<h1>Image Collection</h1>
</div>
<script language="javascript" type="text/javascript">
outputImages()
</script>
</body>
</html>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.