How to read/write from HTML-Form Element? (JavaScript?)

Just starting out? Need help? Post your questions and find answers here.
PureLust
Enthusiast
Enthusiast
Posts: 477
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

How to read/write from HTML-Form Element? (JavaScript?)

Post by PureLust »

I've already discovered how to write/set attributes to simple HTML Input-Elements in a WebGadget (like 'text'-, 'checkbox'- or 'submit'-Types).

The way I set attributes/values right now is:

Code: Select all

Command1$ = "void(document.forms[0].[InputField-NAME].value='NewText');"      ; example for setting the text of an Input-Element
Command2$ = "void(document.forms[0].[CheckBox-NAME].checked='1');"            ; example for checking a CheckBox
Command3$ = "void(document.forms[0].[Button-NAME].click());"                  ; example for clicking a Button (an Input-Submit-Element)

SetGadgetText(#Gad_WebGadget, "javascript:" + Command3$)
SetGadgetState(#Gad_WebGadget, #PB_Web_Back)
I'm not sure if this is 'the best way' ... but at last it seams to work.


What I was not able to find out, is:

- how to set attributes/values of more complex Elements (like a TABLE > ITEM > FIELD).
- and even more important for me - how to READ values from Elements (like INPUT or specific FIELDS out of a TABLE)


Does anyone has an Idea how this could be done?
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: How to read/write from HTML-Form Element? (JavaScript?)

Post by JHPJHP »

Enhanced PureBasic / JavaScript examples:
- Barcode Generator, Embed JS into PureBasic, Geolocation: Google Maps
PureLust wrote:how to set attributes/values of more complex Elements (like a TABLE > ITEM > FIELD).
Build the HTML, JS, CSS from a string value: WebGadget; FEATURE_BROWSER_EMULATION; Windows 10;
- optionally, only that part of the script you need modified, the rest can be loaded from a separate file or include
PureLust wrote:and even more important for me - how to READ values from Elements (like INPUT or specific FIELDS out of a TABLE)
Any information you need access to can be placed in the PageTitle attribute (#PB_Web_PageTitle) using a JavaScript EventListener, etc.
- Monitor Changes: an alternative to the #PB_EventType_TitleChange event (Windows only) is a PureBasic timer event
Last edited by JHPJHP on Fri Sep 01, 2017 6:05 am, edited 1 time in total.
HanPBF
Enthusiast
Enthusiast
Posts: 562
Joined: Fri Feb 19, 2010 3:42 am

Re: How to read/write from HTML-Form Element? (JavaScript?)

Post by HanPBF »

Hello JHPJHP,

is JavaScript

Code: Select all

document.title 
the correct way to access the content after

Code: Select all

SetGadgetItemText(WebMain, #PB_Web_PageTitle, P)
?

With my code, it seems that document.title is not set by PureBasic.


I tried event DOMSubtreeModified observation which did not work. So, I checked everything by using

Code: Select all

window.setInterval(function (){
				alert(document.title);
				if(document.title!=PB.Title){
					alert('onreceive');
					PB.onReceiveMessage(document.title);	
				}
			}, 5000);
The timeout of 5000ms and the first alert show me that nothing changed indeed.

Is there any setting needed in IE?


Thanks a lot for any help!
HanPBF
Enthusiast
Enthusiast
Posts: 562
Joined: Fri Feb 19, 2010 3:42 am

Re: How to read/write from HTML-Form Element? (JavaScript?)

Post by HanPBF »

Funny; I saw an evidence for the behaviour.

Code: Select all

SetGadgetItemText(WebMain, #PB_Web_PageTitle, "...")
debug GetGadgetItemText(WebMain, #PB_Web_PageTitle) ;This is not given back the same result:
Now, for me it seems there must be a silly error somewhere in my code...
HanPBF
Enthusiast
Enthusiast
Posts: 562
Joined: Fri Feb 19, 2010 3:42 am

Re: How to read/write from HTML-Form Element? (JavaScript?)

Post by HanPBF »

Damn;

From PureBasic help:

Code: Select all

- SetGadgetItemText(): With #PB_Web_HtmlCode as 'Item' html code can be streamed into the Gadget. (Windows only) 
- GetGadgetItemText(): The following constants can be used to get information (Windows only):


Seems, only reading but not writing for #PB_Web_PageTitle is supported:-(

For a single page application using set of #PB_Web_HtmlCode is not so good.

Any other idea???
HanPBF
Enthusiast
Enthusiast
Posts: 562
Joined: Fri Feb 19, 2010 3:42 am

Re: How to read/write from HTML-Form Element? (JavaScript?)

Post by HanPBF »

The only workaround I have found is to use writing a file to the file system and seeking it with JavaScript.

Code: Select all

var FS = new ActiveXObject('Scripting.FileSystemObject');
var Path = '...';
				
if(FS.FileExists(Path + 'Message.json')){
	alert('onreceive');	
}
Annoying...
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: How to read/write from HTML-Form Element? (JavaScript?)

Post by JHPJHP »

Hi HanPBF,

Use the PageTitle attribute to store data set from a JavaScript ListenerEvent, etc. If needed monitor PageTitle for a change, either with a PureBasic #PB_EventType_TitleChange event (Windows only) or a PureBasic timer event.

Instead or writing to PageTitle, create a response string and SetGadgetItemText again.
- see: Embed JS into PureBasic -- Flot_PieChart.pb (includes/flot_piechart.pbi) [ Generate Random Data ]

Another group of examples that demonstrate what I'm referring to can be found in Geolocation: Google Maps...

... The entire HTML page is held in a string variable, with various configurable values retrieved from PureBasic Gadgets. The modified string variable (HTML page) is manually submitted back to the WebGadget, making a web-service request.
Last edited by JHPJHP on Wed Jul 19, 2017 8:10 am, edited 1 time in total.
HanPBF
Enthusiast
Enthusiast
Posts: 562
Joined: Fri Feb 19, 2010 3:42 am

Re: How to read/write from HTML-Form Element? (JavaScript?)

Post by HanPBF »

Hello JHPJHP,

I don't get it. Can't see in Your examples how a "server" push is realized.

But, working with local file is o.k.. As I said, I don't change the URL, I use a single page application.
I even could embed a script which is updated every 500ms to poll "server" pushes/callbacks.

Better would be using Websocket library from Danilo... but is too much for my scenario.

Thanks again for the info!
Post Reply