469,649 Members | 1,676 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,649 developers. It's quick & easy.

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

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&tex t=anchor.text)"

Please advise.

TIA
--
-------------------------------------------------------------------------------
Jerry Sievers 305 854-3001 (home) WWW ECommerce Consultant
305 321-1144 (mobile http://www.JerrySievers.com/
Jul 28 '05 #1
4 16218
Jerry Sievers wrote:
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&tex t=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.

Jul 28 '05 #2
Christopher J. Hahn 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);"

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.

Jul 28 '05 #3
"Jerry Sievers" <je***@jerrysievers.com> wrote in message
news:m3************@prod01.jerrysievers.com...
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&tex t=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/...17/439798.aspx />

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 28 '05 #4
"Christopher J. Hahn" <cj****@gmail.com> writes:
Jerry Sievers wrote:
Is there a way to code an onClick event to call a script and pass the
href URL and link text?

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))"this is it</a>


--
-------------------------------------------------------------------------------
Jerry Sievers 305 854-3001 (home) WWW ECommerce Consultant
305 321-1144 (mobile http://www.JerrySievers.com/
Jul 31 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Mike Gratee | last post: by
9 posts views Thread by Marco Krechting | last post: by
53 posts views Thread by usenet | last post: by
1 post views Thread by Miroslav Stampar [MCSD.NET / Security+] | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.