3
<script>
var tids = {
308: 1,
312: 1,
313: 1,
314: 1
};
</script>

results in "missing } in XML expression with an arrow pointing to the first colon in the JS error console. Isn't this a valid declaration?

2
  • 1
    Do you have any other JS code or is this it? This by itself on a page works just fine without any errors. Commented Jan 11, 2010 at 18:06
  • Have you tried declaring your script tag's type: <script type='text/javascript' language='javascript'>? Might help if your browser is confused for some reason... Commented Jan 11, 2010 at 18:10

4 Answers 4

8

First you should fix your <script> tag to

<script type="text/javascript">

Next, if you want to use numeric indexes, try to declare them as a string:

var tids = {
'308': 1,
'312': 1,
'313': 1,
'314': 1
};

Please note, however, that you will not be able to reference them in object notation (i.e. tids.308). You could simply use arrays instead of objects:

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

4 Comments

You could access it as tids['308'], though.
it was the type that was causing it.
Crockford says to not worry about the type attribute in your script tags. The OP's script tag is fine. javascript.crockford.com/script.html
Specifically, the script tag type defaults to text/javascript as part of HTML5. One might want to use the old tag for compatibility with older browsers. Or just decide they don't matter.
1

This isn't an associative array -- it's just a JS object. I believe you need to make the keys strings instead of numeric.

var tids = {
"308": 1,
"312": 1,
"313": 1,
"314": 1
};

More info on associative arrays vs. regular objects.

Comments

0

i guess that the key cannot start with a number. try;

<script>
var tids = {
n308: 1,
n312: 1,
n313: 1,
n314: 1
};
</script>

3 Comments

A key can start with a number.
I'm pretty sure the key can be a number, I think I've used it before.
I guess not. The key cannot start with a number at least it be declared like a string (in that case is not a number anymore)
0

I have tried in both IE and FF and the code is fine. It should be the error of other codes.

Please use Firefox Web Developer and Firebug to find the source of error.

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.