Make any link with .pdf open in new window with jQuery?

前端 未结 4 699
野性不改
野性不改 2020-12-10 17:36

How can I have all links with a .pdf file extension open in a new window using jQuery? I need to change this:



        
4条回答
  •  情深已故
    2020-12-10 18:19

    One way, assuming you want links not ending in pdf to open in the same page:

    $('a').click(
        function(e){
            e.preventDefault();
            if (this.href.split('.').pop() === 'pdf') {
                window.open(this.href);
            }
            else {
                window.location = this.href;
            }
        });
    

提交回复
热议问题