0

I use a TWebBrowser component in a delphi application and communicate using JavaScript.

In JavaScript I have the following code:

function test() {
  var nr = external.GetNumber();
  alert(nr);
  nr = parseInt(nr); // this is where the javascript fails
  doSomethingWith(nr);
}

alert(nr) function shows up the number I was expecting (eg: 1517376) but when I call parseInt (or any other function) it fails with "external.GetNumber(); is not a string". The type of nr is "undefined" .

Is there a way to convert it to string or numeric? I tried nr.toString(), String(nr), nr = nr + '' and they all fail because "String was expected". This only happens on IE6.

Mircea Soaica
  • 2,829
  • 1
  • 14
  • 25
  • 1
    That can't be the real code that's generating the error you've described. One or the other is not quite as you've listed it. And of course, the other obvious question is: What is `external.GetNumber`? – T.J. Crowder Jan 16 '13 at 15:36
  • 1
    one thing you always should take care if you use `parseInt` is to define the `radix`. for decimal do `parseInt(nr,10)`. but i don't think that has something to do with your problem. – t.niese Jan 16 '13 at 15:38
  • [IE6?](http://www.ie6countdown.com/) Most businesses do not even support IE7 any more. – epascarello Jan 16 '13 at 15:38
  • That is the real code that is generating the error. external.GetNumber is a function in Delphi (the parent application) that is 'callable' from Javascript. – Mircea Soaica Jan 16 '13 at 15:50
  • 1
    show us your Delphi `external.GetNumber`. It is probably returning the wrong OleVariant type to `nr`. – kobik Jan 16 '13 at 15:50
  • We must support IE6 because it is the default browser component in Windows XP. In Windows 7 the component is IE7. – Mircea Soaica Jan 16 '13 at 15:51
  • can it create for any weird reason `window.nr` ? – Arioch 'The Jan 16 '13 at 15:51
  • `var nr = "asdf"; nr = external.GetNumber();` ? – Arioch 'The Jan 16 '13 at 15:57

2 Answers2

3

You have 2 problems:

  • external.GetNumber(); may not return a string type

you can solve this in js by adding an empty string, this implicitly converts nr to a string type :

var nr = ""+ external.GetNumber();

  • js parseInt function uses a radix parameter, Full explanation here :

    nr = parseInt(nr, 10); // correct usage

what is puzzling me is why don't you return a number in the first place? We only can guess since you didn't share the code behind external.GetNumber();

Community
  • 1
  • 1
whosrdaddy
  • 11,720
  • 4
  • 50
  • 99
0

I don't think you're alerting the real error. Try:

var nr;
try{ nr = external.GetNumber(); } catch(e) { alert(nr); }
ic3b3rg
  • 14,629
  • 4
  • 30
  • 53