SetBrowserAjaxHandler: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ {{Client_function}} {{New feature/item|3.0160|1.6|| This function provides a requestable ajax resource for Lua/Javascript communication for a Element/Browser|brows...")
 
m (Link to the Romanian translation of this page added.)
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Client_function}}
{{Client function}}
{{New feature/item|3.0160|1.6||
{{New feature/item|3.0151|1.5.1||
This function provides a requestable ajax resource for Lua/Javascript communication for a [[Element/Browser|browser]].
This function provides a requestable ajax resource for Lua/Javascript communication for a [[Element/Browser|browser]].
}}
}}
Line 7: Line 7:
==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool setBrowserAjaxHandler ( browser webBrowser, string url[, function handler] )
bool setBrowserAjaxHandler ( browser webBrowser, string url [, function handler] )
</syntaxhighlight>
</syntaxhighlight>
{{OOP||[[Element/Browser|browser]]:setAjaxHandler||setBrowserAjaxHandler }}
{{OOP||[[Element/Browser|browser]]:setAjaxHandler}}


===Required Arguments===
===Required Arguments===
*'''webBrowser:''' The web browser which will execute the Javascript code
*'''webBrowser:''' The web browser which will execute the Javascript code
*'''url:''' The URL endpoint to handle  
*'''url:''' The URL endpoint to handle
{{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===
===Optional Arguments===
Line 20: Line 22:
===Additional Information===
===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.
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.
{{Warning|Due to CEF Limitations POST parameters do not work and will (until fixed) always be empty.}}


==Example==
==Example==
Line 59: Line 60:
function()
function()
--After the browser has been initialized, we can load our file.
--After the browser has been initialized, we can load our file.
loadBrowserURL(webBrowser, "ajax.html?hello=world")
loadBrowserURL(webBrowser, "http://mta/local/ajax.html?hello=world")
--Now we can start to render the browser.
--Now we can start to render the browser.
addEventHandler("onClientRender", root, webBrowserRender)
addEventHandler("onClientRender", root, webBrowserRender)
Line 70: Line 71:
==See Also==
==See Also==
{{CEF_functions}}
{{CEF_functions}}
[[hu:setBrowserAjaxHandler]]
[[RO:setBrowserAjaxHandler]]

Latest revision as of 14:39, 8 April 2020

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

Syntax

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.

Example

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)
end


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)
		end 
		
		output = output..getParameters.."</pre>"
		return output
	end
);
 
--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, 
	function()
		--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)
	end
)

Returns

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

See Also