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

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

P: n/a
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
Share this Question
Share on Google+
4 Replies


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

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

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

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