Question

I am using this script to force links to Open in a new Window with Jquery, and it works fine

// add external links
function addExternalLinks () { 

$("a[href*='http://']:not([href*='"+location.hostname.replace
       ("www.","")+"']), a.linkException").each(function() {
   if($(this).find('img ').length == 0) {
$(this).click(function(event) {
         event.preventDefault();
         event.stopPropagation();
         window.open(this.href, '_blank');
        }).addClass('externalLink').attr("title", $(this).attr("title")+" - ( This link will open in a new window )");   

        }
   });
}

HOWEVER, part of the page is using content loaded from an external HTML page, using LOAD.

function showInfo( info ) {
$("#layerinfo").load("descriptions.html #" + info );
};

I want the links contained within this loaded content to also be forced to open in a new widow with the same script. I cannot get it to work properly.

Something like :-

function showInfo( info ) {
var infoContent = "descriptions.html #" + info;

$("#layerinfo").load(infoContent,function(){
$("#layerinfo").html().addExternalLinks();
}); 
};

Any help greatly appreciated.

Was it helpful?

Solution

addExternalLinks is just a function, not a method of String (which is what .html returns) nor is it a jQuery method to be chained.

$("#layerinfo").load(infoContent, function () {
    addExternalLinks();
});

By the way, for addExternalLinks couldn't you just add .attr("target", "_blank") to said links instead of using a click event?

OTHER TIPS

try to add attr instead:

$(this).attr("target", "_blank");

hope this help!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top