I’m trying to create a javascript that opens .jpg and .pdf files in a new window, and I keep getting a very annoying error when clicking the file links. Translated from norwegian, the popup error says:
“Invalid URL-address to page(site?):“.
When I click “ok” on that error message, the new pdf/jpg-window will open as expected.
How do I get rid of the completly redundant error message?
<script language="javascript" type="text/javascript">
_spBodyOnLoadFunctionNames.push("OpenPDFInNewWindow()");
function OpenPDFInNewWindow()
{
var aAllLinks = document.links;
for(var i=0;i<aAllLinks.length;i++)
{
var oA = aAllLinks[i];
var sHREF = oA.href.toLowerCase();
if(sHREF.indexOf(".jpg") > 0 || sHREF.indexOf(".pdf") > 0)
oA.href="javascript:window.open('"+oA.href+"','test','width=300').focus();";
}
}
</script>
The script checks the page for a-tags containing relevant file extensions, and then adds the window.open command in their href attribute.
I have tried IE9 and Chrome, both browsers throw same expection. It is important that this works in IE. I have tried many different combinations of arguments in the window.open call. The oA.href call returns an url like this:
http://<server>/arbeidsrom/ET24130/dokumenter/image.jpg
I’m a rookie on javascripts, so please keep that in mind when replying 🙂
(code snippet nicked from Randy Williams @ http://www.synergyonline.com/Blog/Lists/Posts/Post.aspx?ID=18)
try this