Calling a javascript function from an anchor tag?

E

elsenraat_76

Hello! I was wondering if someone could help me out with a problem I'm
having? I'm trying to input a javascript value into an anchor tag
(from a function), but don't have an event to call the function...if
that makes sense. Here's what I mean...

I have a javascript array set up like so:

<script language="javascript" TYPE="text/JavaScript">

//custom object constructor
function theImage(path, width, height, link)
{
this.path = path;
this.width = width;
this.height = height;
this.link = link;
}


var arrayImages = new Array()

arrayImages[0] = new theImage("/somelink_a.jpg", "216", "302",
"/somelink_b.jpg");
arrayImages[1] = new theImage(/anotherimage_a.jpg", "216", "302",
"/anotherimage_b.jpg");

etc, etc...


And I have the following javascript functions...


function getLink(i)
{
var currentLink = arrayImages.link;
return currentLink;
}

function getPath(i)
{
var currentPath = arrayImages.path;
return currentPath;

}

function getWidth(i)
{
var currentWidth = arrayImages.width;
return currentWidth;
}

function getHeight(i)
{
var currentHeight = arrayImages.height;
return currentHeight;
}


Now, for the HTML...

When I call these four functions through a body onLoad, like this:

<body class="background_color"
onLoad="alert(getLink(1));alert(getPath(1));alert(getWidth(1));alert(getHeight(1));">

the values '/anotherimage_a.jpg"', '216', '302', '/anotherimage_b.jpg'
are returned in the alert. But, I would like to have these values
returned in an anchor tage like so...

<a href="javascript:getLink(1);" target="_blank"><img
src="javascript:getPath(1);" width="javascript:getWidth(1);"
height="javascript:getHeight(1);" border="0"><br>LARGER IMAGE</a>

Is this possible? Is there a workaround for this? Any help would be
VERY appreciated!!
 
R

RobG

Hello! I was wondering if someone could help me out with a problem I'm
having? I'm trying to input a javascript value into an anchor tag
(from a function), but don't have an event to call the function...if
that makes sense. Here's what I mean...

I have a javascript array set up like so:

<script language="javascript" TYPE="text/JavaScript">

//custom object constructor
function theImage(path, width, height, link)
{
this.path = path;
this.width = width;
this.height = height;
this.link = link;
}


var arrayImages = new Array()

arrayImages[0] = new theImage("/somelink_a.jpg", "216", "302",
"/somelink_b.jpg");
arrayImages[1] = new theImage(/anotherimage_a.jpg", "216", "302",
"/anotherimage_b.jpg");

etc, etc...


And I have the following javascript functions...


function getLink(i)
{
var currentLink = arrayImages.link;
return currentLink;
}


Please don't post code with tabs, use 2 or four spaces for indents.
These functions can be written like this:

function getLink(i) {
return = arrayImages.link;
}

[...]
Now, for the HTML...

When I call these four functions through a body onLoad, like this:

<body class="background_color"
onLoad="alert(getLink(1));alert(getPath(1));alert(getWidth(1));alert(getHeight(1));">

the values '/anotherimage_a.jpg"', '216', '302', '/anotherimage_b.jpg'
are returned in the alert. But, I would like to have these values
returned in an anchor tage like so...

<a href="javascript:getLink(1);" target="_blank"><img
src="javascript:getPath(1);" width="javascript:getWidth(1);"
height="javascript:getHeight(1);" border="0"><br>LARGER IMAGE</a>

This seems pretty futile, but I'll play along anyway. Presumably the
A elements are in the HTML source and you want to use JavaScript to
add properties. You could use the links collection, but it's likely
you have other images in your document so you can use ID's. You
could also give all the A elements you want to modify the same name
and then use getElementsByName, the following example uses IDs.

Give all your A elements an id like "anchor-1", "anchor-2", etc.:

<html><head><title>blah</title>
</head><body onload="addAs();">
<script type="text/javascript">

function theImage(path, width, height, link) {
this.path = path;
this.width = width;
this.height = height;
this.link = link;
}

var arrayImages = [];
arrayImages[0] = new theImage("a.jpg", "216", "302","a.jpg");
arrayImages[1] = new theImage("a.jpg", "216", "302","a.jpg");

function addAs() {
if ( !document.getElementById
|| !document.createElement
|| !arrayImages ) {
return;
}
var x, i, j=arrayImages.length;
for ( i=0; i<j; i++ ){
x = document.getElementById('anchor-'+i);
if ( x ) {
x.href = getLink(i);
var oImg = document.createElement('img');
oImg.src = getPath(i);
oImg.width = getWidth(i);
oImg.height = getHeight(i);
oImg.alt =
'Sunset over the Outer Barcoo (where fences are few)';
x.appendChild(oImg);
}
}
}

function getLink(i) { return arrayImages.link; }

function getPath(i) { return arrayImages.path; }

function getWidth(i) { return arrayImages.width; }

function getHeight(i) { return arrayImages.height; }

</script>
<a id="anchor-0" href="" target="_blank">blah</a><br>
<a id="anchor-1" href="" target="_blank">blah</a><br>
</body></html>

[...]
 
S

scooter

