onClick to call script with href and link test as parms?

J

Jerry Sievers

JS Programmers, "I'm a server-side coder PHP, Postgres etc...

My question; Given the following anchor

<a href="http://www.somesite.com/somefile.html">link text</a>

Is there a way to code an onClick event to call a script and pass the
href URL and link text?

That is; We'd like to put an identical piece of JS code into each
anchor tag essentially to have the anchors, when clicked call one
script which does logging of the URL and link text before doing a
redirect.

With no working knowledge in JS, only some past background reading, my
assumption is that something like this may be valid;

onClick="load(loggingScript.php?url=anchor.url&text=anchor.text)"

Please advise.

TIA
 
C

Christopher J. Hahn

Jerry said:
JS Programmers, "I'm a server-side coder PHP, Postgres etc...

My question; Given the following anchor

<a href="http://www.somesite.com/somefile.html">link text</a>

Is there a way to code an onClick event to call a script and pass the
href URL and link text?

That is; We'd like to put an identical piece of JS code into each
anchor tag essentially to have the anchors, when clicked call one
script which does logging of the URL and link text before doing a
redirect.

With no working knowledge in JS, only some past background reading, my
assumption is that something like this may be valid;

onClick="load(loggingScript.php?url=anchor.url&text=anchor.text)"

Please advise.

TIA

I'd go with something like this (based on your above suggestion):
onClick="location.href='/loggingScript.php?url='+
this.href+'&text='+this.text;return(false);"

For the record, though, this can break any other onClick code you may
have, as well as making the code more unreadable.
 
C

Christopher J. Hahn

Christopher said:
I'd go with something like this (based on your above suggestion):
onClick="location.href='/loggingScript.php?url='+
this.href+'&text='+this.text;return(false);"

For the record, though, this can break any other onClick code you may
have, as well as making the code more unreadable.

Ugh... again, I do this thing with text properties.

..text won't work.

That, and the ampersand *should* be escaped into an HTML entity
(&amp;). You *may* also need to do the same with the equal sign (not
really sure, but = will do it).

onClick="location.href='/loggingScript.php?url='+
this.href+'&amp;text='+this.innerHTML;return(false);"


Be advised that this will include any HTML between the <a> and </a>
tags as part of the link text, and that innerHTML isn't part of any
written standards.


The more complicated solution is to implement a function:

function load( href, text ) {
location.href =
'/loggingScript.php?url=' + href + '&text=' +
text.replace( /<.*?>/g, '' );

return false;
}


and an onlick:
onclick="return(load(this.href,this.innerHTML))"

Though HTML *entities* (&thing; constructs) within the link text will
not be converted.

This will also still break any other onclick code you may have wanted
to wrap in your <a> tags.
For those, use:
onclick="old_onclick_code_here;return(load(...

et cetera.
 
G

Grant Wagner

Jerry Sievers said:
JS Programmers, "I'm a server-side coder PHP, Postgres etc...

My question; Given the following anchor

<a href="http://www.somesite.com/somefile.html">link text</a>

Is there a way to code an onClick event to call a script and pass the
href URL and link text?

That is; We'd like to put an identical piece of JS code into each
anchor tag essentially to have the anchors, when clicked call one
script which does logging of the URL and link text before doing a
redirect.

With no working knowledge in JS, only some past background reading, my
assumption is that something like this may be valid;

onClick="load(loggingScript.php?url=anchor.url&text=anchor.text)"

Please advise.

TIA

Horribly bad idea.

"Note that you should not build a redirect page that takes an arbitrary
URL, as this can be abused in cross-domain attacks. Always build your
redirect pages to take an opaque identifier that then maps back to a URL
you control and know to be trustworthy."

<url: http://blogs.msdn.com/ptorr/archive/2005/07/17/439798.aspx />
 
J

Jerry Sievers

Christopher J. Hahn said:
Jerry Sievers wrote:
I'd go with something like this (based on your above suggestion):
onClick="location.href='/loggingScript.php?url='+
this.href+'&text='+this.text;return(false);"

Thank you for this suggestion as it was the basis for a simple
solution to my project needs. Within the scope of the project, some
security concerns raised by another respondent were considered but
dismissed.


function doit(obj)
{
location.href='logger.php?url='+obj.href+'&text='+obj.text;
return(false);
}

<a href="foobar.html"
onClick="return(doit(this))"
 

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,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top