Solved

Javascript error on adding onClick event

Posted on 2011-09-04
7
160 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
geting geocode of a user with using Google maps api 8 34
How to make this slider behave as it should with javascript 5 28
How to make a moving character 3 55
HTML5 frame 5 25
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

786 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