JavaScript Safari - SyntaxError: Unexpected token '>'

可紊 提交于 2019-12-24 06:48:25

问题


I have the problem that this JavaScript snippet runs in Firefox / Chrome without any problem, and Safari I get error: "SyntaxError: Unexpected token '>'".

Here's the code:

window.onclick = (test) => {
  const googleWindow = window.open();
  fakeAjax(response => {
    googleWindow.location.replace(`https://google.com?q=${response}`);
  });
};

function fakeAjax(callback) {
  setTimeout(() => {
    callback('example');
  }, 1);
}

I've googled and have already seen here in the forum, the problem there appears often, unfortunately I have not found a suitable solution.

Thank you in advance Best regards


回答1:


Arrow function ()=>{} is es6 feature, firefox and chrome both are already supported. But safari old version doesn't. Please check http://kangax.github.io/compat-table/es6/ for more information.




回答2:


Safari 5.1 is really bad !!!

Example it does not load with =>

var t=w.map(v => String.fromCharCode(v));

Replacement it will take charge

var t=w.map(function(v){return String.fromCharCode(v)});

To make a script work on all browsers, avoid,

  1. The => and prefer function () {} instead
  2. The function (param = x) {}, prefer function (param) {if (param == 'undefined') {param = x;}}
  3. The let and use var instead.



回答3:


The problem is with the arrow function syntax from es6 as Jerry Zhu already mentioned.

To support older browsers you need to replace "response => {" with "function(response) {" and also "setTimeout(() => {" with "setTimeout(function() {".




回答4:


Advice for you to be quiet with all browsers ...

  1. Avoid using the VAR => RESULT VAR, prefer function (VAR) {return RESULT VAR;}

  2. Never use parameters in your codes with values ​​like: function (VAR = DEFAULTVALUE) {} Use function (VAR) {if (VAR == "undefined") VAR = DEFAULTVALUE;}

  3. To get an object: document.getelementByID

  4. To specify an uninitialized property, choose Object.setAttribute (property, value) rather than Object.property = value. This can lead to errors or inappropriate behavior.

Some Styles properties can only be used if you set them up, especially the CSS3. Javascript accepts the same thing as CSS provided that you respect its usage.

  1. to get DOM coordinates browsers from Netscape X and IE or other browser Opéra, FireFox, Apple X, etc...

this.Ypos = document.body.scrollTop + ((! this.IE)? e.pageY: (window.event.y)); this.Xpos = document.body.scrollLeft + ((! this.IE)? e.pageX: (window.event.x)); If coordinates smartphones Event page.X is : Event.changedTouches[e.changedTouches.length-1].pageX;

  1. To print an object in document: Object.appendChild (mebox)

I'm 53 years old, I've been working on the Internet for 35 years and since then nothing has really changed.



来源:https://stackoverflow.com/questions/39008975/javascript-safari-syntaxerror-unexpected-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!