WebView JavaScript to Lua communication

I am trying to get URLs requested via javascript fired in my native.newWebView() to be received by my listener. This is not working and I’d love to know if anyone has got this or something like it working.

I am attempting to send a very simple (short string) message to my Lua code from the web page hosted inside the newWebView. I intended to artificially request a page and to set the url of the request to my short string, hoping that the request would be received, as normal anchor links are, by the listener function attached to the web view. This did not work.

I found this old thread, but the best solution it seems to present is to create a locally hosted web server. That’s a bit of a long haul for something I think should be a lot simpler:

http://forums.coronalabs.com/topic/20108-web-popups/

Here’s my current main.lua:

local function webListener( event ) print(event.url) end local webView = native.newWebView( 300,300 , 600,600 ) webView:request( "test.html", system.ResourceDirectory ) webView:addEventListener( "urlRequest", webListener )

And here’s my test.html:

\<!DOCTYPE html\> \<html\> \<head\> \<script\> function loadXMLDoc() { if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","test.html",true); xmlhttp.send(); } \</script\> \</head\> \<body\> \<h2\>Using the XMLHttpRequest object\</h2\> \<div id="myDiv"\>\</div\> \<button type="button" onclick="loadXMLDoc()"\>Change Content\</button\> \</body\> \</html\>

Hi Matt

I’ve got passing data back and forth to a web view to work OK like this.

To send data from Lua to the web view just add URL parameters to the request string, something like …

[lua]webView:request(“test.html?” … params, system.DocumentsDirectory)[/lua]

Note that the HTML page cannot be in the Resources directory, otherwise it can’'t be updated, and the url parameters need to be url encoded. You can then retrieve each parameter with this function in your HTML file …

[lua]

<script>function getURLParameter(name) {

    return decodeURIComponent((new RegExp(’[?|&]’ + name + ‘=’ + ‘([^&;]+?)(&|#|;|$)’).exec(location.href)||[,""])[1].replace(/+/g, ‘%20’))||null;

}

</script>

[/lua]

To communicate back from the webView to Lua I just use this …

[lua]window.location.href = “<myEvent>” + myString;[/lua]

… and in your Lua code

[lua]

local function webViewListener(event)    

  if event.url then

    if (event.type == “other”) then

      local urlString = url.unescape(event.url)

      local start, end = string.find(urlString, “<myEvent>”)

      if start ~= nil then

        local myString = string.sub(urlString, end + 1)

      end

    end

  end

end

webView:addEventListener( “urlRequest”, webViewListener )

[/lua]

Hope that helps!

Stefan

Thanks Stefan. Have you got that working with an AJAX call? I am trying to detect mouse events and fire them back to the app quite rapidly. All the pieces seem to work but the whole is less than the sum of the parts, for some reason.

I have found this post: http://forums.coronalabs.com/topic/20108-web-popups/ which includes a link to this blog page:  http://www.think-techie.com/2012/05/corona-sdk-how-to-communicate-between.html

There, the code is explaining how to create a small web server in the Corona app with a web page which fires data to the server. I found another post (w3schools I think) which could detect mouse events and update the page with the mouse location.

I merged these examples together to try to send the mouse location to the Lua app but that one join seems to break. Just nothing happens. Here’s my code:

main.lua:

-- Corona SDK: Example how to interact between JS and Lua -- Possible solution for the URL bug in Android honeycomb and ICS -- https://code.google.com/p/xui-sdk/source/browse/#svn%2Fsrc%2FLuaToHTML%253Fstate%253Dclosed local isSimulator = "simulator" == system.getInfo("environment") if not isSimulator then display.setStatusBar( display.HiddenStatusBar ) end if system.getInfo( "platformName" ) == "Mac OS X" then isSimulator = false; end -- Parse values from webpopup function getArgs( query ) local parsed = {} local pos = 0 query = string.gsub(query, "&amp;", "&") query = string.gsub(query, "&lt;", "\<") query = string.gsub(query, "&gt;", "\>") local function ginsert(qstr) local first, last = string.find(qstr, "=") if first then parsed[string.sub(qstr, 0, first-1)] = string.sub(qstr, first+1) end end while true do local first, last = string.find(query, "&", pos) if first then ginsert(string.sub(query, pos, first-1)); pos = last+1 else ginsert(string.sub(query, pos)); break; end end return parsed end -- Write values to be used by webpopup function setArgs( args ) local path = system.pathForFile( "args.json", system.DocumentsDirectory ) local file, errStr = io.open( path, "w+b" ) if file then local newstr = "" for k,v in pairs(args) do if k ~= nil and v ~= nil then if newstr ~= "" then newstr = newstr .. ", " end local val = "" if type(v) == "boolean" or tonumber(v) ~= nil then val = tostring(v) else val = "\"" .. v .. "\"" end newstr = newstr .. "\"" .. k .. "\": " .. val end end local content = "{ " .. newstr .. " }" file:write( content ) file:close() return true end end -- Thank you Matthew Pringle for this piece of code -- http://developer.anscamobile.com/reference/index/networkrequest local function createTCPServer( port ) local socket = require("socket") -- Create Socket local tcpServerSocket , err = socket.tcp() local backlog = 5 -- Check Socket if tcpServerSocket == nil then return nil , err end -- Allow Address Reuse tcpServerSocket:setoption( "reuseaddr" , true ) -- Bind Socket local res, err = tcpServerSocket:bind( "\*" , port ) if res == nil then return nil , err end -- Check Connection res , err = tcpServerSocket:listen( backlog ) if res == nil then return nil , err end -- Return Server return tcpServerSocket end -- code from http://developer.anscamobile.com/node/2937 function copyFile( srcName, srcPath, dstName, dstPath, overwrite ) -- assume no errors local results = true -- Copy the source file to the destination file local rfilePath = system.pathForFile( srcName, srcPath ) local wfilePath = system.pathForFile( dstName, dstPath ) local rfh = io.open( rfilePath, "rb" ) local wfh = io.open( wfilePath, "wb" ) if not wfh then print( "writeFileName open error!") results = false else -- Read the file from the Resource directory and write it to the destination directory local data = rfh:read( "\*a" ) if not data then print( "read error!" ) results = false -- error else if not wfh:write( data ) then print( "write error!" ) results = false -- error end end end -- Clean up our file handles rfh:close() wfh:close() return results end local function doStuff( args ) setArgs( args ) if runTCPServer ~= nil then Runtime:removeEventListener( "enterFrame" , runTCPServer ) end runTCPServer = function() tcpServer:settimeout( 0 ) local tcpClient , \_ = tcpServer:accept() if tcpClient ~= nil then local tcpClientMessage , \_ = tcpClient:receive('\*l') if tcpClient ~= nil then tcpClient:close() end if ( tcpClientMessage ~= nil ) then local myMessage = tcpClientMessage local event = {} local xArgPos = string.find( myMessage, "?" ) if xArgPos then local newargs = getArgs(string.sub( myMessage, xArgPos+1 )) if newargs.shouldLoad == nil or newargs.shouldLoad == "false" then native.cancelWebPopup() else -- do some stuff ... local url = require("socket.url") local str = url.unescape(newargs.arg) print("Value from HTML:" .. (str)) -- send some dumb stuff newargs.arg = tostring(os.date( "\*t" )) setArgs(newargs) -- or you can use send but then you have to re-implement a -- new parser. Note: dont close client before this line -- tcpClient:send( "ssssss" .. "\n") end end end end end if tcpServer == nil then tcpServer, \_ = createTCPServer( "8087" ) end Runtime:addEventListener( "enterFrame" , runTCPServer ) local options = { --hasBackground = false, baseUrl = system.DocumentsDirectory, --urlRequest = function( event ) return true end } -- native.showWebPopup("index.html", options ) webView = native.newWebView( display.actualContentWidth/2, display.actualContentHeight/2, display.actualContentWidth, display.actualContentHeight ) webView:request( "index.html", system.ResourceDirectory ) end -- On my case I use uncompress a tar file containing all the page assets -- This illustrates a simple scenario copyFile( "index.html", system.ResourcesDirectory, "index.html", system.DocumentsDirectory ) local args = {} args.arg = "Hello" doStuff( args )

index.html:

\<!doctype html\> \<html lang="en"\> \<head\> \<meta charset="utf-8"\> \<title\>mousemove demo\</title\> \<style\> div { width: 220px; height: 170px; margin: 10px 50px 10px 10px; background: yellow; border: 2px groove; float: right; } p { margin: 0; margin-left: 10px; color: red; width: 220px; height: 120px; padding-top: 70px; float: left; font-size: 14px; } span { display: block; } \</style\> \<script type="text/javascript"\> var urlVars = {} function getArgs(){ try { var xobj = new XMLHttpRequest(); xobj.overrideMimeType("application/json"); xobj.open('GET', "args.json", false); xobj.send(null); return eval("(" + xobj.responseText + ")"); } catch(err){} return null; } function getVariable( name, defaultValue ){ try { if (urlVars[name] == null) return defaultValue; return urlVars[name]; } catch(err){} return defaultValue; } function update( ){ urlVars = getArgs(); var arg = getVariable( 'arg', 1 ); document.getElementById("method").style.display = 'block'; document.getElementById('method').innerHTML = "Corona p-value: " + arg; return true } function send( params ){ var xobj = new XMLHttpRequest(); xobj.open('GET', "http://127.0.0.1:8087?" + params, true); xobj.onreadystatechange = update( ); xobj.send(null); return true } try{ window.onload = function () { update(); }; } catch(err){ } \</script\> \<script src="jquery-2.1.3.min.js"\>\</script\> \</head\> \<body\> \<p\> \<span\>Move the mouse over the div.\</span\> \<span\>&nbsp;\</span\> \</p\> \<div\>\</div\> \<a href="test"\>LINK\</a\> \<script\> $( "div" ).mousemove(function( event ) { var pageCoords = "( " + event.pageX + ", " + event.pageY + " )"; var clientCoords = "( " + event.clientX + ", " + event.clientY + " )"; $( "span:first" ).text( "page: " + pageCoords ); send('arg=1&shouldLoad=false&'); // DOES NOT SEEM TO DO ANYTHING AT ALL!!! $( "span:last" ).text( "Xclient: " + clientCoords ); }); \</script\> \<h1\>Hello World\</h1\> \<h2 id="method" style="display:none;"\>\</h2\> \<button onclick="send('arg=1&shouldLoad=true&')"\>send 1 to lua\</button\> \<button onclick="send('arg=one&shouldLoad=true&')"\>send one to lua\</button\> \<button onclick="send('arg=1&shouldLoad=false&')"\>close\</button\> \</body\> \</html\>

I also have jquery-2.1.3.min.js in the same app folder for good measure.

Swear words. Many swear words.

It would appear that the commented line above should, in fact, have been:

send('arg=1&shouldLoad=true&');

I now appear to have a web page which sends the mouse location rapidly to the back end. Here’s the complete HTML:

\<!doctype html\> \<html lang="en"\> \<head\> \<meta charset="utf-8"\> \<title\>mousemove demo\</title\> \<style\> div { width: 220px; height: 170px; margin: 10px 50px 10px 10px; background: yellow; border: 2px groove; float: right; } p { margin: 0; margin-left: 10px; color: red; width: 220px; height: 120px; padding-top: 70px; float: left; font-size: 14px; } span { display: block; } \</style\> \<script type="text/javascript"\> var urlVars = {} function getArgs(){ try { var xobj = new XMLHttpRequest(); xobj.overrideMimeType("application/json"); xobj.open('GET', "args.json", false); xobj.send(null); return eval("(" + xobj.responseText + ")"); } catch(err){} return null; } function getVariable( name, defaultValue ){ try { if (urlVars[name] == null) return defaultValue; return urlVars[name]; } catch(err){} return defaultValue; } function update( ){ urlVars = getArgs(); var arg = getVariable( 'arg', 1 ); document.getElementById("method").style.display = 'block'; document.getElementById('method').innerHTML = "Corona p-value: " + arg; return true } function send( params ){ var xobj = new XMLHttpRequest(); xobj.open('GET', "http://127.0.0.1:8087?" + params, true); xobj.onreadystatechange = update( ); xobj.send(null); return true } try{ window.onload = function () { update(); }; } catch(err){ } function other( params ){ send('donk'); return true; } \</script\> \<script src="jquery-2.1.3.min.js"\>\</script\> \</head\> \<body\> \<p\> \<span\>Move the mouse over the div.\</span\> \<span\>&nbsp;\</span\> \</p\> \<div\>\</div\> \<a href="test"\>LINK\</a\> \<script\> $( "div" ).mousemove(function( event ) { var pageCoords = "( " + event.pageX + ", " + event.pageY + " )"; var clientCoords = "( " + event.clientX + ", " + event.clientY + " )"; $( "span:first" ).text( "page: " + pageCoords ); send('arg='+ event.clientX +','+ event.clientY +'&shouldLoad=true&'); $( "span:last" ).text( "Xclient: " + clientCoords ); }); \</script\> \<h1\>Hello World\</h1\> \<h2 id="method" style="display:none;"\>\</h2\> \<button onclick="send('arg=1&shouldLoad=true&')"\>send 1 to lua\</button\> \<button onclick="send('arg=bumble&shouldLoad=true&')"\>send bumble to lua\</button\> \<button onclick="send('arg=1&shouldLoad=true&')"\>send one to lua\</button\> \<button onclick="send('arg=1&shouldLoad=false&')"\>close\</button\> \</body\> \</html\>

Hi Matt

I haven’t tried AJAX calls but have used Javascript in a web view to detect mouse movements in order to send back a <swipe> event to Lua. Looks as though you’ve solved your problem though. I’m curious to know what sort of app you’re creating now!

Stefan

Basically I want to control the position of the web view by detecting touch events on the web page. The reason is because I can’t put a display object over the top of a web view because it sits above the OpenGL layer.

What I’m building is a back-swipe effect, like in the iOS Mail, Notes or Settings (or any of a thousand other apps.) My primary idea is to use Lua to build regular widget objects for navigating a folder hierarchy (like in DropBox, for example) and use that to navigate to articles. Those articles will be downloaded as web pages (because of the system they are stored in) and displayed in a web view. To navigate back to the hierarchy viewer I need to be able to perform a back swipe, hence this investigation.

I have even looked into finding a simple Lua based web page renderer, but they all would require C compiler access and I’m not into Enterprise level coding.

Hi Matt

I’ve got passing data back and forth to a web view to work OK like this.

To send data from Lua to the web view just add URL parameters to the request string, something like …

[lua]webView:request(“test.html?” … params, system.DocumentsDirectory)[/lua]

Note that the HTML page cannot be in the Resources directory, otherwise it can’'t be updated, and the url parameters need to be url encoded. You can then retrieve each parameter with this function in your HTML file …

[lua]

<script>function getURLParameter(name) {

    return decodeURIComponent((new RegExp(’[?|&]’ + name + ‘=’ + ‘([^&;]+?)(&|#|;|$)’).exec(location.href)||[,""])[1].replace(/+/g, ‘%20’))||null;

}

</script>

[/lua]

To communicate back from the webView to Lua I just use this …

[lua]window.location.href = “<myEvent>” + myString;[/lua]

… and in your Lua code

[lua]

local function webViewListener(event)    

  if event.url then

    if (event.type == “other”) then

      local urlString = url.unescape(event.url)

      local start, end = string.find(urlString, “<myEvent>”)

      if start ~= nil then

        local myString = string.sub(urlString, end + 1)

      end

    end

  end

end

webView:addEventListener( “urlRequest”, webViewListener )

[/lua]

Hope that helps!

Stefan

Thanks Stefan. Have you got that working with an AJAX call? I am trying to detect mouse events and fire them back to the app quite rapidly. All the pieces seem to work but the whole is less than the sum of the parts, for some reason.

I have found this post: http://forums.coronalabs.com/topic/20108-web-popups/ which includes a link to this blog page:  http://www.think-techie.com/2012/05/corona-sdk-how-to-communicate-between.html

There, the code is explaining how to create a small web server in the Corona app with a web page which fires data to the server. I found another post (w3schools I think) which could detect mouse events and update the page with the mouse location.

I merged these examples together to try to send the mouse location to the Lua app but that one join seems to break. Just nothing happens. Here’s my code:

main.lua:

-- Corona SDK: Example how to interact between JS and Lua -- Possible solution for the URL bug in Android honeycomb and ICS -- https://code.google.com/p/xui-sdk/source/browse/#svn%2Fsrc%2FLuaToHTML%253Fstate%253Dclosed local isSimulator = "simulator" == system.getInfo("environment") if not isSimulator then display.setStatusBar( display.HiddenStatusBar ) end if system.getInfo( "platformName" ) == "Mac OS X" then isSimulator = false; end -- Parse values from webpopup function getArgs( query ) local parsed = {} local pos = 0 query = string.gsub(query, "&amp;", "&") query = string.gsub(query, "&lt;", "\<") query = string.gsub(query, "&gt;", "\>") local function ginsert(qstr) local first, last = string.find(qstr, "=") if first then parsed[string.sub(qstr, 0, first-1)] = string.sub(qstr, first+1) end end while true do local first, last = string.find(query, "&", pos) if first then ginsert(string.sub(query, pos, first-1)); pos = last+1 else ginsert(string.sub(query, pos)); break; end end return parsed end -- Write values to be used by webpopup function setArgs( args ) local path = system.pathForFile( "args.json", system.DocumentsDirectory ) local file, errStr = io.open( path, "w+b" ) if file then local newstr = "" for k,v in pairs(args) do if k ~= nil and v ~= nil then if newstr ~= "" then newstr = newstr .. ", " end local val = "" if type(v) == "boolean" or tonumber(v) ~= nil then val = tostring(v) else val = "\"" .. v .. "\"" end newstr = newstr .. "\"" .. k .. "\": " .. val end end local content = "{ " .. newstr .. " }" file:write( content ) file:close() return true end end -- Thank you Matthew Pringle for this piece of code -- http://developer.anscamobile.com/reference/index/networkrequest local function createTCPServer( port ) local socket = require("socket") -- Create Socket local tcpServerSocket , err = socket.tcp() local backlog = 5 -- Check Socket if tcpServerSocket == nil then return nil , err end -- Allow Address Reuse tcpServerSocket:setoption( "reuseaddr" , true ) -- Bind Socket local res, err = tcpServerSocket:bind( "\*" , port ) if res == nil then return nil , err end -- Check Connection res , err = tcpServerSocket:listen( backlog ) if res == nil then return nil , err end -- Return Server return tcpServerSocket end -- code from http://developer.anscamobile.com/node/2937 function copyFile( srcName, srcPath, dstName, dstPath, overwrite ) -- assume no errors local results = true -- Copy the source file to the destination file local rfilePath = system.pathForFile( srcName, srcPath ) local wfilePath = system.pathForFile( dstName, dstPath ) local rfh = io.open( rfilePath, "rb" ) local wfh = io.open( wfilePath, "wb" ) if not wfh then print( "writeFileName open error!") results = false else -- Read the file from the Resource directory and write it to the destination directory local data = rfh:read( "\*a" ) if not data then print( "read error!" ) results = false -- error else if not wfh:write( data ) then print( "write error!" ) results = false -- error end end end -- Clean up our file handles rfh:close() wfh:close() return results end local function doStuff( args ) setArgs( args ) if runTCPServer ~= nil then Runtime:removeEventListener( "enterFrame" , runTCPServer ) end runTCPServer = function() tcpServer:settimeout( 0 ) local tcpClient , \_ = tcpServer:accept() if tcpClient ~= nil then local tcpClientMessage , \_ = tcpClient:receive('\*l') if tcpClient ~= nil then tcpClient:close() end if ( tcpClientMessage ~= nil ) then local myMessage = tcpClientMessage local event = {} local xArgPos = string.find( myMessage, "?" ) if xArgPos then local newargs = getArgs(string.sub( myMessage, xArgPos+1 )) if newargs.shouldLoad == nil or newargs.shouldLoad == "false" then native.cancelWebPopup() else -- do some stuff ... local url = require("socket.url") local str = url.unescape(newargs.arg) print("Value from HTML:" .. (str)) -- send some dumb stuff newargs.arg = tostring(os.date( "\*t" )) setArgs(newargs) -- or you can use send but then you have to re-implement a -- new parser. Note: dont close client before this line -- tcpClient:send( "ssssss" .. "\n") end end end end end if tcpServer == nil then tcpServer, \_ = createTCPServer( "8087" ) end Runtime:addEventListener( "enterFrame" , runTCPServer ) local options = { --hasBackground = false, baseUrl = system.DocumentsDirectory, --urlRequest = function( event ) return true end } -- native.showWebPopup("index.html", options ) webView = native.newWebView( display.actualContentWidth/2, display.actualContentHeight/2, display.actualContentWidth, display.actualContentHeight ) webView:request( "index.html", system.ResourceDirectory ) end -- On my case I use uncompress a tar file containing all the page assets -- This illustrates a simple scenario copyFile( "index.html", system.ResourcesDirectory, "index.html", system.DocumentsDirectory ) local args = {} args.arg = "Hello" doStuff( args )

index.html:

\<!doctype html\> \<html lang="en"\> \<head\> \<meta charset="utf-8"\> \<title\>mousemove demo\</title\> \<style\> div { width: 220px; height: 170px; margin: 10px 50px 10px 10px; background: yellow; border: 2px groove; float: right; } p { margin: 0; margin-left: 10px; color: red; width: 220px; height: 120px; padding-top: 70px; float: left; font-size: 14px; } span { display: block; } \</style\> \<script type="text/javascript"\> var urlVars = {} function getArgs(){ try { var xobj = new XMLHttpRequest(); xobj.overrideMimeType("application/json"); xobj.open('GET', "args.json", false); xobj.send(null); return eval("(" + xobj.responseText + ")"); } catch(err){} return null; } function getVariable( name, defaultValue ){ try { if (urlVars[name] == null) return defaultValue; return urlVars[name]; } catch(err){} return defaultValue; } function update( ){ urlVars = getArgs(); var arg = getVariable( 'arg', 1 ); document.getElementById("method").style.display = 'block'; document.getElementById('method').innerHTML = "Corona p-value: " + arg; return true } function send( params ){ var xobj = new XMLHttpRequest(); xobj.open('GET', "http://127.0.0.1:8087?" + params, true); xobj.onreadystatechange = update( ); xobj.send(null); return true } try{ window.onload = function () { update(); }; } catch(err){ } \</script\> \<script src="jquery-2.1.3.min.js"\>\</script\> \</head\> \<body\> \<p\> \<span\>Move the mouse over the div.\</span\> \<span\>&nbsp;\</span\> \</p\> \<div\>\</div\> \<a href="test"\>LINK\</a\> \<script\> $( "div" ).mousemove(function( event ) { var pageCoords = "( " + event.pageX + ", " + event.pageY + " )"; var clientCoords = "( " + event.clientX + ", " + event.clientY + " )"; $( "span:first" ).text( "page: " + pageCoords ); send('arg=1&shouldLoad=false&'); // DOES NOT SEEM TO DO ANYTHING AT ALL!!! $( "span:last" ).text( "Xclient: " + clientCoords ); }); \</script\> \<h1\>Hello World\</h1\> \<h2 id="method" style="display:none;"\>\</h2\> \<button onclick="send('arg=1&shouldLoad=true&')"\>send 1 to lua\</button\> \<button onclick="send('arg=one&shouldLoad=true&')"\>send one to lua\</button\> \<button onclick="send('arg=1&shouldLoad=false&')"\>close\</button\> \</body\> \</html\>

I also have jquery-2.1.3.min.js in the same app folder for good measure.

Swear words. Many swear words.

It would appear that the commented line above should, in fact, have been:

send('arg=1&shouldLoad=true&');

I now appear to have a web page which sends the mouse location rapidly to the back end. Here’s the complete HTML:

\<!doctype html\> \<html lang="en"\> \<head\> \<meta charset="utf-8"\> \<title\>mousemove demo\</title\> \<style\> div { width: 220px; height: 170px; margin: 10px 50px 10px 10px; background: yellow; border: 2px groove; float: right; } p { margin: 0; margin-left: 10px; color: red; width: 220px; height: 120px; padding-top: 70px; float: left; font-size: 14px; } span { display: block; } \</style\> \<script type="text/javascript"\> var urlVars = {} function getArgs(){ try { var xobj = new XMLHttpRequest(); xobj.overrideMimeType("application/json"); xobj.open('GET', "args.json", false); xobj.send(null); return eval("(" + xobj.responseText + ")"); } catch(err){} return null; } function getVariable( name, defaultValue ){ try { if (urlVars[name] == null) return defaultValue; return urlVars[name]; } catch(err){} return defaultValue; } function update( ){ urlVars = getArgs(); var arg = getVariable( 'arg', 1 ); document.getElementById("method").style.display = 'block'; document.getElementById('method').innerHTML = "Corona p-value: " + arg; return true } function send( params ){ var xobj = new XMLHttpRequest(); xobj.open('GET', "http://127.0.0.1:8087?" + params, true); xobj.onreadystatechange = update( ); xobj.send(null); return true } try{ window.onload = function () { update(); }; } catch(err){ } function other( params ){ send('donk'); return true; } \</script\> \<script src="jquery-2.1.3.min.js"\>\</script\> \</head\> \<body\> \<p\> \<span\>Move the mouse over the div.\</span\> \<span\>&nbsp;\</span\> \</p\> \<div\>\</div\> \<a href="test"\>LINK\</a\> \<script\> $( "div" ).mousemove(function( event ) { var pageCoords = "( " + event.pageX + ", " + event.pageY + " )"; var clientCoords = "( " + event.clientX + ", " + event.clientY + " )"; $( "span:first" ).text( "page: " + pageCoords ); send('arg='+ event.clientX +','+ event.clientY +'&shouldLoad=true&'); $( "span:last" ).text( "Xclient: " + clientCoords ); }); \</script\> \<h1\>Hello World\</h1\> \<h2 id="method" style="display:none;"\>\</h2\> \<button onclick="send('arg=1&shouldLoad=true&')"\>send 1 to lua\</button\> \<button onclick="send('arg=bumble&shouldLoad=true&')"\>send bumble to lua\</button\> \<button onclick="send('arg=1&shouldLoad=true&')"\>send one to lua\</button\> \<button onclick="send('arg=1&shouldLoad=false&')"\>close\</button\> \</body\> \</html\>

Hi Matt

I haven’t tried AJAX calls but have used Javascript in a web view to detect mouse movements in order to send back a <swipe> event to Lua. Looks as though you’ve solved your problem though. I’m curious to know what sort of app you’re creating now!

Stefan

Basically I want to control the position of the web view by detecting touch events on the web page. The reason is because I can’t put a display object over the top of a web view because it sits above the OpenGL layer.

What I’m building is a back-swipe effect, like in the iOS Mail, Notes or Settings (or any of a thousand other apps.) My primary idea is to use Lua to build regular widget objects for navigating a folder hierarchy (like in DropBox, for example) and use that to navigate to articles. Those articles will be downloaded as web pages (because of the system they are stored in) and displayed in a web view. To navigate back to the hierarchy viewer I need to be able to perform a back swipe, hence this investigation.

I have even looked into finding a simple Lua based web page renderer, but they all would require C compiler access and I’m not into Enterprise level coding.

Nice work!

I am experimenting with this and it sort of works, but I have found a strange problem: I can’t make jQuery working when the html file is local. It works fine when it’s on a webserver outside the phone (though I didn’t try Horace’s example with the server running on the phone yet). I tried to download jQuery from external server, save it to system.DocumentsDirectory, even embed it in the html that was saved to system.DocumentsDirectory. HTML loads and basic JS works fine, but no jQuery function works. Any ideas, anyone, pls?

Nice work!

I am experimenting with this and it sort of works, but I have found a strange problem: I can’t make jQuery working when the html file is local. It works fine when it’s on a webserver outside the phone (though I didn’t try Horace’s example with the server running on the phone yet). I tried to download jQuery from external server, save it to system.DocumentsDirectory, even embed it in the html that was saved to system.DocumentsDirectory. HTML loads and basic JS works fine, but no jQuery function works. Any ideas, anyone, pls?