Klikerko
Klikerko

Reputation: 1097

This script applies specific attr to all links on the website. How to exclude pdf and zip files?

I'm building Wordpress website where all pages are being loaded using Ajax. I'm using script created by Chris Coyer that can be found here. Code below will add specific tags to all links on the website. Wordpress will try to load content from those links using Ajax. Problem is that those attributes are being applied on links to PDF and ZIP files which are meant to be downloaded, not loaded into page.

How I can exclude specific file formats from getting those attributes? I already tried something like this $internalLinks = $("a[href^='"+siteURL+"']:not([href$=/*.pdf])") but that didn't work for me. Am I doing something wrong?

Below is part of the original code that adds attributes. Full code can be found on this JSFiddle page. Thanks!

var $mainContent = $("#main-content"),
    siteURL = "http://" + top.location.host.toString(),
    $el = $("a");

function hashizeLinks() {
    $("a[href^='" + siteURL + "']").each(function() {
        $el = $(this);

        // Hack for IE, which seemed to apply the hash tag to the link weird
        if ($.browser.msie) {
            $el.attr("href", "#/" + this.pathname).attr("rel", "internal");
        } else {
            $el.attr("href", "#" + this.pathname).attr("rel", "internal");
        }
    });
};​

Upvotes: 0

Views: 95

Answers (2)

gilly3
gilly3

Reputation: 91637

You can't use * as a wildcard like a filespec. Just use this:

$internalLinks = $("a[href^='"+siteURL+"'):not([href$='.pdf'])");

Upvotes: 1

Nathan
Nathan

Reputation: 4067

You can apply a filter on all matched links:

var $mainContent = $("#main-content"),
siteURL = "http://" + top.location.host.toString(),
$el = $("a");

function hashizeLinks() {
$("a[href^='" + siteURL + "']").filter(function(){
       return this.href.match(/[^\.pdf]$/i);
    }).each(function() {
    $el = $(this);

    // Hack for IE, which seemed to apply the hash tag to the link weird
    if ($.browser.msie) {
        $el.attr("href", "#/" + this.pathname).attr("rel", "internal");
    } else {
        $el.attr("href", "#" + this.pathname).attr("rel", "internal");
    }
});
};​

Upvotes: 0

Related Questions