Referring to Boolean from 'script' section

Learning APL or new to Dyalog? Ask "silly" questions here, without fear...

Referring to Boolean from 'script' section

Postby Jinny on Sun Oct 27, 2019 7:56 pm

Hi there,
I'd be very grateful for some help as i'm stuck again!

I am using MiServer. I have an APL boolean in my Compose function. I need to refer to it from within a scripted section for a conditional action. I've tried along the lines below:


dark← 6 ∊ darklist
Add _.script ScriptFollows
⍝var darknew = document.getElementById ('dark');
⍝ if (darknew) {
⍝....
⍝....
⍝ }

Both of the variables (dark and darknew) appear to be recognised as existing in the scripted section. However it always seems to contain a zero value. The code works fine if I assign the variable value within the scripted section (⍝var darknew = 1)

Many thanks, Jinny
Jinny
 
Posts: 31
Joined: Sun Jul 01, 2018 10:15 am

Re: Referring to Boolean from 'script' section

Postby RichardP|Dyalog on Mon Oct 28, 2019 9:57 am

So the web page (and therefore the Javascript) doesn't automatically pick up APL variables from the workspace, they need to be retrieved from the server. In the case of
Code: Select all
⍝var darknew = 1
, you are assigning a JS variable "darknew" the value 1. However, using
Code: Select all
dark←6∊darklist
doesn't create an html element with ID "dark".

One way to get the value of APL's "dark" into your web page is to construct a javascript statement as an APL text vector. I misread at first and thought you wanted darklist in there. If you want to pass a list to Javascript, use ⎕JSON to format the list properly.

In the code below, the title displays "My App" unless you remove the line "dark←0", in which case the title displays "darknew works".

Code: Select all
  ∇ Compose
  :Access Public
  'title'Add _.h1 'My App'
  darklist←6 6 4 10 5 3 1 10 1 6
  dark←6∊darklist
  dark←0
  script←'var darknew = ', dark,';'
  script,←'console.log("test");'
  Add _.script script
  Add _.script ScriptFollows
  ⍝if (darknew) {
  ⍝console.log("darknew");
  ⍝document.getElementById("title").innerHTML = "darknew works";
  ⍝}
  ∇
RichardP|Dyalog
 
Posts: 30
Joined: Fri Oct 12, 2018 3:05 pm

Re: Referring to Boolean from 'script' section

Postby Jinny on Thu Oct 31, 2019 8:02 am

Many thanks. I haven't managed to get it to work yet - but still experimenting with fresh optimism!
Jinny
 
Posts: 31
Joined: Sun Jul 01, 2018 10:15 am

Re: Referring to Boolean from 'script' section

Postby Jinny on Thu Nov 21, 2019 7:01 pm

Hooray, it's working.

However, I thought that it might be useful for other readers to know why it didn't work immediately. For some reason, cutting and pasting the lines failed but when I typed them on from scratch they were fine.

Thank you again!
Jinny
 
Posts: 31
Joined: Sun Jul 01, 2018 10:15 am


Return to New to Dyalog?

Who is online

Users browsing this forum: No registered users and 1 guest