Solved

Javascript error on adding onClick event

Posted on 2011-09-04
7
164 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

724 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