Solved

Javascript error on adding onClick event

Posted on 2011-09-04
7
159 Views
Last Modified: 2012-05-12
Hi,

I have the following function which could help me in looping through all the external links inside my current page and will add target="_blank".

what i need to do is to write a function that will add some onclick value which will send some tracking event. Not sure how to proceed, Any clues?

//Open external links in new window START
this.blankwin = function(){
	var hostname = window.location.hostname;
	hostname = hostname.replace("www.","").toLowerCase();
	var a = document.getElementsByTagName("a");	
	this.check = function(obj){
		var href = obj.href.toLowerCase();
		return (href.indexOf("http://")!=-1 && href.indexOf(hostname)==-1) ? true : false;				
	};
	this.set = function(obj){
		obj.target = "_blank";
		var txtFilterValue=obj.text;
		var onClickValue="_gaq.push(['_trackPageView', '/news/"+txtFilterValue+"']);"
		$(this).attr("onClick", onClickValue);
		//obj.className = "external";
	};

	for (var i=0;i<a.length;i++){
		if(check(a[i])) set(a[i]);
	};
};

Open in new window

0
Comment
Question by:Perumal_RM
  • 3
  • 2
  • 2
7 Comments
 

Author Comment

by:Perumal_RM
ID: 36482627
var txtFilterValue=obj.text;
            var onClickValue="_gaq.push(['_trackPageView', '/news/"+txtFilterValue+"']);"
            $(this).attr("onClick", onClickValue);

The above code snippet is the area of interest for me.
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36482685
Sorry, not able to understand the question.
Are you trying to understand what is happening in this code?
Or
You know what is the problem with the code and want to rectify the same?

Please give some details
0
 
LVL 13

Expert Comment

by:ansudhindra
ID: 36482698
as I understood..
you need to write a custom function which will send the info to the server on click of the link... so that you can update the page views in your server.. right?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Perumal_RM
ID: 36482811
Sorry for not being clear. Apologize.

I want to add some value to the onClick function.

For example...
<a href="www.yahoo.com">Yahoo</a>
The existing code would add target="_blank", what i want is to add something which will add an OnClick event to the same URL with some value on it.
The end URL should be
<a href="www.yahoo.com" onClick="_gaq.push(['_trackPageView', '/news/"+txtFilterValue+"']);">yahoo</a>

The code what i wrote above was a crap, I am not sure even if it will work.
0
 
LVL 13

Expert Comment

by:ansudhindra
ID: 36482831
you can do something like...

function customTrack(at1,at2){
    _gaq.push([at1,at2]);
    return true;
}

in your script change to..

var onClickValue="return customTrack('_trackPageView', '/news/"+txtFilterValue+"');";
$(this).attr("onClick", onClickValue);
0
 
LVL 13

Accepted Solution

by:
ansudhindra earned 500 total points
ID: 36482911
or you can attach event as
obj.onClick=function() { return customTrack('_trackPageView', '/news/"+txtFilterValue+"'); }
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36483129
how do you establish that links are external?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

930 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now