Jump to content

calling external javascript


Recommended Posts

This maths parser http://mathjs.org/ looks like a library I could really use.

 

However, I am having difficulties incorporating into SMS

I have the js file in the lib subfolder

But I am stuck how to 

1. pass through a string to parse

2. retrieve the result

   console.log(math.sqrt(-4))

I can't find any posts or information on how to even start.

 

 

math.js

Link to post
Share on other sites

Well that was unexpected

 

That works  a treat. The FMathParser.eval('1.2 / (2.3 + 0.7)') works nicely

 

the otherfunctions like 

.simplify('2 * 3 * x', {x: 4}).toString()) // '24'

Don't seem to be part of math.js and math.js file on the website doesn't want to work. But this will be very useful. 

Link to post
Share on other sites
  • Moderators

If you don't want to distribute the js library yourself, there is a cdn version as well

there are different ways of doing it but I usually do something like this 

  var Script := document.createElement('script');
  Script.src := https://cdnjs.cloudflare.com/ajax/libs/mathjs/5.1.0/math.min.js';
  document.head.appendChild(Script);
  Script.onload := procedure
  begin
    console.log('loaded');
    ... do your thing
  end;

so that processing occurs only after loading. (Works with local libraries instead of cdn as well of course, alternative to the $R directive)

 

Link to post
Share on other sites

Sorry, I tried getting your code to work but I haven't mage to I added W3C.HTML5,  but .src and .onload were still unknown

 

I would like to get it going locally

function mathjs: Variant; external 'mathjs';

  var FMathParser: THandle;

 

But it appears mathsjs is no longer the function and it is unclear to me how to get functioning.

 

Link to post
Share on other sites
  • Moderators

this code works

var document external 'document': variant;
var console  external 'console':  variant;

function mathjs: Variant; external 'mathjs';

implementation

{ TForm1 }

procedure TForm1.W3Button1Click(Sender: TObject);
begin
  var Script : THandle := document.createElement('script');
  Script.src := 'lib/math.js';   //'https://cdnjs.cloudflare.com/ajax/libs/mathjs/5.1.0/math.min.js';
  document.head.appendChild(Script);
  Script.onload := procedure
  begin
    console.log('loaded');
    //... do your thing
    console.log('1.2 / (2.3 + 0.7) [Execute] = ' + mathjs.eval('1.2 / (2.3 + 0.7)'));
  end;
end;

Obviously there is a difference between the local library and the cdn library. The latter doesn't recognise the function 'mathjs', or its called differently. I'll have a look later on

Link to post
Share on other sites
  • Moderators

ok, they changed the entry point from a function call to a var, and renamed it to 'math'

//function mathjs: Variant; external 'mathjs';
var mathjs external 'math': variant;

point script.src to 'https://cdnjs.cloudflare.com/ajax/libs/mathjs/5.1.0/math.min.js' and use as before

(instead of referencing the cdn or a local url you can of course store whatever version of the library you like on one of your own servers and point the script.src to that)

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...