javascript noob trying to figure something out. I saw a script located here:
Using jQuery to open all external links in a new window
in which urls on a page are checked to see if they match the current page url. If not, this particular script would open them in a new window. I would like to use the same test on urls but if they are external I would like ?iframe to be appended to each url, a la fancybox syntax. My noob script is:
$(document).ready(function() {
$('a[@href^="http://"]').filter(function() {
return this.hostname && this.hostname !== location.hostname;
}).append('?iframe')
});
which I believe is incorrect. Any assistance? Thanks
You have to change the
hrefattribute usingattr, notappendnew child elements:Note that
hostnameis only available in HTML5. So in browsers not supporting HTML 5, the filter would discard every element (Update: Given that the question you linked to is from 2009, it seems browsers implementedhostnamebut it was not part of any specification). You can change it to:And if you also want to convert links to
httpswebsites, ditch the attribute selector and change the regular expression:Update:
Specifically for fancybox, according to the documentation, it should work this way. However, you are right, you could just add the
iframeclass: