# DHTML:AddFunction

- **Ortam:** İstemci, Menü
- **İngilizce aslı:** https://wiki.facepunch.com/gmod/DHTML:AddFunction
- **Çeviri tarihi:** 2025-09-08

```lua
DHTML:AddFunction( string library, string name, function callback )
```

## Açıklama

Çağrıldığında bir Lua geri çağrısını (callback) tetikleyecek bir Javascript fonksiyonu tanımlar.

> **NOT:** HTML belgesi tamamen yüklendikten sonra çağrılmalıdır.

## Argümanlar

1. [string](https://gmod.tr/wiki/string) **library**: Tanımladığınız JS fonksiyonunun kütüphane adı.
2. [string](https://gmod.tr/wiki/string) **name**: Tanımladığınız JS fonksiyonunun adı.
3. [function](https://gmod.tr/wiki/function) **callback**: JS fonksiyonu çağrıldığında çalıştırılacak olan fonksiyon. JS fonksiyonuna iletilen argümanlar buraya da iletilir.

### Örnek

Javascript'ten gelen metni konsola renkli olarak yazdırır.

```lua
-- Create the frame
local frame = vgui.Create( "DFrame" )
frame:SetSize( 800, 600 )
frame: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", frame )
DHTML:Dock( FILL )
DHTML:OpenURL( "https://wiki.gmodtr.com/gmodTRTRTR/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!' );" )
```

### Örnek

Eklenen JavaScript fonksiyonuna son argüman olarak bir geri çağrı (callback) iletmek, Lua fonksiyonumuzdan döndürülen değerleri kullanmamızı sağlar.

Elbette, her biri kendi argümanı olacak şekilde birden fazla dönüş değeri kullanmanıza izin verilir.

Tablolar beklediğiniz gibi çalışmalıdır. Lua tablosu `{true, a = true}`, her ikisi de `true` değerine eşit olan `1` ve `a` anahtarlarına sahip bir nesne olacaktır. Metatablo davranışları belirsizdir.

Userdata (`Vector`, `Player`, vb.) doğal olarak `undefined` (tanımsız) olacaktır.

[https://github.com/Facepunch/garrysmod-issues/issues/3995#issuecomment-531491316](https://github.com/Facepunch/garrysmod-issues/issues/3995#issuecomment-531491316) adresindeki bilgiye göre

> **NOT:** Bu durum yalnızca x86-64 dalı için geçerlidir (Awesomium yerine Chromium kullanılan sürüm), yine de varsayılan dalda da çalışması için [dönüş değerlerini normal şekilde işlemeye](https://github.com/Facepunch/garrysmod/blob/b7da4993e8fbee1789eee4cded85114849d17699/garrysmod/html/js/svc.Tranny.js#L21-L34) devam edebilirsiniz.

```lua
] lua_run_cl A = vgui.Create"DHTML"
] lua_run_cl A:OpenURL("https://duckduckgo.com/html/")
] lua_run_cl A:AddFunction("gmod", "test", function(x) return x end )
-- This works
] lua_run_cl A:RunJavascript("gmod.test('test', console.log)")
[HTML] test
] lua_run_cl A:AddFunction("gmod", "say", function(x) RunConsoleCommand("say", x) end)
] lua_run_cl A:RunJavascript("gmod.test('test', gmod.say)")
Bell: test
-- This doesn't work
] lua_run_cl A:RunJavascript("gmod.test(console.log, 'test')")
```

- [GMod Wiki ana sayfası](https://gmod.tr/wiki)

---

[Tüm sunucular](https://gmod.tr/) · [GmodTR Launcher](https://gmod.tr/launcher) · [Rehber](https://gmod.tr/rehber) · [GMod Wiki](https://gmod.tr/wiki) · [GmodTR nedir?](https://gmod.tr/hakkinda) · [Sunucu ekle](https://gmod.tr/sunucu-ekle) · [Discord](https://discord.gg/gmodtr)

Kaynak: https://gmod.tr/wiki/DHTML:AddFunction
