问题
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,
- The
=>
and preferfunction () {}
instead - The
function (param = x) {}
, preferfunction (param) {if (param == 'undefined') {param = x;}}
- The
let
and usevar
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 ...
Avoid using the
VAR => RESULT VAR
, preferfunction (VAR) {return RESULT VAR;}
Never use parameters in your codes with values like:
function (VAR = DEFAULTVALUE) {}
Usefunction (VAR) {if (VAR == "undefined") VAR = DEFAULTVALUE;}
To get an object:
document.getelementByID
To specify an uninitialized property, choose
Object.setAttribute (property, value)
rather thanObject.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.
- 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
;
- 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