Link to home
Start Free TrialLog in
Avatar of Balibooblue
Balibooblue

asked on

Open Document in New Window - Javascript for Sharepoint

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.
Avatar of Balibooblue
Balibooblue

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of kadaba
kadaba
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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>


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???
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!!