Solved

Open documents in new window in Sharepoint 2007

Posted on 2007-11-16
9
6,897 Views
Last Modified: 2010-04-22
I have CQWPs in MOSS 2007 that list PDF document links with target="" attributes by default (is there a site setting to change that?) and views of document libraries that default their links with target="_ new".  I want all of those, and only those (they're PDFs) to open in a new window so the users don't keep knocking themselves off the site when they close the PDF.  Essentially, I need my script to change the target=""  to target="_blank" (or _new) without messing with the PDFs that already have "_new" in the target attribute.  I also have to leave .aspx pages opening in the same window (_self).   I also need documents with mixed-case extensions (Pdf, pdF, etc) (user naming errors abound) to open in separate windows.  I've attached a script that was working until I started working on target="" piece.

I appreciate any and all help.  I'm down to about a week of this project left.  Thanks.


<script>
//_spBodyOnLoadFunctionNames.push("rewriteLinks");
function rewriteLinks()
{
 
//create an array of all anchor elements in the page
var anchors = document.getElementsByTagName("a");
 
//loop through that array
for (var x=0; x<anchors.length; x++)
	{
	
	
		//identify the old anchor
		var oldText = anchors[x].outerHTML;
		
		//check original whole link
			//alert("original link: " + oldText);
		//verify that there isn't a target="_new" or target="" and it's not an aspx page(<0)
			alert("index of new target: " + oldText.indexOf(' target=_new'));
			alert(oldText);
			alert("index of empty target: " + oldText.indexOf('target=null'));
			alert("index of aspx: " + oldText.indexOf(".aspx"));
			//check index of aspx and pdf links
			alert("index of pdf: " + oldText.toLowerCase().indexOf(".pdf"));
		
		//if it is a pdf AND not an aspx page AND there's not already a new target
		if (oldText.toLowerCase().indexOf(".pdf")>0 && (oldText.toLowerCase().indexOf(".aspx")== -1) && oldText.indexOf(" target=_new")== -1 && oldText..indexOf(' target="" '))
		{
		//split up the anchor into pre- and post-.
			//create an array of parts for each anchor, one before and one after the period
			var temp = new Array();
			temp = oldText.split('.');
			//look at parts individually
				//alert("before the .: " + temp[0]);
				//alert("after the .: " + temp[1]);
			//split up the second part at the double quote
			var parts = new Array();
			parts = temp[1].split('"');
			//look at the parts individually
				//alert("parts[0]: " + parts[0]);
				//alert("parts[1]: " + parts[1]);
			
			//if part 0 (lowercased) is pdf then add target="_blank"
			if (parts[0].toLowerCase="pdf")
			{
				//check parts[0]
					//alert("parts[0] " + parts[0]);
				parts[0] = 'pdf" target="_blank"'
				//check it again for changes
					//alert("parts[0] " + parts[0]);
			
			//rewrite anchor 
			newText = temp[0] + "." + parts[0] + parts[1]
			//check new link
				//alert(newText);
			//write the HTML back to the browser
			anchors[x].outerHTML = newText;
			}
		}
}
 
}
 
</script>

Open in new window

0
Comment
Question by:nsyyoung
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 20302725
Here you go:
<script>
 
function rewriteLinks(){
  var lnk = document.links;
  for(var i=0;i<lnk.length;i++){
    if(lnk[i].href.match(/\.pdf$/i)){
      if(lnk[i].target==""){
        lnk[i].target="_blank";
      }
    }
  } 
}
 
</script>

Open in new window

0
 
LVL 8

Author Comment

by:nsyyoung
ID: 20308696
It's fine in IE7, but not when put into SP.  That /i was the first thing I tried, too, but it doesn't work in Sharepoint.  ASPX pages are opened in a new window, too.  I want only the pdf docs to open in New.

Is there maybe a better way to call this kind of a script in Sharepoint than:
//_spBodyOnLoadFunctionNames.push("rewriteLinks");
??

0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 20308848
That take control over SP with this:
<script>
 
function rewriteLinks(){
  var lnk = document.links;
  for(var i=0;i<lnk.length;i++){
    if(lnk[i].href.match(/\.pdf$/i)){
     lnk[i].target="_blank";
    } else {
     lnk[i].target="_self";
    }
  } 
}
 
</script>

Open in new window

0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 8

Author Comment

by:nsyyoung
ID: 20309236
I put alerts in this script to test it with all of the types of links I use in Sharepoint, and it works beautifully in IE without Sharepoint, but when I load it into SP, it goes haywire, throwing javascript errors and opening in the same window, or opening EVERYTHING in a new window.

I have a document library called Reusable Scripts, to which I load this script as "OpenNew.htm".  In the SP page, I have a content editor web part that calls "/ReusableScript/OpenNew.htm" on each page that needs it.  This is how I was instructed to do it by a SharePoint MVP.  Is there a better way to call these scripts?  I'm trying to avoid having to put a content editor web part into every page that needs it; if I have to make a change to the script, I'd have to do it in approximately 87 pages.

I'm definitely learning more about javascript.  Thanks for your help with that, at the very least.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 20309261
We come to the limit what we can provide to you. Beside that you have SharePoint dependencies you do have also custom script needs. That all together cannot run, at least not by remote debugging. Are you aware that we have never seen your html pages in browser and you ask dependencies deep into internals.
Can we see your pages live or can you post one example page html source as seen in browser?

0
 
LVL 8

Author Comment

by:nsyyoung
ID: 20309346
I can copy source as the browser presents it, but it will be lengthy.  It's a restricted intranet page, so I can't get you in to see it.  I think I'm on to something, though.  Here is the code that appears to work.  Notice the comments: everything but the script is commented out, but if I remove it altogether, it falls apart.  I'm becoming a believer in "if it works, don't fix it,"   and I'm inclined to just leave the whole html script page as it is.  Also, no javascript errors are appearing (in the last hour, anyway).  I'm going to point a few more pages to it and see what happens.

Thanks for your help.  On all fronts.
NSY

<!--<html><head>-->
<script>
_spBodyOnLoadFunctionNames.push("rewriteLinks"); 
function rewriteLinks(){
//alert("function called");  (to test)
  var lnk = document.links;
  for(var i=0;i<lnk.length;i++){
  	//alert(lnk[i].href); (yes, every link was "alerted", about 105 of them)
    if(lnk[i].href.match(/\.pdf$/i)){  //appears to work with all cases of PDF, in case user saves carelessly
     lnk[i].target="_blank";
    } else {
     lnk[i].target="_self";
    }
  } 
}
</script>
<!-- </head><body onLoad="rewriteLinks()">
 <a href="sitemap.PDF" target="" title="">Aka Energy Group, LLC Operating Agreement</a><br />
 <a href="sitemap.Pdf" target="" title="">Aka Energy Group, LLC Operating Agreement</a><br />
 <a href="sitemap.pdf" target="" title="">Aka Energy Group, LLC Operating Agreement</a><br />
 <a href="test.aspx" target="" title="">Aka Energy Group, LLC Operating Agreement</a><br />
 </body></html> -->

Open in new window

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 20309416
One more note, the new target is not written to page as html to be seen in browser but as a change to the DOM structure.
To observe that dynamic page DOM structure I can point you the Expert devic's PageSpy: http:/M_1344026.html

0
 

Expert Comment

by:dekempeneer
ID: 20938120
where can I add this code ??

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
 <script>
 
function rewriteLinks(){
  var lnk = document.links;
  for(var i=0;i<lnk.length;i++){
    if(lnk[i].href.match(/\.pdf$/i)){
     lnk[i].target="_blank";
    } else {
     lnk[i].target="_self";
    }
  }
}
 
</script>
  ??
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 20939987
@dekempeneer: please open your own question. You can reference to this question if you want.
But please do not ask me questions on email.
I do NOT answer EE emails privately, exception is when you have gifts for me ;-)



0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question