Solved

Javascript error on adding onClick event

Posted on 2011-09-04
7
158 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:Perumal_RM
Comment Utility
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
Comment Utility
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
Comment Utility
or you can attach event as
obj.onClick=function() { return customTrack('_trackPageView', '/news/"+txtFilterValue+"'); }
0
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
how do you establish that links are external?
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

744 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

15 Experts available now in Live!

Get 1:1 Help Now