ExecuteBrowserJavascript: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Added example)
(→‎Example: add semicolon)
 
(5 intermediate revisions by 4 users not shown)
Line 21: Line 21:
This example shows how to display the name (nick) of the local player on the webpage.
This example shows how to display the name (nick) of the local player on the webpage.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local browser = guiCreateBrowser(200, 200, 400, 200, true, false, false)
local browser = guiGetBrowser(guiCreateBrowser(200, 200, 400, 200, true, false, false))


addEventHandler("onClientBrowserCreated", browser,
addEventHandler("onClientBrowserCreated", browser,
Line 31: Line 31:
addEventHandler("onClientBrowserDocumentReady", browser,
addEventHandler("onClientBrowserDocumentReady", browser,
     function ()
     function ()
         executeBrowserJavascript(source, "document.getElementById('nick').innerHTML = '" .. getPlayerName(localPlayer) .. "'");
         executeBrowserJavascript(source, "document.getElementById('nick').innerHTML = '" .. getPlayerName(localPlayer) .. "';")
     end)
     end)
</syntaxhighlight>
</syntaxhighlight>
Line 37: Line 37:
==See Also==
==See Also==
{{CEF_functions}}
{{CEF_functions}}
[[hu:executeBrowserJavascript]]
[[RO:executeBrowserJavascript]]

Latest revision as of 08:08, 7 March 2022

This function executes a Javascript string to the specified browser. Works only with local browsers.

Syntax

bool executeBrowserJavascript ( browser webBrowser, string jsCode )

OOP Syntax Help! I don't understand this!

Method: browser:executeJavascript(...)


Required Arguments

  • webBrowser: The web browser which will execute the Javascript code
  • jsCode: The Javascript code string

Returns

Returns true if executing Javascript is allowed in the current context, false otherwise.

Example

This example shows how to display the name (nick) of the local player on the webpage.

local browser = guiGetBrowser(guiCreateBrowser(200, 200, 400, 200, true, false, false))

addEventHandler("onClientBrowserCreated", browser,
    function ()
        loadBrowserURL(source, "http://mta/local/example.html") --Containing <span id="nick"></span> somewhere in the file
    end)

--The page has to load first
addEventHandler("onClientBrowserDocumentReady", browser,
    function ()
        executeBrowserJavascript(source, "document.getElementById('nick').innerHTML = '" .. getPlayerName(localPlayer) .. "';")
    end)

See Also