A. Lau A. Lau - 2 months ago 15
Javascript Question

Safari browser-specific error (optional argument)

So I have the following error which only occur in Safari (Apple product, go figure) while not occurring in Chrome or Firefox (code runs perfectly).

Error:

SyntaxError: Unexpected token '='. Expected a ')' or a ',' after a parameter declaration.


This is erroring for:
function ajax_general(url, params, title="")


When I remove the optional argument, it runs. Otherwise this occurs. How should I change this to make it also run on Safari?

Answer

You are using default function parameters which are not supported by safari currently. Your function:

function ajax_general(url, params, title="")

Default function parameters allow formal parameters to be initialized with default values if no value or undefined is passed.

title="" 

...is essentially saying, "If no title argument is passed, set title to empty string."

If the third parameter must be set to a value, do it like so:

function ajax_general(url, params, "")

Or depending on your use case, do it inside/outside the function:

var title = "";
function ajax_general(url, params, title);

Chrome and Firefox both have support for default function parameters, but safari unfortunately does not.

For more information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters

Comments