Rob, thanks for your help!!! However, for some reason, the <body
onLoad="addAs();" is not finding the function! I added an alert to the
first line of the function addAs(), and the alert isn't firing. Any
idea on why it wouldn't be finding this function?

Thanks (again) in advance!

jason



Hello! I was wondering if someone could help me out with a problem I'm
having? I'm trying to input a javascript value into an anchor tag
(from a function), but don't have an event to call the function...if
that makes sense. Here's what I mean...

I have a javascript array set up like so:

<script language="javascript" TYPE="text/JavaScript">

//custom object constructor
function theImage(path, width, height, link)
{
this.path = path;
this.width = width;
this.height = height;
this.link = link;
}


var arrayImages = new Array()

arrayImages[0] = new theImage("/somelink_a.jpg", "216", "302",
"/somelink_b.jpg");
arrayImages[1] = new theImage(/anotherimage_a.jpg", "216", "302",
"/anotherimage_b.jpg");

etc, etc...


And I have the following javascript functions...


function getLink(i)
{
var currentLink = arrayImages.link;
return currentLink;
}


Please don't post code with tabs, use 2 or four spaces for indents.
These functions can be written like this:

function getLink(i) {
return = arrayImages.link;
}

[...]
Now, for the HTML...

When I call these four functions through a body onLoad, like this:

<body class="background_color"
onLoad="alert(getLink(1));alert(getPath(1));alert(getWidth(1));alert(getHeight(1));">

the values '/anotherimage_a.jpg"', '216', '302', '/anotherimage_b.jpg'
are returned in the alert. But, I would like to have these values
returned in an anchor tage like so...

<a href="javascript:getLink(1);" target="_blank"><img
src="javascript:getPath(1);" width="javascript:getWidth(1);"
height="javascript:getHeight(1);" border="0"><br>LARGER IMAGE</a>

This seems pretty futile, but I'll play along anyway. Presumably the
A elements are in the HTML source and you want to use JavaScript to
add properties. You could use the links collection, but it's likely
you have other images in your document so you can use ID's. You
could also give all the A elements you want to modify the same name
and then use getElementsByName, the following example uses IDs.

Give all your A elements an id like "anchor-1", "anchor-2", etc.:

<html><head><title>blah</title>
</head><body onload="addAs();">
<script type="text/javascript">

function theImage(path, width, height, link) {
this.path = path;
this.width = width;
this.height = height;
this.link = link;
}

var arrayImages = [];
arrayImages[0] = new theImage("a.jpg", "216", "302","a.jpg");
arrayImages[1] = new theImage("a.jpg", "216", "302","a.jpg");

function addAs() {
if ( !document.getElementById
|| !document.createElement
|| !arrayImages ) {
return;
}
var x, i, j=arrayImages.length;
for ( i=0; i<j; i++ ){
x = document.getElementById('anchor-'+i);
if ( x ) {
x.href = getLink(i);
var oImg = document.createElement('img');
oImg.src = getPath(i);
oImg.width = getWidth(i);
oImg.height = getHeight(i);
oImg.alt =
'Sunset over the Outer Barcoo (where fences are few)';
x.appendChild(oImg);
}
}
}

function getLink(i) { return arrayImages.link; }

function getPath(i) { return arrayImages.path; }

function getWidth(i) { return arrayImages.width; }

function getHeight(i) { return arrayImages.height; }

</script>
<a id="anchor-0" href="" target="_blank">blah</a><br>
<a id="anchor-1" href="" target="_blank">blah</a><br>
</body></html>

[...]
 
S

scooter

Actually, I finally got this thing to fire, however, the snippet x =
document.getElementById('ancho­r-'+i); is NULL. Any idea why it's
null? What value should it be showing in x?

Thanks in advance!
 
L

Lasse Reichstein Nielsen

scooter said:
Actually, I finally got this thing to fire, however, the snippet x =
document.getElementById('ancho­r-'+i); is NULL. Any idea why it's
null?

I haven't read the rest of the thread, but ID's may not contain the
character "-", so no matter what value "i" has, the resulting string
is not a valid id.

If the browser forgives you, and lets you use invalid strings for id's,
then you should still check that the document contains an element with
the id you are asking for.

/L
 
R

RobG

No. I tested the code in Safari & Firefox on Mac before posting, I've
now checked in Firefox and IE for Windows. The only issue seems to be
that Firefox on Windows does not display the images - I can't set the
image element's src attribute (either directly or using setAttribute).

But clicking on the image placeholders shows a new window with the
image. Go figure - I haven't sorted an answer to this one yet.
I haven't read the rest of the thread, but ID's may not contain the
character "-", so no matter what value "i" has, the resulting string
is not a valid id.

<URL:http://www.w3.org/TR/html4/types.html#type-name>

The above section of the HTML spec says:

"ID and NAME tokens must begin with a letter ([A-Za-z]) and may be
followed by any number of letters, digits ([0-9]), hyphens ("-"),
underscores ("_"), colons (":"), and periods (".")."

which seems to indicate that hyphens are OK, but I've misread the spec
before :-(

The hyphen isn't really necessary, it can be removed or replaced with
an underscore if required.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top