...

View Full Version : Open PDFs in new window



djgh
04-28-2006, 01:53 AM
Hi,

I'd like to be able to place a script in the head of my page to go through all links in the page (or even better, all links in a div with a class of "entrytext"). If the link is to a PDF (i.e <a href="foo.pdf">Foo</a>), I'd like to have it so that when that link is clicked, it will open in a new window. If it helps, I can add a rel="pdf" or class="pdf" to any PDF links.

Unfortunately, I have close to no JS knowledge above window.open, so I'm asking here.

Many thanks for any help offered.

_Aerospace_Eng_
04-28-2006, 02:00 AM
Do you want ALL pdfs to open in a new window or just the ones that are in the div with class="entrytext"?

A1ien51
04-28-2006, 04:40 AM
Can I ask why you can not just add target="_blank" to start out with?

Eric

djgh
04-29-2006, 02:50 AM
Can I ask why you can not just add target="_blank" to start out with?

Eric

Because I'm using a Strict DOCTYPE. Plus this would be a better solution, as any time I wanted to stop opening PDFs in separate windows, I'd just have to remove the script.

_Aerospace_Eng_ - If you can make it only apply to class="entrytext", that would be best.

If it helps anyone, I found the following online - how would it be possible to tweak it so that instead of using the rel, it used the URL (as the URL would end in .pdf)


function externalLinks() {
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++) {
var anchor = anchors[i];
if (anchor.getAttribute("href") &&
anchor.getAttribute("rel") == "pdf")
anchor.target = "_blank";
}
}
window.onload = externalLinks;

_Aerospace_Eng_
04-29-2006, 08:25 AM
<script type="text/javascript">
function externalLinks()
{
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++)
{
var anchor1 = anchors[i];
if (anchor1.getAttribute("href").match('.pdf') == ".pdf" && anchor1.parentNode.className == 'entrytext')
{
anchor1.target = "_blank";
}
}
}
window.onload = externalLinks;
</script>

djgh
04-30-2006, 11:15 PM
Thanks _Aerospace_Eng_, that seems to do pretty much what I wanted! Thank you for your help :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum