Reputation: 13
I want to open all external links in an iframe like digg/themeforest.net Most post on my site redirects users to external links and hence I want to use this method. I have searched a lot but couldn't find the answer I was looking for. Please help me get this done. Thank you. The solution I am looking for is
<a href="http://stackoverflow.com/posts/34472898/">Question</a>
When the link is clicked, if it is external it loads in iFrame
Upvotes: 0
Views: 1602
Reputation: 4137
First you need to check if your anchor's href is external or internal, if it is external then load it in iframe
var comp = new RegExp( "your_site_url" );
var http_regez = new RegExp( "http" ); // for http:// and https://
var www_regez = new RegExp( "www" );
jQuery('a').click(function(event){
var href_val = jQuery(this).attr('href');
var is_external = false;
if(comp.test(href_val)){
is_external=false;
}
else if( http_regez.test(href_val) || www_regez.test(href_val) ){
is_external=true;
}
else{
is_external=false;
}
if(is_external){
event.preventDefault();
jQuery("#externalLinkIframe").attr('src',href_val);
return false;
}
});
Upvotes: 0
Reputation:
Supposing your iframe has id your-iframe
var $iframeSelector = $("#your-iframe");
var currentDomain = document.location.protocol + '//' + document.location.hostname;
var outboundLinks = 'a[href^="http"]:not([href*="' + currentDomain + '"])';
$(document).on('click', outboundLinks, function(){
$iframeSelector.attr('src', $(this).attr('href'))
});
Just add this to any javascript after DOM is loaded (in $(document).ready(function(){... })
block)
Upvotes: 2