-2

Is it possible to pass a string into an if statement as a means to create dynamic if statements? A simplified example would be:

var condition = "bool == true";
if(condition) console.log('It is true!);

Of course the example above does not work, because it always returns true, because the string has content. Is there some way to inject to contents of condition into the if statement as actual parameters though? The condition needs to be in string format because in a real world application, the condition will be pulled out of an existing HTML block using a regular expression and is therefore returned in string format.

Vanilla javascript only please.

ryandlf
  • 27,155
  • 37
  • 106
  • 162
  • 3
    you *could* use `eval` but thats a bad method. – Daniel A. White Nov 16 '12 at 14:20
  • this looks like a design error for me... you should not need to do such stuff. However if you absolutely need to do it this way, you can do nothing else than `evaling` the string. – Christoph Nov 16 '12 at 14:22
  • 1
    Why would you do this? Why is your condition in a string like that? what in the name of Pete is going on here?! – rlemon Nov 16 '12 at 14:22
  • 2
    When you've been backed into the `eval` corner, your only good option is to step back and rethink your design. – jbabey Nov 16 '12 at 14:27
  • I don't think this question warrants downvotes, however to offer some explanation I am building a templating system similar to handlebars.js and attempting to use conditions from within an html block. I guess if eval is the only way to go, then true...I should rethink the way I am going about this in the first place. – ryandlf Nov 16 '12 at 14:31
  • _"The condition will be pulled out of an existing HTML block using a regular expression"_ Nooooo, [why does this idea of HTML and Regex come from?](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – Elias Van Ootegem Nov 16 '12 at 14:34
  • Its a means to create a templating system per my commend above. Its been done before...handlebars.js uses this method. – ryandlf Nov 16 '12 at 14:37
  • Handlebars isn't parsing html though, it's parsing `{{Handlebars Templates}}`. – RichardTowers Nov 16 '12 at 16:00

2 Answers2

1
if(eval(condition)){

    //do something

}

Daniel is right, this is bad practise. But naming variables via strings is also bad practise - so one follows the other...

shennan
  • 10,798
  • 5
  • 44
  • 79
  • 2
    @DanielA.White well, if the initial design is broken, there is no other way than evaling. – Christoph Nov 16 '12 at 14:24
  • *bad design and bandaid solutions* are frowned upon. – rlemon Nov 16 '12 at 14:25
  • @rlemon but SO reputation isn't, and this is the only way to do what he has (foolishly?) asked to do... – shennan Nov 16 '12 at 14:26
  • what does "but SO reputation" have to do with this being a horrible design (OPs code, this answer is relevant). – rlemon Nov 16 '12 at 14:27
  • @rlemon have a cup of tea and relax - programming is supposed to be fun. he'll have got the message that this is a bad way of doing things for now - but at least he has been made aware of `eval` in the process. – shennan Nov 16 '12 at 14:29
  • @rlemon coz js/html errorz are silent and 'ting - let me be my own G – shennan Nov 16 '12 at 14:34
  • @rlemon: lolz at kitteh-speekz... and shennan: to some people, programming is a full time job. A likeable job, but it's work nonetheless. A job that enables you to live a comfortable life, and 25% of the time you're doing stuff you like to do. 75% of the time you're cleaning up other ppl's mess, attending meetings or secretly plotting the best way to exterminate all man kind... just like anybody else, really – Elias Van Ootegem Nov 16 '12 at 14:40
  • 1
    @EliasVanOotegem I'm more than aware of the programming neurosis, thanks. But when someone posts a question like this, and 6 'serious' programmers jump down each-others throats to distinguish which is the 'right' answer - it does take a little bit of the sunshine out of it. – shennan Nov 16 '12 at 14:55
0

eval would work, but it's dodgy.

How confident are you that these strings are always going to be valid javascript?

What happens if someone tricks your app into putting

location.replace('http://stackoverflow.com');

(or worse) into that HTML block?

If you find yourself needing to use eval it generally means you should rethink your approach.

RichardTowers
  • 4,682
  • 1
  • 26
  • 43
  • Although I share your opinion this should be a comment and not an answer. – Christoph Nov 16 '12 at 14:30
  • I think this is a relevant answer. Albeit it does not directly solve the OPs issue it does shed light on a serious security risk. – rlemon Nov 16 '12 at 14:31
  • @Cristoph I suppose you're right. When I started writing I was going to suggest an alternate approach to eval, but I decided I didn't know enough about OP's use case. – RichardTowers Nov 16 '12 at 14:46