-4

why this JS:

<script type="text/javascript">
    $(function() {
        $('a[href$=".pdf"]').prop('onClick', '_gaq.push(["_trackEvent", "Pdf", "Download", "Download_pdf"]);');
    });
</script>

doesn't work? is there any quotes errors?

The script should find all the a tags with href ending in .pdf and adds to it the onClick attribute with the value that follows (Google Analytics Event), but in Chrome Developer Tool Console appears this error:

Uncaught SyntaxError: Unexpected token <(…)

is there any syntax error?

Evan Davis
  • 35,493
  • 6
  • 50
  • 57
strongff
  • 23
  • 4
  • Is this the full code? – Sebastian Simon Oct 21 '15 at 21:16
  • 1
    Don't add inline onclick events to elements. Add a `.click()` handler instead. Does the error happen at page load or when you click on the link? – JJJ Oct 21 '15 at 21:16
  • Try removing `"."` at `".pdf"` – guest271314 Oct 21 '15 at 21:17
  • 2
    Don't encourage this type of question. Inform the asker to [ask a better question](http://stackoverflow.com/help/on-topic) – Mulan Oct 21 '15 at 21:18
  • Yes this is the full script. I try removing "." at ".pdf" but there is the same error. I try it in the Chrome Developer Tool Console in a page where there is a a tag the links to a pdf file. – strongff Oct 21 '15 at 21:20
  • > '_gaq.push(["_trackEvent", "Pdf", "Download", "Download_pdf"]);' This should have been a function definitioin – gprasant Oct 21 '15 at 21:21
  • 2
    Do you have this _in a .js file_? You don't need the ` – Evan Davis Oct 21 '15 at 21:36

1 Answers1

1

This is the error you get when you paste a <script> tag into an external .js file, because it barfs trying to parse the first <.

Paste this directly into your HTML, or remove the <script> tag.

Evan Davis
  • 35,493
  • 6
  • 50
  • 57