snipt

Ctrl+h for KB shortcuts

JavaScript

This will open external links and any PDF, Office Documents in a new window. Good for WordPress type sites.

1
2
3
$("a[href^='http:']").not("[href*='YOUR-DOMAIN']").attr('target','_blank');
	$("a[href^='https:']").not("[href*='YOUR-DOMAIN']").attr('target','_blank');
	$("a[href*='.pdf'], a[href*='.docx'], a[href*='.xlsx'], a[href*='.doc']").attr('target','_blank');
https://snipt.net/embed/5d247bdb4e398ea67057b299d15dd85a/
/raw/5d247bdb4e398ea67057b299d15dd85a/
5d247bdb4e398ea67057b299d15dd85a
js
JavaScript
3
2019-08-21T19:51:30
True
False
False
/api/public/snipt/29219/
this-will-open-external-links-and-any-pdf-office-documents-in-a-new-window-good-for-wordpress-type-sites
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;a[href^=&#39;http:&#39;]&quot;</span><span class="p">).</span><span class="nx">not</span><span class="p">(</span><span class="s2">&quot;[href*=&#39;YOUR-DOMAIN&#39;]&quot;</span><span class="p">).</span><span class="nx">attr</span><span class="p">(</span><span class="s1">&#39;target&#39;</span><span class="p">,</span><span class="s1">&#39;_blank&#39;</span><span class="p">);</span> </span><span id="L-2"><a name="L-2"></a> <span class="nx">$</span><span class="p">(</span><span class="s2">&quot;a[href^=&#39;https:&#39;]&quot;</span><span class="p">).</span><span class="nx">not</span><span class="p">(</span><span class="s2">&quot;[href*=&#39;YOUR-DOMAIN&#39;]&quot;</span><span class="p">).</span><span class="nx">attr</span><span class="p">(</span><span class="s1">&#39;target&#39;</span><span class="p">,</span><span class="s1">&#39;_blank&#39;</span><span class="p">);</span> </span><span id="L-3"><a name="L-3"></a> <span class="nx">$</span><span class="p">(</span><span class="s2">&quot;a[href*=&#39;.pdf&#39;], a[href*=&#39;.docx&#39;], a[href*=&#39;.xlsx&#39;], a[href*=&#39;.doc&#39;]&quot;</span><span class="p">).</span><span class="nx">attr</span><span class="p">(</span><span class="s1">&#39;target&#39;</span><span class="p">,</span><span class="s1">&#39;_blank&#39;</span><span class="p">);</span> </span></pre></div> </td></tr></table>
"external links", jquery, links