{"id":113,"date":"2018-07-25T06:03:34","date_gmt":"2018-07-25T06:03:34","guid":{"rendered":"http:\/\/www.projectimmerse.com\/?p=113"},"modified":"2020-06-17T01:48:23","modified_gmt":"2020-06-17T01:48:23","slug":"using-jquery-to-automatically-download-a-pdf-file","status":"publish","type":"post","link":"https:\/\/www.projectimmerse.com\/blog\/using-jquery-to-automatically-download-a-pdf-file\/","title":{"rendered":"Using jQuery to automatically download a PDF file"},"content":{"rendered":"<p>I&#8217;ve been working through one of those client requests that isn&#8217;t use too often. They wanted a PDF be automatically downloaded after submitting their email (the only field on the form). <\/p>\n<p>I started thinking about different scenarios, particularly this one:<\/p>\n<blockquote><p>\nIf a visitor successfully completes the form and hits submit, should the PDF open on a new window tab or should it be automatically be downloaded?\n<\/p><\/blockquote>\n<p><!--more--><\/p>\n<p>I came up with an awesome solution, two methods that work slightly the same.<\/p>\n<h3>Method #1<\/h3>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\n&lt;script&gt;\r\njQuery(document).ready(function() {\r\n\r\n\r\n      if (jQuery(&quot;#confirm-download&quot;).length) {\r\n\r\n        var pdfDocument = jQuery(&quot;#download-pdf&quot;).attr(&quot;href&quot;);\r\n\r\n        var newTab = window.open(pdfDocument, '_blank');\r\n        newTab.location;\r\n        \r\n      } \r\n      \r\n\r\n});\r\n&lt;\/script&gt;\r\n\r\n<\/pre>\n<p>The problem with the #1 method is that trigger a browser pop up blocker. I spent hours on this subject and there&#8217;s no way around it with javascript\/jQuery. <\/p>\n<p>The only workaround would be to reload the PDF document on the same URL, see method #2.<\/p>\n<h3>Method #2<\/h3>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\n&lt;script&gt;\r\njQuery(document).ready(function() {\r\n\r\n\r\n      if (jQuery(&quot;#confirm-download&quot;).length) {\r\n\r\n        var pdfDocument = jQuery(&quot;#download-pdf&quot;).attr(&quot;href&quot;);\r\n\r\n        jQuery.get(pdfDocument, function() {\r\n          window.location.href = jQuery(&quot;#download-pdf&quot;).attr('href');\r\n        });\r\n        \r\n      } \r\n      \r\n\r\n});\r\n&lt;\/script&gt;\r\n\r\n<\/pre>\n<p>My particular project didn&#8217;t need a submit() or click() handler but if you wanted to integrate that you just need to wrap the function within an event handler of your choice. Yeah, my particular situation a bit unique. <\/p>\n<p>For most situations you&#8217;re probably going to run into a more common scenario where the event handler is attached to the button itself. If you&#8217;re interested in this approach, I&#8217;ve got you covered. <\/p>\n<p>Check it out&#8230;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\n&lt;script&gt;\r\njQuery(document).ready(function() {\r\n \r\n      jQuery(&quot;#the-button-that-is-clicked&quot;).click(function() {\r\n\r\n        if (jQuery(&quot;#check-input-field&quot;).val() != '') {\r\n \r\n          var pdfDocument = &quot;\/wp-content\/uploads\/the-pdf-file.pdf&quot;;\r\n   \r\n          var newTab = window.open(pdfDocument, '_blank');\r\n          newTab.location;\r\n\r\n        } else {\r\n\r\n          return false; \/\/ optional\r\n        \r\n        }\r\n      });\r\n});\r\n&lt;\/script&gt;\r\n\r\n<\/pre>\n<p>The last final solution you might have thought about would be html5&#8217;s download attribute, look I think it&#8217;s awesome &#8211; at the time of this posting it is only supported by the latest browsers though.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.projectimmerse.com\/wp-content\/uploads\/2018\/07\/download-attribute-support.png\" alt=\"Download Attribute Browser Support\" width=\"1480\" height=\"490\" class=\"aligncenter size-full wp-image-117\" srcset=\"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/07\/download-attribute-support.png 1480w, https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/07\/download-attribute-support-300x99.png 300w, https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/07\/download-attribute-support-768x254.png 768w, https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/07\/download-attribute-support-1024x339.png 1024w, https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/07\/download-attribute-support-80x26.png 80w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<p>The download attribute in action below:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\n\/\/ (.img, .pdf, .txt, .html, etc.)\r\n&lt;a href=&quot;\/pdf\/pdf-file.pdf&quot; download&gt;Download&lt;\/a&gt;\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working through one of those client requests that isn&#8217;t use too often. They wanted a PDF be automatically downloaded after submitting their email (the only field on the form). I started thinking about different scenarios, particularly this one: If a visitor successfully completes the form and hits submit, should the PDF open on &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.projectimmerse.com\/blog\/using-jquery-to-automatically-download-a-pdf-file\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using jQuery to automatically download a PDF file&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1127,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-113","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Using jQuery to automatically download a PDF file - Project Immerse<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.projectimmerse.com\/blog\/using-jquery-to-automatically-download-a-pdf-file\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using jQuery to automatically download a PDF file - Project Immerse\" \/>\n<meta property=\"og:description\" content=\"I&#8217;ve been working through one of those client requests that isn&#8217;t use too often. They wanted a PDF be automatically downloaded after submitting their email (the only field on the form). I started thinking about different scenarios, particularly this one: If a visitor successfully completes the form and hits submit, should the PDF open on &hellip; Continue reading &quot;Using jQuery to automatically download a PDF file&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.projectimmerse.com\/blog\/using-jquery-to-automatically-download-a-pdf-file\/\" \/>\n<meta property=\"og:site_name\" content=\"Project Immerse\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-25T06:03:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-06-17T01:48:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/07\/jquery-window-location-href.png\" \/>\n\t<meta property=\"og:image:width\" content=\"750\" \/>\n\t<meta property=\"og:image:height\" content=\"750\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"projectimmerse\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"projectimmerse\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/using-jquery-to-automatically-download-a-pdf-file\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/using-jquery-to-automatically-download-a-pdf-file\\\/\"},\"author\":{\"name\":\"projectimmerse\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#\\\/schema\\\/person\\\/c53f2864be524ee6fa08a7e4800dd1e5\"},\"headline\":\"Using jQuery to automatically download a PDF file\",\"datePublished\":\"2018-07-25T06:03:34+00:00\",\"dateModified\":\"2020-06-17T01:48:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/using-jquery-to-automatically-download-a-pdf-file\\\/\"},\"wordCount\":400,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/using-jquery-to-automatically-download-a-pdf-file\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/07\\\/jquery-window-location-href.png\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/using-jquery-to-automatically-download-a-pdf-file\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/using-jquery-to-automatically-download-a-pdf-file\\\/\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/using-jquery-to-automatically-download-a-pdf-file\\\/\",\"name\":\"Using jQuery to automatically download a PDF file - Project Immerse\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/using-jquery-to-automatically-download-a-pdf-file\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/using-jquery-to-automatically-download-a-pdf-file\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/07\\\/jquery-window-location-href.png\",\"datePublished\":\"2018-07-25T06:03:34+00:00\",\"dateModified\":\"2020-06-17T01:48:23+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#\\\/schema\\\/person\\\/c53f2864be524ee6fa08a7e4800dd1e5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/using-jquery-to-automatically-download-a-pdf-file\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/using-jquery-to-automatically-download-a-pdf-file\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/using-jquery-to-automatically-download-a-pdf-file\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/07\\\/jquery-window-location-href.png\",\"contentUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/07\\\/jquery-window-location-href.png\",\"width\":750,\"height\":750,\"caption\":\"Window Location Href in Javascript\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/using-jquery-to-automatically-download-a-pdf-file\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using jQuery to automatically download a PDF file\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/\",\"name\":\"Project Immerse\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#\\\/schema\\\/person\\\/c53f2864be524ee6fa08a7e4800dd1e5\",\"name\":\"projectimmerse\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g\",\"caption\":\"projectimmerse\"},\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/author\\\/projectimmerse\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using jQuery to automatically download a PDF file - Project Immerse","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.projectimmerse.com\/blog\/using-jquery-to-automatically-download-a-pdf-file\/","og_locale":"en_US","og_type":"article","og_title":"Using jQuery to automatically download a PDF file - Project Immerse","og_description":"I&#8217;ve been working through one of those client requests that isn&#8217;t use too often. They wanted a PDF be automatically downloaded after submitting their email (the only field on the form). I started thinking about different scenarios, particularly this one: If a visitor successfully completes the form and hits submit, should the PDF open on &hellip; Continue reading \"Using jQuery to automatically download a PDF file\"","og_url":"https:\/\/www.projectimmerse.com\/blog\/using-jquery-to-automatically-download-a-pdf-file\/","og_site_name":"Project Immerse","article_published_time":"2018-07-25T06:03:34+00:00","article_modified_time":"2020-06-17T01:48:23+00:00","og_image":[{"width":750,"height":750,"url":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/07\/jquery-window-location-href.png","type":"image\/png"}],"author":"projectimmerse","twitter_card":"summary_large_image","twitter_misc":{"Written by":"projectimmerse","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.projectimmerse.com\/blog\/using-jquery-to-automatically-download-a-pdf-file\/#article","isPartOf":{"@id":"https:\/\/www.projectimmerse.com\/blog\/using-jquery-to-automatically-download-a-pdf-file\/"},"author":{"name":"projectimmerse","@id":"https:\/\/www.projectimmerse.com\/blog\/#\/schema\/person\/c53f2864be524ee6fa08a7e4800dd1e5"},"headline":"Using jQuery to automatically download a PDF file","datePublished":"2018-07-25T06:03:34+00:00","dateModified":"2020-06-17T01:48:23+00:00","mainEntityOfPage":{"@id":"https:\/\/www.projectimmerse.com\/blog\/using-jquery-to-automatically-download-a-pdf-file\/"},"wordCount":400,"commentCount":0,"image":{"@id":"https:\/\/www.projectimmerse.com\/blog\/using-jquery-to-automatically-download-a-pdf-file\/#primaryimage"},"thumbnailUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/07\/jquery-window-location-href.png","inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.projectimmerse.com\/blog\/using-jquery-to-automatically-download-a-pdf-file\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.projectimmerse.com\/blog\/using-jquery-to-automatically-download-a-pdf-file\/","url":"https:\/\/www.projectimmerse.com\/blog\/using-jquery-to-automatically-download-a-pdf-file\/","name":"Using jQuery to automatically download a PDF file - Project Immerse","isPartOf":{"@id":"https:\/\/www.projectimmerse.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.projectimmerse.com\/blog\/using-jquery-to-automatically-download-a-pdf-file\/#primaryimage"},"image":{"@id":"https:\/\/www.projectimmerse.com\/blog\/using-jquery-to-automatically-download-a-pdf-file\/#primaryimage"},"thumbnailUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/07\/jquery-window-location-href.png","datePublished":"2018-07-25T06:03:34+00:00","dateModified":"2020-06-17T01:48:23+00:00","author":{"@id":"https:\/\/www.projectimmerse.com\/blog\/#\/schema\/person\/c53f2864be524ee6fa08a7e4800dd1e5"},"breadcrumb":{"@id":"https:\/\/www.projectimmerse.com\/blog\/using-jquery-to-automatically-download-a-pdf-file\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.projectimmerse.com\/blog\/using-jquery-to-automatically-download-a-pdf-file\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.projectimmerse.com\/blog\/using-jquery-to-automatically-download-a-pdf-file\/#primaryimage","url":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/07\/jquery-window-location-href.png","contentUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/07\/jquery-window-location-href.png","width":750,"height":750,"caption":"Window Location Href in Javascript"},{"@type":"BreadcrumbList","@id":"https:\/\/www.projectimmerse.com\/blog\/using-jquery-to-automatically-download-a-pdf-file\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.projectimmerse.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Using jQuery to automatically download a PDF file"}]},{"@type":"WebSite","@id":"https:\/\/www.projectimmerse.com\/blog\/#website","url":"https:\/\/www.projectimmerse.com\/blog\/","name":"Project Immerse","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.projectimmerse.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.projectimmerse.com\/blog\/#\/schema\/person\/c53f2864be524ee6fa08a7e4800dd1e5","name":"projectimmerse","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g","caption":"projectimmerse"},"url":"https:\/\/www.projectimmerse.com\/blog\/author\/projectimmerse\/"}]}},"_links":{"self":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/comments?post=113"}],"version-history":[{"count":8,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts\/113\/revisions"}],"predecessor-version":[{"id":462,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts\/113\/revisions\/462"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/media\/1127"}],"wp:attachment":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/media?parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/categories?post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/tags?post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}