Javascript Snippet not working in Safari 11 & 12

1.9k views Asked by At

does anyone know the reason why the following javascript snippet works in both Chrome & Firefox but not in the Safari 11 & 12 versions?

The only thing it does is take the value in the url parameter code and insert it in the url's on the page that need I want it to be in.

Are there any restrictions concerning javascript in the new Safari versions? I can't find any info online..

<script>
window.addEventListener("DOMContentLoaded", function() {

    if (window.location.href.indexOf('?code') > -1) {
        var uniqueCode = window.location.search.split(/\?|&/g).filter(function(str){
                                                               return str.toLowerCase().indexOf('code') > -1
                                               })[0].replace('code=','');

        var codeLinks = document.querySelectorAll('[href*="/validate/promocode/"'); 

        for (var i = 0; i < codeLinks.length; i++) {
            var currentHref = codeLinks[i].href;
            var newHref = currentHref.replace(/\/validate\/promocode\/.*\/buy\//, "/validate/promocode/" + uniqueCode + "/buy/");
            codeLinks[i].href = newHref;
        }   
    }
}, false);

</script>       

I have no Mac to test this , but is it possible that Javascript is default disabled on version 11 and 12 on Mac?

1

There are 1 answers

4
J88 On BEST ANSWER

Solved

The problem lies in the following line :

var codeLinks = document.querySelectorAll('[href*="/validate/promocode/"');

should be

var codeLinks = document.querySelectorAll('[href*="/validate/promocode/"]');

A small syntax error the other 4 browsers don't complain about. Conclusion : Safari is much stricter on Syntaxerrors.