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

Calling a javascript function from an anchor tag?

P: n/a
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[i].link;
return currentLink;
}

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

}

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

function getHeight(i)
{
var currentHeight = arrayImages[i].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!!

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
el**********@yahoo.com wrote:
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[i].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[i].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[i].link; }

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

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

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

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

[...]
--
Rob
Jul 23 '05 #2

P: n/a
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


RobG wrote:
el**********@yahoo.com wrote:
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[i].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[i].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[i].link; }

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

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

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

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

[...]
--
Rob


Jul 23 '05 #3

P: n/a
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!

Jul 23 '05 #4

P: n/a
"scooter" <el**********@yahoo.com> writes:
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
--
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 #5

P: n/a
Lasse Reichstein Nielsen wrote:
"scooter" <el**********@yahoo.com> writes:

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?

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.

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

--
Rob
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.