-4

I have a piece of code that I have written which shows a word when I use a specific tag:

<script type='text/javascript'>
    var {type} = '{type}';

    if ({type} == 'football'){
        document.write('football');
    }
</script>

The above will output the word 'football'.

I can manipulate it to include various words:

<script type='text/javascript'>
    var {type} = '{type}'; 

    if ({type} == 'football' || {type} == 'womensfootball'){
        document.write('football');
    }
</script>

Use of either tag will render the output word 'football'.

However, how would I amend it to not duplicate the output for when I use multiple tags? For example, using the above piece of code. I want to post something and use both the 'football' and 'womensfootball' tags without getting the output twice - which is currently what happens. Do I need to add another function or maybe use an alternative to ||?

Additionally, is there a way I can use a hyphen in the or a space?

e.g.

{type} == 'womens-football' || {type} == 'womens football'

Thanks.

Tom
  • 7,640
  • 1
  • 23
  • 47
Vincent Lee
  • 177
  • 1
  • 9
  • 1
    Does this answer your question? http://stackoverflow.com/questions/1661197/what-characters-are-valid-for-javascript-variable-names – trevorade Jun 04 '15 at 13:45
  • @VincentLee can you post a fiddle or more code? Your code should only print football once... – brso05 Jun 04 '15 at 13:46
  • 11
    I feel like the title of the question and the contents of the question are very different – Tom Jun 04 '15 at 13:46
  • 2
    Unless your target js engine supports destructuring this `var {type} = '{type}'; ` is not valid javascript. And even if it does. Then you will "always" get `type` to be undefined. – Yury Tarabanko Jun 04 '15 at 13:51
  • I'm open to amending it, I'm still learning so not yet fully accustomed with all the terminologies - had to fall back of qualitative. – Vincent Lee Jun 04 '15 at 13:53
  • It's an accepted variable of the website in which I'm writing the code Yury. It works for me. – Vincent Lee Jun 04 '15 at 13:56
  • I think you're getting Javascript confused with another language; is it possible to declare a variable by wrapping it in curly brackets? – Shotgun Ninja Jun 04 '15 at 13:59
  • 1
    I feel like it should be `var type = "{type}";` instead of `var {type} = "{type}";`... you can't use curly braces in a *variable name*. You can use them all you want in the value, but declaring a var with anything other than $, _, or alphanumeric characters is pretty much off-limits, as discussed [here](http://stackoverflow.com/questions/1661197/what-characters-are-valid-for-javascript-variable-names). – Shotgun Ninja Jun 04 '15 at 14:01
  • 1
    I tried that but it only works when wrapped around curly brackets – Vincent Lee Jun 04 '15 at 14:03
  • You'll have to give me a link to where this is valid; I'd love to see this. – Shotgun Ninja Jun 04 '15 at 14:04
  • I don't need to change `{type}`, I need to change the value which is in the `''`. I don't think hyphens or spaces are allowed though, because when I try it I get no output. – Vincent Lee Jun 04 '15 at 14:04
  • @VincentLee So basically it's not a javascript question but name_your_website_script question :) – Yury Tarabanko Jun 04 '15 at 14:10
  • Basically ^. For instance, they use `{Title}` to display the title of the webpage - which isn't standardized code, but works on their site. `{type}` is just some shorthand they use. – Vincent Lee Jun 04 '15 at 14:14

1 Answers1

2

It seems you are using some kind of pseudo code - {type} looks like a template language like Smarty

In JavaScript this will work - note the quote nesting

var type = "women's football"; 
if (type == 'football' || type == "women's football") {
  document.write('football');
}

This will also work

var types = {
  football : ["football","women's football"]
}

alert(types["football"][1])
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Thanks for the suggestion. I could get this working outside of the site but not within it, so had to revert `type` back to `{Type}`. The results were that there was now an output, but it was outputting 'football' for every type of article, not just 'football' and 'women's football'. – Vincent Lee Jun 04 '15 at 14:38
  • As I said, the site is likely running some sort of templating like Smarty. – mplungjan Jun 04 '15 at 14:47