Using JQuery to make all PDF links open in a new window

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! :)

Related Posts Plugin for WordPress, Blogger...

Incoming searches:

jquery open pdf, jquery open pdf in new window, jquery pdf viewer, jquery open file in new window, open pdf jquery, jquery open file, jquery view pdf, jquery show pdf, open pdf in jquery, open pdf in new window

Comments

No related posts.

  • http://www.facebook.com/profile.php?id=100001008436491 Aldren B Oliveros

    kakainis nmn nag hahanap aq ng motto ng english na maganda ok..

  • http://www.facebook.com/profile.php?id=100001008436491 Aldren B Oliveros

    kakainis nmn nag hahanap aq ng motto ng english na maganda ok..

  • http://www.facebook.com/profile.php?id=580719398 Asiri Darshana Gunasena

    hi

  • http://www.facebook.com/profile.php?id=580719398 Asiri Darshana Gunasena

    hi

  • http://www.facebook.com/profile.php?id=599715619 Kavita Jagtap

    waw!

  • http://www.facebook.com/profile.php?id=599715619 Kavita Jagtap

    waw!

  • http://www.facebook.com/profile.php?id=599715619 Kavita Jagtap

    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!

  • http://www.facebook.com/profile.php?id=599715619 Kavita Jagtap

    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!

  • http://www.facebook.com/profile.php?id=100001136670858 Bayern Caseres

    hi. can we see the codes?

  • http://www.facebook.com/profile.php?id=100001136670858 Bayern Caseres

    hi. can we see the codes?

  • http://www.facebook.com/profile.php?id=100001136670858 Bayern Caseres

    is this working when my pdf file is stored in database?

  • http://www.facebook.com/profile.php?id=100001136670858 Bayern Caseres

    is this working when my pdf file is stored in database?

  • http://www.flashjunior.ch FJ

    i do it with $=

    = is exactly equal
    != is not equal
    ^= is starts with
    $= is ends with
    *= is contains

    my selector to find a tags with pdf:
    $(“a[href$='.pdf']“)

  • http://www.KevinUdy.com/ Kevin

    Awesome, just what I needed, thanks!

  • http://www.facebook.com/profile.php?id=100002598528850 Muhammed Shafi K

    how can convert files from html to pdf using javascript….pls help me?