Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Open PDFs in new window

    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.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Do you want ALL pdfs to open in a new window or just the ones that are in the div with class="entrytext"?
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Can I ask why you can not just add target="_blank" to start out with?

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #4
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by A1ien51
    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)
    Code:
    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;

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Code:
    <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>
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #6
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks _Aerospace_Eng_, that seems to do pretty much what I wanted! Thank you for your help


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •