2

I am having a bit of a problem with the following:

function loadContent(id) {
     $("#featuredDiv").load("featuredDiv.php?list="+id);
     );
}

It all works fine when opening the URL with an ID of 21012011 like:

javascript:loadContent(28012011)

but when 04022011 is the id (javascript:loadContent(28012011)), it seems to change the number to 1057801.

I need the number to be passed through exactly as it is reading the page from a database. Does anyone have an idea what I am doing wrong?

Many thanks in advance!

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
Oakesy
  • 91
  • 1
  • 3

2 Answers2

5

That is because 04022011 is parsed by Javascript as an octal number.

Wrap the ID in quotes at all times and it will work.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • Sorry for being a bit thick. I tried: $("#featuredDiv").load("featuredDiv.php?list="+'id'); but it didn't work – Oakesy Feb 14 '11 at 21:28
  • @Oak the value of `id` must be in quotes when you pass it: `loadContent('28012011');` But don't quote `id` itself – Pekka Feb 14 '11 at 21:30
  • Thanks Pekka, you're a ledgend! I can sleep tonight now :) – Oakesy Feb 14 '11 at 21:32
3

The problem is the difference between decimal and octal notation.

For historical reasons, which have nothing to do with utility to modern programmers, a sequence of numbers starting with a 0 and containing no 8 or 9 characters is interpreted as an octal number. This means that it is treated as being in base-8, rather than base-10.

The octal 04022011 is 1057801 in decimal notation.

You can fix this by wrapping your number in quote marks (e.g. javascript:loadContent("28012011")): this will cause it to be interpreted as a string and passed directly into your URL.

However You appear to be using <a href="javascript:..."> links. This is bad practice. Look into an introduction to jQuery (e.g. jqfundamentals.com) to see optimal ways to bind actions to elements and events.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318