By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,678 Members | 1,920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,678 IT Pros & Developers. It's quick & easy.

Random image script with link, alt, and title attributes

P: n/a
I've found a basic script, however I also need to add alt and title
attributes as well, how would I go about doing this?

Here's the script I found:
Thanks

<script language="JavaScript">
<!--

/*
Random Image Link Script- By JavaScript Kit(http://www.javascriptkit.com)
Over 200+ free JavaScripts here!
Updated: 00/04/25
*/

function random_imglink(){
var myimages=new Array()
//specify random images below. You can have as many as you wish
myimages[1]="../images/s01.jpg"
myimages[2]="../images/s02.jpg"
myimages[3]="../images/s03.jpg"
myimages[4]="../images/s04.jpg"
myimages[5]="../images/s05.jpg"
myimages[6]="../images/s06.jpg"

//specify corresponding links below
var imagelinks=new Array()
imagelinks[1]="../photos/01.html"
imagelinks[2]="../photos/02.html"
imagelinks[3]="../photos/03.html"
imagelinks[4]="../photos/04.html"
imagelinks[5]="../photos/05.html"
imagelinks[6]="../photos/06.html"

var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img
src="'+myimages[ry]+'" border=0></a>')
}
random_imglink()
//-->
</script>

--
Olly

Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
sorry I missed this off, but if possible could it also be in a *.js file?

On Tue, 25 May 2004 16:49:11 +0100, Olly <ol********@yahoo.co.uk> wrote:
I've found a basic script, however I also need to add alt and title
attributes as well, how would I go about doing this?

Here's the script I found:
Thanks

<script language="JavaScript">
<!--

/*
Random Image Link Script- By JavaScript Kit(http://www.javascriptkit.com)
Over 200+ free JavaScripts here!
Updated: 00/04/25
*/

function random_imglink(){
var myimages=new Array()
//specify random images below. You can have as many as you wish
myimages[1]="../images/s01.jpg"
myimages[2]="../images/s02.jpg"
myimages[3]="../images/s03.jpg"
myimages[4]="../images/s04.jpg"
myimages[5]="../images/s05.jpg"
myimages[6]="../images/s06.jpg"

//specify corresponding links below
var imagelinks=new Array()
imagelinks[1]="../photos/01.html"
imagelinks[2]="../photos/02.html"
imagelinks[3]="../photos/03.html"
imagelinks[4]="../photos/04.html"
imagelinks[5]="../photos/05.html"
imagelinks[6]="../photos/06.html"

var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img
src="'+myimages[ry]+'" border=0></a>')
}
random_imglink()
//-->
</script>


--
Olly

Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 23 '05 #2

P: n/a
Lee
Olly said:

sorry I missed this off, but if possible could it also be in a *.js file?

On Tue, 25 May 2004 16:49:11 +0100, Olly <ol********@yahoo.co.uk> wrote:
I've found a basic script, however I also need to add alt and title
attributes as well, how would I go about doing this?

Here's the script I found:


[snipped]

You found some remarkably bad code.

Here's one solution that includes alt and title attributes.
The script block that I've put in the head could be placed into
a .js file. remember not to include the <script> and </script>
HTML tags in the .js file. The block that would appear in the
head would look something like:

<script type="text/javscript" src="../myscripts/myimgscript.js></script>

The small script block I've placed in the body can be placed
wherever you want the random link to appear.

You can use more than one random link in the page without
reusing any until you've used them all. After that, the
last link will repeat.
<html>
<head>
<script type="text/javascript">

var myimages = [
{ src : "../images/s01.jpg",
link : "../photos/01.html",
alt : "Alt Text 01",
title : "Title 01"
},

{ src : "../images/s02.jpg",
link : "../photos/02.html",
alt : "Alt Text 02",
title : "Title 02"
},

{ src : "../images/s0n.jpg",
link : "../photos/0n.html",
alt : "Alt Text 0n",
title : "Title 0n"
}
];
function random_imglink(){
var ry=Math.floor(Math.random()*myimages.length)
document.write('<a href="'+myimages[ry].link
+'"><img src="'+myimages[ry].src
+'" alt="'+myimages[ry].alt
+'" title="'+myimages[ry].title
+' " border=0></a>');
if(myimages.length>1){
myimages[ry]=myimages[myimages.length-1];
myimages.length--;
}
}
</script>
</head>
<body>
Here's the link: <script type="text/javascript">random_imglink()</script>
</body>
</html>

