Garry's Mod Wiki

DHTML

Description

The DHTML control wraps the internal Awesomium framework, supports calling Javascript functions from Lua, as well as running Lua from within the HTML. Running Lua code is disabled by default.

Parent

Derives from Awesomium.

Methods

DHTML:AddFunction( string library, string name, function callback )
Defines a Javascript function that when called will call a Lua callback. Must be called after the HTML document has fully loaded.
DHTML:Call( string js )
Runs/Executes a string as JavaScript code in a panel. This function does NOT evaluate expression (i.e. allow you to pass variables from JavaScript (JS) to Lua context).Because a return value is nil/no value (a.k.a. void).If you wish to pass/return values from JS to Lua, you may want to use DHTML:AddFunction function to accomplish that job.This function is an alias of DHTML:QueueJavascript (source).
DHTML:ConsoleMessage( string msg )
Called when the page inside the DHTML window runs console.log. This can also be called within the Lua environment to emulate console.log. If the contained message begins with RUNLUA: the following text will be executed as code within the Lua environment (this is how Lua is called from DHTML windows).
boolean DHTML:GetAllowLua()
Returns if the loaded page can run Lua code, set by DHTML:SetAllowLua
DHTML:OpenURL( string URL )
Instructs a HTML control to download and parse a HTML script using the passed URL. This function can also be used on HTML.
DHTML:OpenURL( string URL )
Instructs a HTML control to download and parse a HTML script using the passed URL. This function can also be used on HTML.
DHTML:QueueJavascript( string js )
Runs/Executes a string as JavaScript code in a panel. This function does NOT evaluate expression (i.e. allow you to pass variables from JavaScript (JS) to Lua context).Because a return value is nil/no value (a.k.a. void).If you wish to pass/return values from JS to Lua, you may want to use DHTML:AddFunction function to accomplish that job.
DHTML:SetAllowLua( boolean allow = false )
Determines whether the loaded page can run Lua code or not. See DHTML for how to run Lua from a DHTML window.
DHTML:SetScrollbars( boolean show )
We advise against using this. It may be changed or removed in a future update. Broken. Use the CSS overflow rule instead. Sets if the loaded window should display scrollbars when the webpage is larger than the viewing window. This is similar to the CSS overflow rule.
DHTML:StopLoading()
Stops the loading of the HTML panel's current page.
Awesomium doesn’t work with most HTTPS websites because it only supports up to TLS 1.0

Using url/href/src

You might have noticed that Relative File Paths like <img src="/images/picture.jpg"> wont work. There are 3 solutions for that:

asset://

Instead of https:// we use asset:// which will point to our local files.

<img src="asset://garrysmod/materials/example_addon/example.png">
Please don't point towards your Addons folder. Use the File Location where Gmod has your addon files included.

Online

Classic Absolute File Paths still work ... well unless you are offline or the website is down or the website uses HTTPS above TLS 1.0 Version

<img src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png">

Base64 encoding

This will encode your local File into a Base64 String. The String will start with the datatype like data:image/jpeg;base64, followed by a long chain of characters being the Base64 data.

--In HTML <img src="data:image/jpeg;base64,LzlqLzRBQ...<!-- base64 data -->"> --In CSS url("data:image/jpeg;base64,LzlqLzRBQ...<!-- base64 data -->")

Websites like https://www.base64-image.de/ can encode the file for you

Examples

Example

Creates a DHTML.

local frame = vgui.Create( "DFrame" ) frame:SetSize( 300, 200 ) frame:SetTitle( "My new Derma frame" ) frame:SetVisible( true ) frame:SetDraggable( true ) frame:Center() --Fill the form with a html page local html = vgui.Create( "DHTML" , frame ) html:Dock( FILL ) html:SetHTML( [[ <input type='submit' onclick='console.log("RUNLUA:print(\"This is called in Lua context\")")' /> ]] ) --Enable the webpage to call lua code html:SetAllowLua( true ) frame:MakePopup()

Example

Creates a DHTML and opens Google inside.

local frame = vgui.Create("DFrame") frame:SetSize(ScrW() * 0.5, ScrH() * 0.5) frame:SetTitle("HTML Example!") frame:Center() frame:MakePopup() local html = vgui.Create("DHTML", frame) html:Dock(FILL) html:OpenURL("https://www.google.com/")
Output:

Methods

Page Links


Special Pages


Wikis

?

Render Time: 157ms

DB GetPage 41
Generate Html 16
SaveChanges (1) 86
Render Body 0
Render Sidebar 10