Can monkey read URL or html form field strings?
Monkey Forums/Monkey Programming/Can monkey read URL or html form field strings?
| ||
Any way for monkey in either HTML5 or Flash to read the browsers URL string or better yet read a HTML form text field? |
| ||
quicky one-liner for HTML5:Extern Function urlsearch$()="(function(){return window.location.search.slice(1);})" Public Function Main() Print "Search is "+urlsearch() End (this won't work through MServer because it doesn't split off the search string, so you will either need to load the .html file directly, or run it off a real web server) If you have a form in the same page as your app that you want to read while the app is running, doing a similar trick to the one above, but using jQuery, is probably the easiest way. |
| ||
Thanks Warpy, I just want a dirty/quick way of getting some data into my monkey app for testing without using load/save string, adding to the URL should work out but ideally typing in some text boxes would work better. |
| ||
Could anyone be so kind as to post code to show reading/writing from/to a form field box? For example I have a field box on the webpage called "frmName" and I wont my money app (html5/flash) to read the string in that field and also write to that field. |
| ||
OK, here's something that goes out of its way to do everything in the monkey file:Import mojo Extern Function listenForm(id$)="(function(id){document.getElementById(id).addEventListener('keypress',function(e){if(e.which==13){this.className='submitted'}})})" Function getInput$(id$)="(function(id){return document.getElementById(id).value;})" Function submitted:Bool(id$)="(function(id){var f = document.getElementById(id);if(f.className=='submitted'){f.className='';return true;}else{return false;}})" Public Class MyApp Extends App Field name$ Method OnCreate() SetUpdateRate 60 listenForm("input") End Method OnUpdate() If submitted("input") Local firstName$ = getInput("firstName") Local lastName$ = getInput("lastName") name = firstName+" "+lastName Endif End Method OnRender() DrawText "Hello, "+name+".",0,0 End End Function Main() New MyApp End with the following html inserted inside the body tag in MonkeyGame.html <form id="input"> <label for="firstName">First name:</label> <input id="firstName"/> <label for="lastName">Last name:</label> <input id="lastName"/> </form> </script> |
| ||
Thanks Warpy, just what I need. =D |
| ||
This is great stuff. Out of interest, could a string be exported out of Monkey to a textbox with a similar method? (or just to a new page using query or something) |
| ||
Yes.Function setInput(id$,value$)="(function(id,value){document.getElementById(id).value = value;})" |
| ||
wonderful, cheers! |