1

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 ;

10
  • Look into try...catch Commented Jan 13, 2014 at 15:18
  • 2
    What is the output of <?=$this->shopRequest[operator]?>. I would imagine it doesn't include the required quotes Commented Jan 13, 2014 at 15:22
  • 1
    You can't fix syntax errors without simply fixing the syntax errors. Commented Jan 13, 2014 at 15:22
  • 1
    Don't use try/catch. This this not the kind of error, that should be catched, it's an error that should be fixed. $this->shopRequest[operator] apparantly isn't what you expect it to be, so have a look at it. If it is supposed to be a string, maybe it would be enough to add quotes: var operator = "<?=$this->shopRequest[operator]?>"; Commented Jan 13, 2014 at 15:23
  • 1
    @NiekNijland you have to detect the situation in PHP and prevent the syntactically-incorrect code from being generated. Commented Jan 13, 2014 at 15:27

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

5 Comments

Given that the OP said in the comments (which looking at your answer I'm sure you've read) the output is a number, this is invalid logic. You shouldn't output this as a string if it is a number
Yes that's true, but now I use the number as a string which is oke, since I cannot get into the php code since that is back-end stuff and another guy in the company handles that.
I you want rep, add your answer and I will accept is as corret, since you were first and probably this is a rip-off of yours
I know I can access it in the cms I'm working, but I cannot do more then request values.
@RGraham I hadn't read your comment by that time I started to write my answer. Sometimes I use this approach when I need to pass value from PHP to JavaScript directly, and i don't want to wait till the page is loaded. So if you write your own answer I will remove my own, if you wish
0

If I understood correctly your answer you should use

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

Comments

0

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.