473,503 Members | 9,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16410
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
35213
by: Matt | last post by:
I created 3 hyperlinks, when the user click each link, it will change the color of the text of a link. For example, when user clicks Link1, text Link1 will become red color, but Link2 and Link3...
17
61399
by: Mike Gratee | last post by:
Is it possible to use JavaScript to cause the browser to click a link on a page and have the browser act exactly like the user had clicked on the link directly? In other words, I need to...
2
9141
by: Kevin Lyons | last post by:
Hello, Can anyone assist me with what I am trying to do with the following code (six different scenarios to try to make the functionality work correctly)? I want to always (and ONLY) display...
9
4465
by: Marco Krechting | last post by:
Hi All, I have a page with a list of hyperlinks. I want to save information in a cookie about the fact that I entered an hyperlink or not. When I click one of the hyperlinks I want this stored...
53
81658
by: usenet | last post by:
See <ul> <li><a name="link1" onClick="alert(this.name);return false;" href="#">Link1</a></li> <li><a name="link2" href="javascript:alert(this);">Link2</a></li> <li>Item 3</li> </ul> ...
11
2974
by: GaryB | last post by:
Hi Guys, I've been battling with this one for hours - I hope that you can help me! My code modifies the <aon a page, from a standard document link into a link with a tailored onclick event. ...
9
3216
by: monomaniac21 | last post by:
hi all i want to use hyperlinks to 'load' content by changing the display of div tags. the problem i have is that unless i specify a href the anchor does not change the mouse pointer on hover...
9
8540
by: whitgurley | last post by:
I've searched the web as well as I can for a solution to this problem but have found nothing and just don't know enough about JavaScript to figure out what's going. What I'm trying to do is convert...
1
3658
by: Miroslav Stampar [MCSD.NET / Security+] | last post by:
I have a problem that is best described by following code: <a href="http://www.google.com" onclick="new Image().src=\'http:// www.example.com/process.cgi?p=1\'">Google</a> I want to call cgi...
0
7095
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7294
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7015
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5602
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1523
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.