0

I am getting Safari ReferenceError: Can't find variable: Set error from safari browser. I have checked other browsers but I didn't get any this error all codes working fine for me.

Is anyone can help me here what is wrong here and what is the solution to work my code from all browsers?

Full Demo

Problem Line

 var charactersX = new Set([
      0,
      32, // space
      13 // enter
      // add other punctuation symbols or keys
   ]);
  // Convert characters to charCode
   function toCharCodeX(char) {
      return char.charCodeAt(0);
   }

   var forbiddenCharactersX = new Set([
      toCharCodeX("_"),
      toCharCodeX("-"),
      toCharCodeX("?"),
      toCharCodeX("*"),
      toCharCodeX("\\"),
      toCharCodeX("/"),
      toCharCodeX("("),
      toCharCodeX(")"),
      toCharCodeX("="),
      toCharCodeX("&"),
      toCharCodeX("%"),
      toCharCodeX("+"),
      toCharCodeX("^"),
      toCharCodeX("#"),
      toCharCodeX("'"),
      toCharCodeX("<"),
      toCharCodeX("|"),
      toCharCodeX(">"),
      toCharCodeX("."),
      toCharCodeX(","),
      toCharCodeX(";")
   ]);
AlwaysStudent
  • 1,354
  • 18
  • 49
  • What version of Safari are you using? It's been more or less [supported since version 7.1](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#Browser_compatibility) – Rob M. Jul 25 '17 at 20:00
  • @RobM. Hmm, i see it. So do you have any idea for any solution to work all browsers like desktop and mobile devices? – AlwaysStudent Jul 25 '17 at 20:03
  • @RobM. If you have any idea you can answer from this bounty [question](https://stackoverflow.com/questions/43351621/check-pressed-space-then-add-diez-tag-using-jquery-with-multi-language) – AlwaysStudent Jul 25 '17 at 20:04
  • Use an array instead... – baao Jul 25 '17 at 20:21
  • you can try this too https://stackoverflow.com/questions/7958292/mimicking-sets-in-javascript – Sourav Jul 25 '17 at 20:22
  • Not needed @Sourav . For what he does, an array is perfectly fine. No need for anything else – baao Jul 25 '17 at 20:24

1 Answers1

1

Use arrays instead

var charactersX = [
  0,
  32, // space
  13 // enter
  // add other punctuation symbols or keys
];

and replace the .has(), e.g. here:

if (charactersX.has(code))

with

if (charactersX.indexOf(code) > -1) 

and

if (forbiddenCharactersX.has(code))

with indexOf > -1 too...

baao
  • 71,625
  • 17
  • 143
  • 203
  • your code is working fine. Thanks for it. But i have a question from you if you have a time please answer me. I have used CharcodeAt(0); in demo on line 76, to work my code on mobile devices. But it is not working on mobile. What is the problem here? You can check it my question [DEMO](https://codepen.io/shadowman86/pen/RZwrzQ) – AlwaysStudent Jul 25 '17 at 20:51
  • You have charCodeAt(0) in your pen, this shouldn't be a problem on mobile. If you have further errors, feel free to ask a new question and include the details @DevStud – baao Jul 25 '17 at 20:56
  • I have included a bounty question from here about my comment question [HERE](https://stackoverflow.com/questions/43351621/check-pressed-space-then-add-diez-tag-using-jquery-with-multi-language) – AlwaysStudent Jul 25 '17 at 21:25