Getting data from JS to RB

new BookmarkLockedFalling
billw
Guest

Guest Avatar

Post by billw on Dec 17, 2007 20:19:47 GMT -5

This had been puzzling me before my code vacation, but I think I'll look at it with fresh eyes.

There are a number of circumstances in which it may be ideal to get a value from Javascript into Run BASIC. For instance, using a framework like OpenRico or script.aculo.us, it would be possible to do some pretty neat effects and other stuff. I know there are other use cases, but I just can't think of any right now... my brain is fried ATM...

Anyway, my first approach was to set up a hidden textbox that is used to pass values back to the script. The only drawback to this (beta 5) was that a textbox's contents$() method doesn't seem to catch values entered using the element's "value" DOM property. I haven't tried it outside RB, but the stuff I was putting in using JS did show up on-screen when I un-hid the textbox.

Perhaps, and this is just a muse, the script needs to throw the "blur" DOM event.

If anybody has any input, it'd be much appreciated!

thanks,
- Bill
turbov21
Guest

Guest Avatar

Post by turbov21 on Dec 17, 2007 22:49:28 GMT -5

cls
textarea #xyz, ""
print ""
link #abc, "Click me", [nownow]
print " ";
html "<a href=""#"" onclick=""document.getElementsByTagName('textarea')[0].value='testing 1 2 3';"">Testing</a>"
wait

[nownow]
cls
html "<h1>" + #xyz contents$() + "</h1>"


Try that.
billw
Guest

Guest Avatar

billw
Guest

Guest Avatar

turbov21
Guest

Guest Avatar

alix
Junior Member
**

alix Avatar

Posts: 57

Post by alix on May 26, 2008 5:59:14 GMT -5

the code :

html "<a href=""#"" onclick=""document.getElementsByTagName('textarea')[0].value='testing 1 2 3';"">Testing</a>"


Should be :

html "<a href=""#"" onclick=""document.getElementsByTagName('textarea')[1].value='testing 1 2 3';"">Testing</a>"

no ? otherwise, the JS value gets to be written in RB's main editor window.

@+


Stefan: changed JB's to RB's
Last Edit: May 26, 2008 12:52:18 GMT -5 by StefanPendl
Just Learning...
Jerry Muelver
Administrator
*****

Jerry Muelver Avatar

Posts: 521

Post by Jerry Muelver on May 26, 2008 7:10:58 GMT -5

Yes! textarea[0] refers to the RB IDE textarea... while programming. But it refers to the first created textarea when running as a published program, or when running in the IDE as Full Screen.

And there's a good lesson in that, too. Any Javascript calls or CSS rules that reference document elements have to take into account the fact that RB programming and deployment happens in a browser environment. Test them by publishing and running the program from the Published access, or in the IDE run as "Full Screen". With other languages, you write the code cold, outside of the operating environment, and environmental references come into play in the DEPLOYMENT environment. With RB, you have to pay attention to when sandbox you're playing in. ::)
StefanPendl
Global Moderator
*****

StefanPendl Avatar

Run for BASIC ...
Posts: 945

[b]Stefan[/b] - [a href=http://stefanpendl.runbasichosting.com/]Homepage[/a][br][br][b]Please give credit if you use code I post, no need to ask for permission.[/b][br][br]Run BASIC 1.01, Fire-/Waterfox (IE11, Edge), Windows 10 Professional x64, Intel Core i7-4710MQ 2.5GHz, 16GB RAM
alix
Junior Member
**

alix Avatar

Posts: 57

Post by alix on May 26, 2008 15:54:19 GMT -5

Thanks for these explanations. Internet programming is pretty tricky but I feel I've got somewhere today: :)



'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' html :
' ID is a universal attribute. Same as "class" except it applies to
' one element only and stays valid in the whole
' document. example <img src="whale.jpg" alt="logo" id="logo">
'
' JavaScript :
' "getElementById" is a function which gives you access to an
' element(like the textbox in this program). It
' takes the ID as a parameter.
'
' RB :
' SETID :sets the ID value in generated HTML for the following web
' widgets: LINK, CHECKBOX, TABLE, LISTBOX, TEXTBOX,
' TEXTAREA, PASSWORDBOX. By default the ID will be the same as the widget's handle.
'
' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

cls
textarea #xyz, ""
#xyz SetID("xyz")
print ""
link #abc, "Click me", [nownow]
print " ";
html "<a href=""#"" onclick=""document.getElementById('xyz').value='testing 1 2 3';"">Testing</a>"
wait

[nownow]
cls
html "<h1>" + #xyz contents$() + "</h1>"
Last Edit: May 28, 2008 4:51:46 GMT -5 by alix
Just Learning...
alix
Junior Member
**

alix Avatar

Posts: 57

Post by alix on May 28, 2008 5:01:21 GMT -5

Here, the textbox is hidden so the data is passed transparently from JS to RB.
It works ! :o
But I don't really understand why the element ID must be 'nodisplay' in the first link, and #xyz in the second link ? ::)



cls
cssid #nodisplay,"{display:none;}"
textarea #xyz, ""
#xyz SetID("nodisplay")
print ""

print " ";
html "<a href=""#"" onclick=""document.getElementById('nodisplay').value='testing 1 2 3';"">Save JS value</a>"
print
link #abc, "Is it really saved?", [nownow]
wait

[nownow]
print
string$=#xyz contents$()
print "JS value = ";string$
Just Learning...
StefanPendl
Global Moderator
*****

StefanPendl Avatar

Run for BASIC ...
Posts: 945

Post by StefanPendl on May 28, 2008 10:34:43 GMT -5

alix Avatar
But I don't really understand why the element ID must be 'nodisplay' in the first link, and #xyz in the second link ? ::)
'...
cssid #nodisplay,"{display:none;}"
textarea #xyz, ""
#xyz SetID("nodisplay")
'...
string$=#xyz contents$()
'...

SETID sets the CSSID of the widget, so you need to use the handle of the CSSID.
If you like to access the widget, you need to use the handle of the widget, that it got during creation.
Clear ???
[b]Stefan[/b] - [a href=http://stefanpendl.runbasichosting.com/]Homepage[/a][br][br][b]Please give credit if you use code I post, no need to ask for permission.[/b][br][br]Run BASIC 1.01, Fire-/Waterfox (IE11, Edge), Windows 10 Professional x64, Intel Core i7-4710MQ 2.5GHz, 16GB RAM