From Multi Theft Auto: Wiki
Jump to navigation Jump to search

This function provides a requestable ajax resource for Lua/Javascript communication for a browser.


bool setBrowserAjaxHandler ( browser webBrowser, string url [, function handler] )

OOP Syntax Help! I don't understand this!

Method: browser:setAjaxHandler(...)

Required Arguments

  • webBrowser: The web browser which will execute the Javascript code
  • url: The URL endpoint to handle
[[|link=|]] Warning: Do not use the same path as an existing file as url parameter. Ajax handlers have a higher priority than regular files, which will lead to inaccesibility of the original file if an ajax handler is attached to the same path.

Optional Arguments

  • handler: The function to call if the webBrowser attempts to open the ajax endpoint. If this parameter is nil or omitted, the ajax handler for the url will be deleted.

Additional Information

The handling function (if given), will be called with two tables, representing GET and POST parameters. The handling function may return a string which will be provided to the browser as file content.


This example will output all GET Parameters as well the number of requests made to the ajax endpoint.

--In order to render the browser on the full screen, we need to know the dimensions.
local screenWidth, screenHeight = guiGetScreenSize()
--Let's create a new browser in local mode. We will not be able to load an external URL.
local webBrowser = createBrowser(screenWidth, screenHeight, true, false)
--This is the function to render the browser.
function webBrowserRender()
	--Render the browser on the full size of the screen.
	dxDrawImage(0, 0, screenWidth, screenHeight, webBrowser, 0, 0, 0, tocolor(255,255,255,255), true)

local counter = 0
setBrowserAjaxHandler(webBrowser, "ajax.html",
	function(get, post)
		counter = counter+1
		local output = string.format("<pre>You have requested this page %d times.\n", counter)
		-- List Parameters
		local getParameters = "Get Parameters: \n"
		for k, v in pairs(get) do 
			getParameters = getParameters..string.format("[%s] = %s\n", k, v)
		output = output..getParameters.."</pre>"
		return output
--The event onClientBrowserCreated will be triggered, after the browser has been initialized.
--After this event has been triggered, we will be able to load our URL and start drawing.
addEventHandler("onClientBrowserCreated", webBrowser, 
		--After the browser has been initialized, we can load our file.
		loadBrowserURL(webBrowser, "http://mta/local/ajax.html?hello=world")
		--Now we can start to render the browser.
		addEventHandler("onClientRender", root, webBrowserRender)


Returns true if the ajax handler could be created/removed.

See Also