Pop up PDFs in a new window with jQuery
August 5, 2010 · Chris Peters
My approach to popping up PDF URLs in a new window using jQuery.
I was looking for a good way to pop up all PDF links in a new window and ran across Dennison Uy’s post with code.
Pretty slick and very unobtrusive, and it keeps target="_blank"
out of your markup, which is arguably a behavioral and not structural thing anyway. Plus it’s one step closer to killing target="_blank"
.
My slight modification: being a tad more user friendly
Here’s my version of the code:
$(document).ready(function() { | |
$("a[href*='.pdf']").click(function(e) { | |
window.open(this.href, "", "toolbar=0"); | |
e.preventDefault(); | |
}); | |
}); |
And here it is in CoffeeScript:
jQuery -> | |
$('a[href*=".pdf"]').click (e) -> | |
window.open this.href, '', 'toolbar=0' | |
e.preventDefault() |
I basically took what he did and added toolbar=0
to the arguments for window.open()
.
Why? Jakob Nielsen’s guidelines for opening PDFs in a new window call for removing browser chrome like the Back button. Why? Browser navigation doesn’t fit the paradigm for PDF document navigation. And Nielsen has watched thousands of users fumble around with the web, while most of us have not.