Niek Nijland
Niek Nijland

Reputation: 772

If code gives error do.. javascript

When a piece of javascript code gives an error in the console. How can I do something like: If this piece of code gives back an error execute another piece of code?

This is what I have:

try {
    var operator = <?=$this->shopRequest[operator]?>;
} catch(errorObj) {
    var operator = sessionStorage.getItem('operator');
}

This is the error I get in the console:

Uncaught SyntaxError: Unexpected token ;

Upvotes: 1

Views: 89

Answers (3)

Oleg
Oleg

Reputation: 23337

Try:

var operator = "<?=$this->shopRequest[operator]?>";
alert(operator);

Edit

of better:

var operator = "<?=str_replace('"', '\"', $this->shopRequest[operator])?>";

just in case it will contain " symbols

Upvotes: 3

sinou
sinou

Reputation: 41

You can do something like this:

try
{
  // Run some code here
}
catch(err)
{
  // Handle errors here; You can make a call to the other piece of code from here
}

Upvotes: 0

mex23
mex23

Reputation: 111

If I understood correctly your answer you should use

try {
   // your code
} catch(e) {
   console.err('Ops ' + e); 
   // execute here other code
}

Upvotes: 0

Related Questions