最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript: open all links to pdf on a page in new window - Stack Overflow

programmeradmin1浏览0评论

I have a site that builds the pages dynamically. It's a SharePoint site. The site contains a lot of document libraries. The libraries contain word, excel and PDF documents. When a user clicks on a document, the document opens in the client application for office documents only. PDFs simply open in the same window. when people close the window containing the document, they close the site. I'm trying to use javascript to onload add target="_blank" to the PDF links.

So far I have:

window.onload = function(){
    l=document.links.length;
    for(i = 0; i<l; i++) {
        n = document.links[i].href.indexOf(".pdf");

        if (n > 0){
            document.links[i].setAttribute('target', '_blank');
        }
    }
}

This code sort of works as some of the pdf links load in a new window as expected, some load in the parent window and some links load in both a new window and the parent. How do I tell the browser not to load in the parent window and only in the new window?

This is what I want to achieve:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<a href="a.pdf">a.pdf</a><br /><br />
<a href="b.html">b.html</a><br /><br />
<a href="c.pdf">c.pdf</a><br /><br />

<script>
window.onload = function(){
    l=document.links.length;
    for(i = 0; i<l; i++) {
        n = document.links[i].href.indexOf(".pdf");

        if (n > 0){
            document.links[i].setAttribute('target', '_blank');
        }
    }
}
</script>
</body>
</html>

The problem I'm running into is that sharepoint document libraries are modifying the link behavior such that the javascript does not make then open in a new window. Below is an example of a link from a document library:

<a onfocus="OnLink(this)" href=".pdf" onmousedown="return VerifyHref(this,event,'0','','')" onclick="return DispEx(this,event,'TRUE','FALSE','FALSE','','0','','','','','331','0','0','0x7fffffffffffffff','','')">HR Policy Manual</a>

I have a site that builds the pages dynamically. It's a SharePoint site. The site contains a lot of document libraries. The libraries contain word, excel and PDF documents. When a user clicks on a document, the document opens in the client application for office documents only. PDFs simply open in the same window. when people close the window containing the document, they close the site. I'm trying to use javascript to onload add target="_blank" to the PDF links.

So far I have:

window.onload = function(){
    l=document.links.length;
    for(i = 0; i<l; i++) {
        n = document.links[i].href.indexOf(".pdf");

        if (n > 0){
            document.links[i].setAttribute('target', '_blank');
        }
    }
}

This code sort of works as some of the pdf links load in a new window as expected, some load in the parent window and some links load in both a new window and the parent. How do I tell the browser not to load in the parent window and only in the new window?

This is what I want to achieve:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<a href="a.pdf">a.pdf</a><br /><br />
<a href="b.html">b.html</a><br /><br />
<a href="c.pdf">c.pdf</a><br /><br />

<script>
window.onload = function(){
    l=document.links.length;
    for(i = 0; i<l; i++) {
        n = document.links[i].href.indexOf(".pdf");

        if (n > 0){
            document.links[i].setAttribute('target', '_blank');
        }
    }
}
</script>
</body>
</html>

The problem I'm running into is that sharepoint document libraries are modifying the link behavior such that the javascript does not make then open in a new window. Below is an example of a link from a document library:

<a onfocus="OnLink(this)" href="https://rcd.sharepoint./HR/HR%20Policy%20Manual.pdf" onmousedown="return VerifyHref(this,event,'0','','')" onclick="return DispEx(this,event,'TRUE','FALSE','FALSE','','0','','','','','331','0','0','0x7fffffffffffffff','','')">HR Policy Manual</a>
Share Improve this question edited Nov 18, 2019 at 17:24 RCDAWebmaster asked Nov 18, 2019 at 15:26 RCDAWebmasterRCDAWebmaster 3255 silver badges18 bronze badges 4
  • It should not open in both windows, you need to show an example of this happening. stackoverflow./help/minimal-reproducible-example There's probably something else going on. – Ruan Mendes Commented Nov 18, 2019 at 15:28
  • unclear how it would load in the parent window when you set target to blank – epascarello Commented Nov 18, 2019 at 15:28
  • Now I can't find the PDF opening in both the parent and child. Most of the links open in the parent. I've found only one link that opens in a child window. The bigger problem is that when pressing the back button after loading the PDF in the parent, I get the message that the page has expired and it needs to be loaded again (IE only not chrome, Firefox). – RCDAWebmaster Commented Nov 18, 2019 at 16:09
  • it appears to have something to do with the document libraries that SharePoint provides as the site navigation links work as expected. links in the library appear as: <a onfocus="OnLink(this)" href="rcda145.sharepoint./HR/HR%20Policy%20Manual.pdf" onmousedown="return VerifyHref(this,event,'0','','')" onclick="return DispEx(this,event,'TRUE','FALSE','FALSE','','0','','','','','331','0','0','0x7fffffffffffffff','','')">HR Policy Manual</a> now i need to see what is going on with the functions. – RCDAWebmaster Commented Nov 18, 2019 at 16:16
Add a ment  | 

3 Answers 3

Reset to default 1

If you don't have access to all elements for ahead with capturing all clicks on the page. Use addEventListener with enabled capturing for handling event. Test whether it's anchor tag and proceed to new page by own with code below:

document.addEventListener("click", function(e){
    if (e.target.localName == 'a') {
        var url = e.target.getAttribute('href');
        e.stopPropagation();
        // You can place extra checks here.
        var tab = window.open(url, '_blank');
        tab.focus();
    }
}, true)

Do

Here is what I would do, I think I would collect the anchors and loop over them to check if the hrefs ends with .pdf and then add a function on all the .pdf links

Don't

Don't check for pdf files with .indexOf('.pdf'). Your check should fail If there's a filename called somedummyfile.pdf.something.png (which is a .png image) or any other formatted file.


Note that, new window might be blocked at user's end if they are using add-blockers.

Here is the Snippet:

function modifyLinks() {
  let links = document.getElementsByTagName('a');
  let properties = 'height=' + window.innerHeight + ',width=' + window.innerWidth + ',' + 'scrollbars=yes,status=yes';
  for (i = 0; i < links.length; i++) {
    if (links[i].href.endsWith('.pdf')) {
      // links[i].setAttribute('target', '_blank'); // if you want to open them in new tab;
      console.log(links[i].href);
      links[i].addEventListener('click', function(e) {
        e.preventDefault();
        window.open(this.href, '_blank', properties);
      })
    }
  }
}

window.addEventListener('load', modifyLinks);
<a href="somefile_1.pdf">File 1</a>
<a href="somefile_2.pdf">File 2</a>
<a href="somefile_3.pdf">File 3</a>
<a href="test.html">HTML Link</a>
<a href="some_file.ai">Some Other file</a>

I added the following JavaScript to get links containing "pdf" to open in a new window:

window.onload = function(){
    l=document.links.length;
    for(i = 0; i<l; i++) {
        n = document.links[i].href.indexOf(".pdf");

        if (n > 0){
            document.links[i].setAttribute('target', '_blank');
        }
    }
}

I then noticed that document libraries were using a DispEx() javascript function on click of document links which negated the first bit of code. I had to overload the function with my own functionality.

function DispEx(a){
    if (a.href.endsWith('.pdf')){
        window.open(a);
        return false;
    }
}

Using both pieces of JavaScript in a content editor web part, I got PDF documents to open in a new window and all other links/documents to load in the same window.

发布评论

评论列表(0)

  1. 暂无评论