Skip to content

JavaScript Binding API

GitHubPang edited this page Dec 7, 2020 · 5 revisions

The Javascript methods that CefSharp provides in relation to JavaScript Binding are detailed below.

Duplicate methods are provided with different case to suit your preferred coding style. You can use CefSharp.BindObjectAsync or cefSharp.bindObjectAsync depending on your preference.


CefSharp.BindObjectAsync(settings, objectName)

Bind the object that matches objectName, the IJavascriptObjectRepository.ResolveObject will be raised with args.ObjectName equal to objectName. If no objectName is provided then All will be passed as args.ObjectName

Param Optional Remarks
settings yes A set of key/value pairs that configure the default binding settings.
objectName yes Name of the object you wish to bind, if no name is provided then All objects will be bound

Returns: a Promise that can be awaited.

Settings
Settings Optional Remarks
NotifyIfAlreadyBound yes boolean (true/false). If true then triggers the IJavascriptObjectRepository.ObjectBoundInJavascript event even if the object is already bound (by default the event will not be called if an object is already bound).
IgnoreCache yes If true then the local cache will be ignored and the request to the IJavascriptObjectRepository will be made

Example 1

(async function()
{
        //`IJavascriptObjectRepository.ResolveObject` will be called with `args.ObjectName` of `All`.
	await CefSharp.BindObjectAsync();
	
	//Objects will have been bound, you can now access them
})();

Example 2

(async function()
{
	await CefSharp.BindObjectAsync("boundAsync");
	
	//Object with name boundAsync will have been bound, you can now access
})();

Example 3

(async function()
{
	await CefSharp.BindObjectAsync({ NotifyIfAlreadyBound: true, IgnoreCache: true }, "boundAsync2");
	
	//Object with name boundAsync2 will have been bound, you can now access them.
        // Cache will have been ignored and notification in `.Net` will have been provided through `IJavascriptObjectRepository.ObjectBoundInJavascript`
})();

CefSharp.DeleteBoundObject(objectName)

Deletes the object that matches objectName

Param Optional Remarks
objectName no Name of the object to be deleted

Returns: bool, true if successful otherwise false

Example 1

  CefSharp.DeleteBoundObject("boundAsync");

CefSharp.RemoveObjectFromCache(objectName)

Removes the object that matches objectName from the cache

Param Optional Remarks
objectName no Name of the object to be removed from the cache

Returns: bool, true if successful otherwise false

Example 1

  CefSharp.RemoveObjectFromCache("boundAsync");
  //You can use lowercase version if you like, exactly the same function
  cefSharp.removeObjectFromCache("boundAsync");

CefSharp.IsObjectCached(objectName)

Does an object with name objectName exist in the cache

Param Optional Remarks
objectName no Name of the object

Returns: bool, true if cached otherwise false

Example 1

  CefSharp.IsObjectCached("boundAsync") === true;
  //You can use lowercase version if you like, exactly the same function
  cefSharp.IsObjectCached("boundAsync") === true;