Question

I'm trying to have an if statement. The problem is that I'm trying to grab a url parameter that may or may not be there. When I plug it into my JavaScript it throws an error because sometimes the variable isn't there so the syntax is incorrect. Here's what I mean:

var manual = {module_url,manual};

if(manual)
  $('#manual').show();

Now if the url parameter is present the code ends up looking like this:

var manual = true;

if(manual)
  $('#manual').show();

If it is not, then the code ends up looking like this:

var manual = ;

if(manual)
  $('#manual').show();

That's where my problem lies. It ends up breaking all the JavaScript on the rest of the page. I tried using a try/catch but the problem still happens because of the syntax. Unfortunately, I don't have control of the server aspect so I got to work with what I have.

Was it helpful?

Solution

You can't catch or recover from a syntax error.

The solution in this case is to manipulate the code so that it's valid even if the value inserted by the template is empty.

var manual = ("{module_url,manual}" !== "");

When {module_url,manual} is empty, the above line becomes "" !== "" which evaluates to false.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top