0

Hi there I would like PDF files in my website to open into a new tab instead of to the computer/downloads folder.

The files have query string ?wpdf_download_file=xyz.

So ultimately:

On click - if link contains query string and is pdf then open in new tab.

I have tried this:

jQuery("a[href$='.pdf']").click(function(){
   if (window.location.href.indexOf("?wpdf_download_file=") > -1) {
         jQuery(this).attr('target', '_blank');
   }
});  

UPDATE - THIS SEEMS TO RESOLVE MY ISSUE:

jQuery(document).ready(function() {
    jQuery("a[href$='.pdf']").click(function(){
    if (window.location.href.indexOf("?wpdf_download_file=") > -1) {
    jQuery(this).attr('target', '_blank');
    }
    }); 

    var url = window.location.href;
    var urlNoProtocol = url.replace(/^http?:/i, ""); 

    jQuery("a[href$='.pdf']").each(function(){
    if(window.location.href.indexOf("localhost") > -1){
    var newUri = jQuery(this).attr('href').replace(urlNoProtocol+"?    wpdf_download_file=/var/www/ryco/", "http://"+location.host+"/");
    jQuery(this).attr('href', newUri); 
    } else{
    var newUrl = jQuery(this).attr('href').replace(url+"?wpdf_download_file=/var/www/ryco/", "http://"+location.host+"/"); 
    jQuery(this).attr('href', newUrl); 
    }

    });
});
roshambo
  • 2,624
  • 6
  • 31
  • 54

0 Answers0