Solved

Open documents in new window in Sharepoint 2007

Posted on 2007-11-16
9
6,805 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
  • 5
  • 3
9 Comments
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 8

Author Comment

by:nsyyoung
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
@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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now