Help with JavaScript

IndyColtsFan

Limp Gawd
Joined
Jul 4, 2017
Messages
361
So, I have a question regarding a problem I'm trying to solve in SharePoint. I need any link containing a doc, docx, xls, xlsx, ppt, pptx, and pdf file to open in a new tab. I've got the code below inserted into my masterpage, but it doesn't seem to work. Note that if I take out the IF statement, ALL links on the site open in a new tab. I ONLY need links containing the extensions above to open in a new tab.


< script language="JavaScript">
_spBodyOnLoadFunctionNames.push("rewriteLinks");


function rewriteLinks() {
//create an array to store all
var anchors = document.getElementsByTagName("a");
var regex = /\.(doc|xls|ppt|pdf)$/g;

//loop through the array
for (var x=0; x<anchors.length; x++) {
//add the [target] attribute and rewrite the [href] attribute
if (regex.test(anchors[x].href)) {
anchors[x].target = "_blank";
}
}
}
< /script>

I'm hoping I just have a simple error in the code above. Anyone?
 

ChristianVirtual

[H]ard DCOTM x3
Joined
Feb 23, 2013
Messages
2,522
Never user regex is JavaScript but reading https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test the ‘lastIndex’ is as global flag in RegEx instance potentially the reason;

It is worth noting that the lastIndex will not reset when testing a different string.
So maybe you can try to create a new regex instance for each comparison. Or set lastIndex back to zero imside the loop. Tha later approach might be better.

for (var x=0; x<anchors.length; x++) {
//add the [target] attribute and rewrite the [href] attribute

regex.lastIndex = 0;

if (regex.test(anchors[x].href)) {
anchors[x].target = "_blank";
}
 

modi123

Supreme [H]ardness
Joined
Sep 6, 2006
Messages
5,667
Getting any weird console errors when you are in f12 dev tools?

Seems reasonably sound when I test it with a dummy page and txt and jpg. That is after I ripped it out of being a function and .push.
 

IndyColtsFan

Limp Gawd
Joined
Jul 4, 2017
Messages
361
Getting any weird console errors when you are in f12 dev tools?

Seems reasonably sound when I test it with a dummy page and txt and jpg. That is after I ripped it out of being a function and .push.
No, I don't see anything in the Console errors pointing to this code. I thought maybe my regular expression was off. I've launched URLs with no file extension and URLs which definitely have an embedded file extension, and all open in the current tab.

Never user regex is JavaScript but reading https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test the ‘lastIndex’ is as global flag in RegEx instance potentially the reason;

It is worth noting that the lastIndex will not reset when testing a different string.
So maybe you can try to create a new regex instance for each comparison. Or set lastIndex back to zero imside the loop. Tha later approach might be better.

for (var x=0; x<anchors.length; x++) {
//add the [target] attribute and rewrite the [href] attribute

regex.lastIndex = 0;

if (regex.test(anchors[x].href)) {
anchors[x].target = "_blank";
}
Good idea, but still not working. Very strange.
 

IndyColtsFan

Limp Gawd
Joined
Jul 4, 2017
Messages
361
Never user regex is JavaScript but reading https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test the ‘lastIndex’ is as global flag in RegEx instance potentially the reason;

It is worth noting that the lastIndex will not reset when testing a different string.
So maybe you can try to create a new regex instance for each comparison. Or set lastIndex back to zero imside the loop. Tha later approach might be better.

for (var x=0; x<anchors.length; x++) {
//add the [target] attribute and rewrite the [href] attribute

regex.lastIndex = 0;

if (regex.test(anchors[x].href)) {
anchors[x].target = "_blank";
}
Just an update - while I got the regex working, I noticed that on sites with lots of URLs, random URLs wouldn't open in a new tab. I incorporated your suggestion and that fixed my issue. Thanks!
 
Top