Jul 23 '05 #3

P: n/a
Thanks, the script works if the full code is placed in the head of the
document. However, if a link to a *.js file is used it doesn't work.

My *.html document code:
<script src="../random.js" type="text/javscript"></script>
</head>
<body>
<script type="text/javascript">random_imglink()</script>

My random.js document code:
// Generated by AceHTML Freeware http://freeware.acehtml.com
// Creation date: 2004-05-25

var myimages = [
{ src : "images/s01.jpg",
link : "photos/01.html",
alt : "Alt Text 01",
title : "Title 01"
},

{ src : "images/s02.jpg",
link : "photos/02.html",
alt : "Alt Text 02",
title : "Title 02"
},

{ src : "images/s0n.jpg",
link : "photos/0n.html",
alt : "Alt Text 0n",
title : "Title 0n"
}
];
function random_imglink(){
var ry=Math.floor(Math.random()*myimages.length)
document.write('<a href="'+myimages[ry].link
+'"><img src="'+myimages[ry].src
+'" alt="'+myimages[ry].alt
+'" title="'+myimages[ry].title
+' " border=0></a>');
if(myimages.length>1){
myimages[ry]=myimages[myimages.length-1];
myimages.length--;
}
}

On 25 May 2004 10:05:32 -0700, Lee <RE**************@cox.net> wrote:
Olly said:

sorry I missed this off, but if possible could it also be in a *.js
file?

On Tue, 25 May 2004 16:49:11 +0100, Olly <ol********@yahoo.co.uk> wrote:
I've found a basic script, however I also need to add alt and title
attributes as well, how would I go about doing this?

Here's the script I found:


[snipped]

You found some remarkably bad code.

Here's one solution that includes alt and title attributes.
The script block that I've put in the head could be placed into
a .js file. remember not to include the <script> and </script>
HTML tags in the .js file. The block that would appear in the
head would look something like:

<script type="text/javscript" src="../myscripts/myimgscript.js></script>

The small script block I've placed in the body can be placed
wherever you want the random link to appear.

You can use more than one random link in the page without
reusing any until you've used them all. After that, the
last link will repeat.


[snipped]

--
Olly

Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 23 '05 #4

P: n/a
Olly wrote:
I've found a basic script, however I also need to add alt and title
attributes as well, how would I go about doing this?

Here's the script I found:
Thanks

<script language="JavaScript">
<script type="text/javascript">
<!--
Not needed.
/*
Random Image Link Script- By JavaScript Kit(http://www.javascriptkit.com)
Over 200+ free JavaScripts here!
Updated: 00/04/25
*/

function random_imglink(){
var myimages=new Array()
//specify random images below. You can have as many as you wish
myimages[1]="../images/s01.jpg"
myimages[2]="../images/s02.jpg"
myimages[3]="../images/s03.jpg"
myimages[4]="../images/s04.jpg"
myimages[5]="../images/s05.jpg"
myimages[6]="../images/s06.jpg"
Arrays start at 0 in Javascript, by starting at 1, the author of this script
ended up using a hack at the later that results in the first image appearing
more often then the rest.

var myimages = [
"../images/s01.jpg",
"../images/s02.jpg",
"../images/s03.jpg",
"../images/s04.jpg",
"../images/s05.jpg",
"../images/s06.jpg"
];
//specify corresponding links below
var imagelinks=new Array()
imagelinks[1]="../photos/01.html"
imagelinks[2]="../photos/02.html"
imagelinks[3]="../photos/03.html"
imagelinks[4]="../photos/04.html"
imagelinks[5]="../photos/05.html"
imagelinks[6]="../photos/06.html"
var imagelinks = [
"../photos/01.html",
"../photos/02.html",
"../photos/03.html",
"../photos/04.html",
"../photos/05.html",
"../photos/06.html"
];

New code:

var imagealts = [
"alt text 1",
"alt text 2",
"alt text 3",
"alt text 4",
"alt text 5",
"alt text 6"
];

var imagetitles = [
"image title 1",
"image title 2",
"image title 3",
"image title 4",
"image title 5",
"image title 6"
];
var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
Replace this all with:

var img = Math.floor(myimages.length * Math.random());
document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img
src="'+myimages[ry]+'" border=0></a>')
}
document.write(
'<a href="' + imagelinks[img] + '"><img src="' +
myimages[img] + '" border="0" alt="' +
imagealts[img] + '" title="' +
imagetitles[img] + '"></a>'
);
random_imglink()
I have no idea what this does. You don't have the function random_imglink()
defined in the code you showed us. Perhaps the document.write() was once done
in "random_imglink()"? If so, you're now doing the document.write() as inline
Javascript, the function call is no longer required.
//-->
Not needed.
</script>

