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
Solved

Javascript error on adding onClick event

Posted on 2011-09-04
7
161 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
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…

861 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