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.getElementsByTagN ame("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("DispD ocItemEx") >= 0
|| onValues.indexOf("GoToLink ") >= 0
|| onValues.indexOf("OnLink") >= 0
)
{
link.getAttributeNode("tar get").valu e = "_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(STSPageUrlVali dation(tar getUrl));
}
Thanks in advance.
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.getElementsByTagN
//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("DispD
|| onValues.indexOf("GoToLink
|| onValues.indexOf("OnLink")
)
{
link.getAttributeNode("tar
}
}
}
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(STSPageUrlVali
}
Thanks in advance.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(navigato r.userAgen t))) - Firefox gives an Undefined error).
<script type="text/javascript">
try
{
//Get all page links
var links = document.getElementsByTagN ame("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("DispD ocItemEx") >= 0
|| onValues.indexOf("GoToLink ") >= 0
|| onValues.indexOf("OnLink") >= 0
)
{
link.getAttributeNode("tar get").valu e = "_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(navigato r.userAgen t)))
{
window.open(STSPageUrlVali dation(tar getUrl));
}
}
</script>
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(navigato
<script type="text/javascript">
try
{
//Get all page links
var links = document.getElementsByTagN
//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("DispD
|| onValues.indexOf("GoToLink
|| onValues.indexOf("OnLink")
)
{
link.getAttributeNode("tar
}
}
}
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(navigato
{
window.open(STSPageUrlVali
}
}
</script>
ASKER
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(navigato r.userAgen t)))
in the appropriate place of the code? Am I missing a curly bracket or something???
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(navigato
in the appropriate place of the code? Am I missing a curly bracket or something???
ASKER
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(navigato r.userAgen t)))
{
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!!
catch(e){alert(e.Message);
I have tried enclosing it as follows within the code:,
if (!(/Firefox/.test(navigato
{
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!!
ASKER
ie. If you are running firefox, don't process this javascript.