Help with JavaScript

Discussion in 'Webmastering & Programming' started by IndyColtsFan, May 14, 2019.

  1. IndyColtsFan

    IndyColtsFan Limp Gawd

    Messages:
    280
    Joined:
    Jul 4, 2017
    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?
     
  2. ChristianVirtual

    ChristianVirtual [H]ard DCOTM Mar 2016,Aug 2017

    Messages:
    2,492
    Joined:
    Feb 23, 2013
    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";
    }
     
  3. modi123

    modi123 [H]ardness Supreme

    Messages:
    5,144
    Joined:
    Sep 6, 2006
    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.
     
  4. IndyColtsFan

    IndyColtsFan Limp Gawd

    Messages:
    280
    Joined:
    Jul 4, 2017
    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.

    Good idea, but still not working. Very strange.
     
  5. IndyColtsFan

    IndyColtsFan Limp Gawd

    Messages:
    280
    Joined:
    Jul 4, 2017
    I found the issue and fixed it - the $ at the end of the regex was causing the issue. I rewrote the line to:

    var regex = /\.(doc|xls|ppt|pdf)/gi;

    and now it works!
     
    ChristianVirtual and modi123 like this.
  6. IndyColtsFan

    IndyColtsFan Limp Gawd

    Messages:
    280
    Joined:
    Jul 4, 2017
    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!
     
    ChristianVirtual likes this.