--
Olly

Using Opera's revolutionary e-mail client: http://www.opera.com/m2/


--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #5

P: n/a
Grant Wagner <gw*****@agricoreunited.com> writes:

function random_imglink(){

.... random_imglink()


I have no idea what this does. You don't have the function random_imglink()
defined in the code you showed us.


Easy to miss in the this badly indented source (although that might
not be the author's fault, but come from copying from a web page).

So, the code simply defines a function and then calls it. While
only polluting the namespace with one function, it could just as
well be done with an anonymous function.
Otherwise, I agree completely.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #6

P: n/a
Thanks it works great!!

On Tue, 25 May 2004 18:12:40 GMT, Grant Wagner
<gw*****@agricoreunited.com> wrote:
Olly wrote:
I've found a basic script, however I also need to add alt and title
attributes as well, how would I go about doing this?

Here's the script I found:
Thanks

<script language="JavaScript">


<script type="text/javascript">
<!--


Not needed.
/*
Random Image Link Script- By JavaScript
Kit(http://www.javascriptkit.com)
Over 200+ free JavaScripts here!
Updated: 00/04/25
*/

function random_imglink(){
var myimages=new Array()
//specify random images below. You can have as many as you wish
myimages[1]="../images/s01.jpg"
myimages[2]="../images/s02.jpg"
myimages[3]="../images/s03.jpg"
myimages[4]="../images/s04.jpg"
myimages[5]="../images/s05.jpg"
myimages[6]="../images/s06.jpg"


Arrays start at 0 in Javascript, by starting at 1, the author of this
script
ended up using a hack at the later that results in the first image
appearing
more often then the rest.

var myimages = [
"../images/s01.jpg",
"../images/s02.jpg",
"../images/s03.jpg",
"../images/s04.jpg",
"../images/s05.jpg",
"../images/s06.jpg"
];
//specify corresponding links below
var imagelinks=new Array()
imagelinks[1]="../photos/01.html"
imagelinks[2]="../photos/02.html"
imagelinks[3]="../photos/03.html"
imagelinks[4]="../photos/04.html"
imagelinks[5]="../photos/05.html"
imagelinks[6]="../photos/06.html"


var imagelinks = [
"../photos/01.html",
"../photos/02.html",
"../photos/03.html",
"../photos/04.html",
"../photos/05.html",
"../photos/06.html"
];

New code:

var imagealts = [
"alt text 1",
"alt text 2",
"alt text 3",
"alt text 4",
"alt text 5",
"alt text 6"
];

var imagetitles = [
"image title 1",
"image title 2",
"image title 3",
"image title 4",
"image title 5",
"image title 6"
];
var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1


Replace this all with:

var img = Math.floor(myimages.length * Math.random());
document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img
src="'+myimages[ry]+'" border=0></a>')
}


document.write(
'<a href="' + imagelinks[img] + '"><img src="' +
myimages[img] + '" border="0" alt="' +
imagealts[img] + '" title="' +
imagetitles[img] + '"></a>'
);
random_imglink()


I have no idea what this does. You don't have the function
random_imglink()
defined in the code you showed us. Perhaps the document.write() was once
done
in "random_imglink()"? If so, you're now doing the document.write() as
inline
Javascript, the function call is no longer required.
//-->


Not needed.
</script>

--
Olly

Using Opera's revolutionary e-mail client: http://www.opera.com/m2/


--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html


--
Olly

Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.