最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript parse href and add onclick if linking to a PDF - Stack Overflow

programmeradmin2浏览0评论

I would like to parse all of the HTML within a document and if there is a link to a PDF, add an onClick event.

Example:

<a href="/files/report.pdf">Report</a>

Bees:

<a href="/files/report.pdf" onclick="javascript: _gaq.push(['_trackPageview', '/files/report.pdf']);">Report</a>

The following code works in Chrome/Firefox but not on IE9:

function AddGaqPush()
    {
        var links = document.getElementsByTagName("a");

        for (var i=0; i < links.length; i++)
        {
            if (links[i].href.indexOf(".pdf") !== -1)
            {
                links[i].setAttribute("onclick", "javascript: _gaq.push(['_trackPageview', '" + links[i].href + "']);");
            }
        }
    }

Edited to add: IE settings: Browser Mode: IE9; Document Mode: IE9 Standards

I would like to parse all of the HTML within a document and if there is a link to a PDF, add an onClick event.

Example:

<a href="/files/report.pdf">Report</a>

Bees:

<a href="/files/report.pdf" onclick="javascript: _gaq.push(['_trackPageview', '/files/report.pdf']);">Report</a>

The following code works in Chrome/Firefox but not on IE9:

function AddGaqPush()
    {
        var links = document.getElementsByTagName("a");

        for (var i=0; i < links.length; i++)
        {
            if (links[i].href.indexOf(".pdf") !== -1)
            {
                links[i].setAttribute("onclick", "javascript: _gaq.push(['_trackPageview', '" + links[i].href + "']);");
            }
        }
    }

Edited to add: IE settings: Browser Mode: IE9; Document Mode: IE9 Standards

Share Improve this question asked Jan 25, 2013 at 0:16 Charles WesleyCharles Wesley 82913 silver badges27 bronze badges 4
  • Updated Q with detail: Browser Mode: IE9; Document Mode: IE9 Standards – Charles Wesley Commented Jan 25, 2013 at 0:20
  • 1 Don't add onclick. Add an event listener. It might help. – John Dvorak Commented Jan 25, 2013 at 0:21
  • so something like links[i].addEventListener("click", "javascript: _gaq.push(['_trackPageview', '" + links[i].href + "']);", false); ? – Charles Wesley Commented Jan 25, 2013 at 0:22
  • 2 links[i].addEventListener("click", function(){...}). Don't use strings for code, ever. – John Dvorak Commented Jan 25, 2013 at 0:24
Add a ment  | 

3 Answers 3

Reset to default 4

Use jQuery and you won't be limited by the browser (especially IE)

$('a[href~=.pdf]').click(function(e) {
    // your click action
    // e is a jQuery event
    // your <a> element is the variable this
});

Instead of adding an attribute, attach an event handler

if (links[i].attachEvent){
    links[i].attachEvent('onclick', tpv);
}
else{
    links[i].addEventListener('click', tpv);
}
function tpv(){
    _gaq.push(['_trackPageview', '" + this.href + "']);
}

The jQuery statement to acplish this would look like:

$('a[href*=".pdf"]').click(function(e) {
    _gaq.push(['_trackPageview', $(this).attr('href')]);
});

I liked Nannuo Lei's answer to this question but the solution wasn't quite accurate/plete and I don't yet have sufficient reputation to ment on that post.

You will want quotes around the string you are searching for and the *= selector allows you to identify a substring of a larger string, not just a word separated by whitespace as the ~= selector will acplish. More detail on jQuery selectors can be found here: https://api.jquery./category/selectors/

发布评论

评论列表(0)

  1. 暂无评论