Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / July 2005



Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jerry Sievers - 28 Jul 2005 13:00 GMT
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

Signature

-------------------------------------------------------------------------------
Jerry Sievers   305 854-3001 (home)     WWW ECommerce Consultant
               305 321-1144 (mobile    http://www.JerrySievers.com/

Christopher J. Hahn - 28 Jul 2005 13:33 GMT
> JS Programmers, "I'm a server-side coder PHP, Postgres etc...
>
[quoted text clipped - 18 lines]
>
> 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.
Christopher J. Hahn - 28 Jul 2005 13:44 GMT
> 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 &#61; 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.
Jerry Sievers - 31 Jul 2005 14:10 GMT
> > 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>

Signature

-------------------------------------------------------------------------------
Jerry Sievers   305 854-3001 (home)     WWW ECommerce Consultant
               305 321-1144 (mobile    http://www.JerrySievers.com/

Grant Wagner - 28 Jul 2005 18:04 GMT
> JS Programmers, "I'm a server-side coder PHP, Postgres etc...
>
[quoted text clipped - 18 lines]
>
> 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 />

Signature

Grant Wagner <gwagner@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq 

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.