We help IT Professionals succeed at work.

Open Document in New Window - Javascript for Sharepoint

1,615 Views
Last Modified: 2012-05-08
Hi,

I'm using this bit of code to open documents in a Library in a new IE window.  Only problem is our WSS3 is external so we can't guarantee everyone is going to be running IE.  Does anyone know the bit that you can add that says, if you are running mac forget about it?

try
{
  //Get all page links
  var links = document.getElementsByTagName("A");

  //Iterate all page links
  for(var i=0; i<links.length; i++)
  {
    var link = links(i);

    //Coerse to string by concantenating an emptystring
    var onValues = link.onclick + link.onfocus + "";

    //Change the target of ListItem links
    //ListItem links contain one of these literals:
    //  Document Library = DispDocItemEx
    //  Links List = OnLink
    //  All Other Lists List = GoToLink (overridden below)
    if(onValues.indexOf("DispDocItemEx") >= 0
    || onValues.indexOf("GoToLink") >= 0
    || onValues.indexOf("OnLink") >= 0
      )
    {
      link.getAttributeNode("target").value = "_blank";
    }
  }
}
catch(e){alert(e.Message);}

//Override SharePoint's GoToLink function
//Create a custom GoToLink function
var _origGoToLink = GoToLink;
GoToLink = function(elm)
{
  if (elm.href == null)
    return;
  var ch = elm.href.indexOf("?") >= 0 ? "&" : "?";
  var srcUrl = GetSource();
  if (srcUrl != null && srcUrl != "")
    srcUrl = ch + "Source=" + srcUrl;
  var targetUrl = elm.href + srcUrl;

  //Use window.open rather than window.location
  window.open(STSPageUrlValidation(targetUrl));
}

Thanks in advance.
Comment
Watch Question

Author

Commented:
Sorry I just have one comment on my own post!  I meant FireFox, not mac.

ie.  If you are running firefox, don't process this javascript.
Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you, thank you, thank you a million times over.

This works and I managed to insert it at the bottom of my script which sits as a hidden content editor webpart in my document library.  If you are using IE it will open documents in a new window, if you are using firefox it will open in the same window (if you don't insert the line: [if (!(/Firefox/.test(navigator.userAgent))) -  Firefox gives an Undefined error).


<script type="text/javascript">
try
{
  //Get all page links
  var links = document.getElementsByTagName("A");

  //Iterate all page links
  for(var i=0; i<links.length; i++)
  {
    var link = links(i);

    //Coerse to string by concantenating an emptystring
    var onValues = link.onclick + link.onfocus + "";

    //Change the target of ListItem links
    //ListItem links contain one of these literals:
    //  Document Library = DispDocItemEx
    //  Links List = OnLink
    //  All Other Lists List = GoToLink (overridden below)
    if(onValues.indexOf("DispDocItemEx") >= 0
    || onValues.indexOf("GoToLink") >= 0
    || onValues.indexOf("OnLink") >= 0
      )
    {
      link.getAttributeNode("target").value = "_blank";
    }
  }
}
catch(e){alert(e.Message);}

//Override SharePoint's GoToLink function
//Create a custom GoToLink function
var _origGoToLink = GoToLink;
GoToLink = function(elm)
{
  if (elm.href == null)
    return;
  var ch = elm.href.indexOf("?") >= 0 ? "&" : "?";
  var srcUrl = GetSource();
  if (srcUrl != null && srcUrl != "")
    srcUrl = ch + "Source=" + srcUrl;
  var targetUrl = elm.href + srcUrl;

  //Use window.open rather than window.location
  if (!(/Firefox/.test(navigator.userAgent)))
{
  window.open(STSPageUrlValidation(targetUrl));
}
}
</script>


Author

Commented:
Hi,

Seems I might have jumped the gun a bit on this solution.  It worked yesterday....

Seems I get an "Undefined" error again in Firefox.

In the code above, have I put this line:  if (!(/Firefox/.test(navigator.userAgent)))
in the appropriate place of the code?  Am I missing a curly bracket or something???

Author

Commented:
I have isolated the bit of code that Firefox doesn't like:
       catch(e){alert(e.Message);}

I have tried enclosing it as follows within the code:,
                     if (!(/Firefox/.test(navigator.userAgent)))
      {
             catch(e){alert(e.Message);}
      }

Now Firefox disregards the option to open document in new window and doesn't error on me...
BUT IE no longer opens docs in new window!!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.