[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How can I apply two (2) 'onclick' events to a single link?

Posted on 2009-12-17
12
Medium Priority
?
771 Views
Last Modified: 2013-12-12
Experts this is a two part question:

1.  I need to figure out how to use two onclick events in the same link code.
2.  I need to insert php coding inside the second onclick event which gives each link a unique name that can be tracked using Google Analytics.

I use php to dynamically create file links on my page.  Here is an example link:

<a href="'.$getinfo['FilePath'].'" title="'.$getinfo['File'].'" onclick="return confirm(&quot;Notice:  You are about to view privileged information.)">

The code above creates a link using the 'FilePath' stored in the database which leads to an Excell file.  It also gives each link a unique title by using the value of 'File' stored in the database.  On clicking this link it also creates a popup window which states "Note: You are about to view privileged information."

First Question:
How can I add a second onclick event which adds the ability for Google Analytics to be able to track the clicking of this file link.  Google Analytics says to use the following onclick event example to do so:

onClick="javascript: pageTracker._trackPageview('/downloads/map');"

Second Question:
Given the provided Google Analytics code, how can I insert php code into this javascript so that each link is tracked with a unique name?  This is what I was thinking:

onClick="javascript: pageTracker._trackPageview('<?php .$getinfo['File']. ?>');"

I think this may be an easy question but, I just wanted to make sure I provide the information necessary to answer it.

Cheers!
0
Comment
Question by:evibesmusic
  • 7
  • 3
  • 2
12 Comments
 
LVL 14

Expert Comment

by:TedInAK
ID: 26077014
This should work:
<a href="'.$getinfo['FilePath'].'" title="'.$getinfo['File'].'" onclick="pageTracker._trackPageview('<?php .$getinfo['File']. ?>'); return confirm(&quot;Notice:  You are about to view privileged information.)">

Open in new window

0
 
LVL 14

Assisted Solution

by:TedInAK
TedInAK earned 1000 total points
ID: 26077019
You can have multiple functions run in a single onclick, like

onclick="doThis(); doThat(); return Confirm...etc.

Just separate each statement with a semicolon (;).
0
 
LVL 14

Expert Comment

by:TedInAK
ID: 26077025
Oh, wait...just realized that you only want to do the analytics if the user chooses "Yes" (or "Ok" or whatever...my JS is a little rusty).  Nevermind.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 14

Assisted Solution

by:TedInAK
TedInAK earned 1000 total points
ID: 26077088
You can put the following code in a <script> block.  Then your link would look like:

<a href="'.$getinfo['FilePath'].'" title="'.$getinfo['File'].'" onclick="">

function ConfirmClick() {
	var answer = confirm("Notice:  You are about to view privileged information.");
	if (answer) { 
		pageTracker._trackPageview("<?php .$getinfo['File']. ?>");
	}
	return answer;
}

Open in new window

0
 
LVL 14

Expert Comment

by:TedInAK
ID: 26077324
Sorry, long day...the link should be:

<a href="'.$getinfo['FilePath'].'" title="'.$getinfo['File'].'" onclick="return ConfirmClick();">
0
 
LVL 5

Expert Comment

by:manjunathub
ID: 26077815
Better thing use event handlers to attach multiple events of onclick on same object
0
 

Author Comment

by:evibesmusic
ID: 26083745
@TedInAK:

Thanks for your suggestions.  I will implement them and let you know if I have any issues.  Cheers!

@manjunathub:

Can you give an example of an event handler that you would use to accomplish this?  As a learning experience, can you give insight into why you feel this way is better to use?  Cheers!
0
 
LVL 14

Expert Comment

by:TedInAK
ID: 26084653
You're welcome.  I believe manjunathub was intending to say "event listeners".   I'd suggest googling "javascript event listeners" if you want more info, because there's much stuff written on it and is much more eloquent (and correct) than I could hope to say.
0
 

Author Comment

by:evibesmusic
ID: 26085078
@TedInAK:

Thanks TedInAK I've implemented your code and am now waiting for the Analytics system to verify that it is working correctly.  I'll get back to you over the weekend I'm sure.

Cheers!
0
 
LVL 5

Accepted Solution

by:
manjunathub earned 1000 total points
ID: 26093697
i have given u an example

for more information just look at this site

http://www.howtocreate.co.uk/tutorials/javascript/domevents

https://developer.mozilla.org/en/DOM/element.addEventListener

http://ditio.net/2009/04/25/javascript-addeventlistener-method/
<html>
<head>
	<script language="javascript">
		function click1(){
			alert("Click1");
		}
		function click2(){
			alert("clcik2")
		}
		function click3(){
			alert("clcik3");
		}
		function removeClick1(obj){
			var obj = document.getElementById("click");
				removeEventSimple(obj, 'click', click1);
			alert("removed first Click event");
			
		}
		function removeEventSimple (obj,evt,fn) {//remove eventHandlers for crossbrowser compatible
	            if (obj.removeEventListener)
		            obj.removeEventListener(evt,fn,false);
	            else if (obj.detachEvent)
		            obj.detachEvent('on'+evt,fn);
		}
		function addEventSimple(obj,evt,fn) { // asign eventHandlers for crossbrowser compatible
	        if (obj.addEventListener)
		        obj.addEventListener(evt,fn,false);
	        else if (obj.attachEvent)
		        obj.attachEvent('on'+evt,fn);
		}
		function addClickEvent(){
			var obj = document.getElementById("click");
				addEventSimple(obj, 'click', click1);
				addEventSimple(obj, 'click', click2);
				addEventSimple(obj, 'click', click3);
		}
		window.onload = addClickEvent;
		//window.onload = addClickEvent;
		
	</script>
</head>
<body>
	<a href="#" id="click" >click here</a> 
	
	<br/>
	<a href='#' id="remove" onclick="removeClick1()"> remove 1st event </a>
</body>
</html>

Open in new window

0
 

Author Comment

by:evibesmusic
ID: 26115241
Thanks to the both of you for your assistance.  Learned about event handlers and how to include mutliple onclick events into one link.

Cheers and Happy Holidays from the US.

EVibesMusic
0
 
LVL 14

Expert Comment

by:TedInAK
ID: 26116814
You're most welcome. :-)  Merry Christmas to you and all.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses
Course of the Month18 days, 11 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question