0

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

Jerry
  • 13
  • 7
  • See answer here - http://stackoverflow.com/questions/15659749/open-link-from-variable-in-iframe – Armen Dec 26 '15 at 15:58
  • @Armen how to implement this on WordPress? As I said whenever users click on my post's title he/she is redirected to an external site. I want to handle that. – Jerry Dec 26 '15 at 16:03

2 Answers2

2

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)

0

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;
   }
});
Scarecrow
  • 4,057
  • 3
  • 30
  • 56
  • Okay... And the external iframe? How do i connect my iframe with this script? – Jerry Dec 27 '15 at 04:11
  • 1
    @Jerry I am not geting your problem, are you having ` Load denied by X-Frame-Options` error? then use `header("X-Frame-Options: GOFORIT");` before your iframe. Please comment if you are having any other problem. – Scarecrow Dec 27 '15 at 05:05