あなたはあなたがそのような何かができるリンクに余分なクラスを追加することができた場合:
<a href="http://www.externalsite.com/" class="external">AD</a>
すなわち、すべてのリンクに「外部」クラスを追加し、このようにjQueryを使用します。
デフォルトの操作(ユーザーのリダイレクト)を防ぎ、何か他の操作を行う場合は、event.preventDefault()
を使用することもできます。
EDIT あなたは=「外部」直接マークアップにクラスを追加することができない場合のように、あなたはdinamycallyいくつかのコードを使用してjQueryのを使用して、それを追加することができます。私はこの第二をテストしていませんでした
$('a').each(function() {
if($(this).attr('href') is an external link) {
$(this).addClass('external');
}
});
スニペットが動作するはずです。
jQuery(document).ready(function($) {
$('body').on('click', 'a[href^="http"]:not([href*="//' + location.host + '"])', function(){
if (typeof(_gaq) !== 'undefined') {
_gaq.push(['_trackEvent', 'outbound', 'click', this.href.match(/\/\/([^\/]+)/)[1]]);
_gaq.push(['t2._trackEvent', 'outbound', 'click', this.href.match(/\/\/([^\/]+)/)[1]]);
}
});
});
:
// Creating custom :external selector
$.expr[':'].external = function(obj){
return !obj.href.match(/^mailto\:/)
&& (obj.hostname != location.hostname);
};
// Manage clicks on external links
$('a:external').click(function() {
// do your stuff here, ie send the link href to some server-side script
$.ajax({ type: "POST", url: "some.php", data: "href="+$(this).attr("href") });
});
最初の質問:「外部」とはiframe内のリンクを意味しますか? 2番目の質問:あなたはそれを検出したいと何をしたいですか? –