Garry's Mod Wiki

Revision Difference

DHTML:AddFunction#513217

<function name="AddFunction" parent="DHTML" type="panelfunc">⤶ <ispanel>yes</ispanel>⤶ <description>⤶ Defines a Javascript function that when called will call a Lua callback.⤶ <note>Must be called after the HTML document has fully loaded.</note>⤶ </description>⤶ <realm>Client</realm>⤶ <args>⤶ <arg name="library" type="string">Library name of the JS function you are defining.</arg>⤶ <arg name="name" type="string">Name of the JS function you are defining.</arg>⤶ <arg name="callback" type="function">Function called when the JS function is called. Arguments passed to the JS function will be passed here.</arg>⤶ </args>⤶ </function>⤶ ⤶ <example>⤶ <description>Prints text from Javascript to the console in color.</description>⤶ <code>⤶ -- Create the frame⤶ local f = vgui.Create("DFrame")⤶ f:SetSize(800, 600)⤶ f:Center()⤶ ⤶ -- Create a green color variable⤶ local color_green = Color(0, 255, 0)⤶ ⤶ -- Define the Javascript function in the DHTML element⤶ local DHTML = vgui.Create("DHTML", f)⤶ DHTML:Dock(FILL)⤶ DHTML:OpenURL("/VGUI/Elements/DHTML")⤶ DHTML:AddFunction("console", "luaprint", function(str)⤶ MsgC(color_green, str) -- Print the given string⤶ end)⤶ ⤶ --This runs our function. Our function could also be called from Javascript on the DHTML's page.⤶ DHTML:RunJavascript("console.luaprint('Hello from Javascript!');")⤶ </code>⤶ ⤶ </example>