LoadBrowserURL: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Add note about createBrowser being asynchronous, with reference to onClientBrowserCreated)
 
(16 intermediate revisions by 11 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Client Function}}
{{Client function}}
{{New feature/item|3.0150|1.5||
{{New feature/item|3.0150|1.5||
This function loads the specified URL.
This function loads the specified URL.
{{Note|You should use [[requestBrowserDomains]] first to request permission to load the url on the client.}}
{{Note|Calling loadBrowserURL right after [[createBrowser]] will not work normally due to the nature of the asynchronous browser interface. Refer to [[onClientBrowserCreated]] for more information.}}
}}
}}


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">bool loadBrowserURL ( browser webBrowser, string url )</syntaxhighlight>
<syntaxhighlight lang="lua">bool loadBrowserURL ( browser webBrowser, string url [, string postData = "", bool urlEncoded = true ] )</syntaxhighlight>
{{OOP||[[Element/Browser|browser]]:loadURL||loadBrowserURL}}
{{OOP||[[Element/Browser|browser]]:loadURL|url|getBrowserURL}}


===Required arguments===
===Required arguments===
*'''webBrowser:''' The [[browser]] element which will load the URL
*'''webBrowser:''' The [[Element/Browser|browser]] element which will load the URL
*'''url:''' The url you want to load. It can either contain a remote website ("http://" prefix) or a website stored within a local resource ("gui.html" for example).
*'''url:''' The url you want to load. It can either contain a remote website ("http://" prefix) or a website stored within a local resource ("http://mta/local/gui.html" for example, see [[Local_Scheme_Handler|Local Scheme Handler]] for details).
 
===Optional Arguments===
*'''postData:''' The post data passed to the website. Its content type can be any type (e.g. JSON) if urlEncoded is set to ''false''
*'''urlEncoded:''' If set to ''true'', it will be available f.e. in PHP's $_POST variable (the content type is: ''application/x-www-form-urlencoded'')


===Returns===
===Returns===
Line 18: Line 24:
==Example==
==Example==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
--In order to render the browser on the full screen, we need to know the dimensions.
-- In order to render the browser on the full screen, we need to know the dimensions.
local screenWidth, screenHeight = guiGetScreenSize()
local screenWidth, screenHeight = guiGetScreenSize()


--Let's create a new browser in local mode. We will not be able to load an external URL.
-- 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)
local webBrowser = createBrowser(screenWidth, screenHeight, false, false)
--This is the function to render the browser.
-- This is the function to render the browser.
function webBrowserRender()
function webBrowserRender()
--Render the browser on the full size of the screen.
-- 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)
dxDrawImage(0, 0, screenWidth, screenHeight, webBrowser, 0, 0, 0, tocolor(255,255,255,255), true)
end
end


--The event onClientBrowserCreated will be triggered, after the browser has been initialized.
-- 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.
-- After this event has been triggered, we will be able to load our URL and start drawing.
addEventHandler("onClientBrowserCreated", webBrowser,  
addEventHandler("onClientBrowserCreated", webBrowser,  
function()
function()
--After the browser has been initialized, we can load our file.
-- After the browser has been initialized, we can load our website.
loadBrowserURL(webBrowser, "html/site.html")
loadBrowserURL(webBrowser, "https://www.youtube.com/")
--Now we can start to render the browser.
 
-- Now we can start to render the browser.
addEventHandler("onClientRender", root, webBrowserRender)
addEventHandler("onClientRender", root, webBrowserRender)
end
end
Line 44: Line 51:
==See also==
==See also==
{{CEF_functions}}
{{CEF_functions}}
[[hu:loadBrowserURL]]
[[RO:loadBrowserURL]]

Latest revision as of 14:40, 21 December 2020

This function loads the specified URL.

[[{{{image}}}|link=|]] Note: You should use requestBrowserDomains first to request permission to load the url on the client.
[[{{{image}}}|link=|]] Note: Calling loadBrowserURL right after createBrowser will not work normally due to the nature of the asynchronous browser interface. Refer to onClientBrowserCreated for more information.

Syntax

bool loadBrowserURL ( browser webBrowser, string url [, string postData = "", bool urlEncoded = true ] )

OOP Syntax Help! I don't understand this!

Method: browser:loadURL(...)
Variable: .url
Counterpart: getBrowserURL


Required arguments

  • webBrowser: The browser element which will load the URL
  • url: The url you want to load. It can either contain a remote website ("http://" prefix) or a website stored within a local resource ("http://mta/local/gui.html" for example, see Local Scheme Handler for details).

Optional Arguments

  • postData: The post data passed to the website. Its content type can be any type (e.g. JSON) if urlEncoded is set to false
  • urlEncoded: If set to true, it will be available f.e. in PHP's $_POST variable (the content type is: application/x-www-form-urlencoded)

Returns

Returns true if the URL was successfully loaded.

Example

-- 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, false, 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

-- 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 website.
		loadBrowserURL(webBrowser, "https://www.youtube.com/")

		-- Now we can start to render the browser.
		addEventHandler("onClientRender", root, webBrowserRender)
	end
)

See also