References Getting Started Getting Started Architecture Architecture Application Application Window Window WebView WebView Deployment Deployment Components Components Framework Integrations Framework Integrations Examples Examples Community Community github GitHub github Get Started arrow_up_right
Getting Started Getting Started Architecture Architecture Application Application Window Window WebView WebView Deployment Deployment Components Components Framework Integrations Framework Integrations Examples Examples Community Community

WebView

WebView Configuration Events Schemes API Bindings API Data API Scripts API Web Components API Battery API Network API Security API Getting Started Architecture Application Window WebView Deployment Components Framework Integrations Examples Community

Scripts API

This API should be used to register and call arbitrary JavaScript code in WebView.

The API is available in the WebView::$scripts property.

$app = new Boson\Application();

$app->webview->scripts; // Access to Scripts API

JavaScript Evaluation

You can execute arbitrary code directly on current WebView document.

The code will be executed immediately once on the current page (on the currently loaded document).

$app = new Boson\Application();

// Load empty page
$app->webview->html = '';

// Evaluate JS code on this page
$app->webview->scripts->eval(
    'document.write("Hello World!")'
);

WebView also provides a more convenient way (facade method eval()) to execute arbitrary JavaScript code.

Just use WebView::eval() instead of eval method from WebView::$scripts.

$js = 'alert("Hello!")';

$webview->scripts->eval($js);
$js = 'alert("Hello!")';

$webview->eval($js);

In all examples from here on, the short facade method will be used to simplify the examples.

Ready-state Registration

You can register a JavaScript code that will be applied to any page.

This code will be called every time on every page after the document has been processed by the client and its DOM is ready and available.

$app = new Boson\Application();

$app->webview->scripts->add(<<<'JS'
    alert('hello');
    JS);

Or set scripts from configuration

$app = new Boson\Application(
    window: new Boson\Window\WindowCreateInfo(
        webview: new Boson\WebView\WebViewCreateInfo(
            scripts: [
                "alert('hello')",
            ],
        ),
    ),
);

Preloading

It is worth noting that adding code is available in several options.

In particular, if you need to register a set of scripts that will be executed every time on each page before its content is loaded and processed, then you should use the preload() method.

$app = new Boson\Application();

// "This code will be executed BEFORE the page loads: undefined"
$app->webview->scripts->preload(<<<'JS'
    console.log('This code will be executed BEFORE the page loads: ' 
        + document?.body?.innerHTML);
    JS);
    
// Loading content (making a navigation)
$app->webview->html = '<b>hello</b>';
github discord telegram Get started Documentation Contribution Guide License Release Notes BOSON PHP © 2025. All Rights Reversed.