Sometimes it can be really annoying for the user to open a PDF file in the same window, and sometimes you might find yourself working on an existing website, faced with the insurmountable task of making sure all PDF links open in a new window.
Just like today.
Forget about manual editing, it will only take too long.
Forget about search and replace, you might mess up the code.
JQuery to the rescue!
See my code below:
$("a[href*=.pdf]").click(function(){ window.open(this.href); return false; });
Alternatively, you can apply the target=”_self” attribute instead:
$("a[href*=.pdf]").click(function(){ $(this).attr({"target":"_self"}); return false; });
Viola! With Just four lines of code, you now open all pdf links in a new window! Thanks to JQuery’s awesome element matching engine and attribute pattern recognition, you can actually modify the code to apply the same behavior to any link, as long as the text can be found anywhere within the href attribute.
Have fun!
Comments
9 responses to “Using JQuery to make all PDF links open in a new window”
Awesome, just what I needed, thanks!
People say jquery is boring…
how can convert files from html to pdf using javascript….pls help me?
can u paste the whole code to list out the directory content and when we click on any file then it will display in browser!
is this working when my pdf file is stored in database?
hi. can we see the codes?
waw!
hi
kakainis nmn nag hahanap aq ng motto ng english na maganda ok..