TsgcWebView2 provides several methods for navigating to URLs, loading HTML content, and controlling browser history.
Use the Navigate method to load a URL. The component fires OnNavigationStarting before the request and OnNavigationCompleted when it finishes.
// Navigate to a URL
sgcWebView21.Navigate('https://www.example.com');
You can also set the DefaultURL property at design time or before initialization. The component navigates to this URL automatically after WebView2 is ready.
// Set the default URL before initialization
sgcWebView21.DefaultURL := 'https://www.example.com';
Use NavigateToString to load HTML content directly without a server or file.
// Load HTML content directly
sgcWebView21.NavigateToString(
'<html><body><h1>Hello from Delphi</h1>' +
'<p>This content was loaded with NavigateToString.</p>' +
'</body></html>');
Use GoBack and GoForward to navigate the browser history. Check CanGoBack and CanGoForward before calling these methods.
// Navigate back
if sgcWebView21.CanGoBack then
sgcWebView21.GoBack;
// Navigate forward
if sgcWebView21.CanGoForward then
sgcWebView21.GoForward;
Use Reload to refresh the current page and Stop to cancel a pending navigation.
// Reload the current page
sgcWebView21.Reload;
// Stop loading
sgcWebView21.Stop;
Use NavigateWithPostData to send an HTTP request with a custom method, body, and headers. This is useful for submitting form data or calling REST APIs directly in the browser.
// POST form data with custom headers
sgcWebView21.NavigateWithPostData(
'https://api.example.com/login',
'POST',
'username=admin&password=secret',
'Content-Type: application/x-www-form-urlencoded');
// POST JSON data
sgcWebView21.NavigateWithPostData(
'https://api.example.com/data',
'POST',
'{"name":"John","age":30}',
'Content-Type: application/json');
Use OnNavigationStarting to inspect or cancel a navigation before it begins. Set aCancel to True to block the request.
procedure TFormMain.sgcWebView21NavigationStarting(Sender: TObject;
const aURI: string; aIsUserInitiated, aIsRedirected: Boolean;
var aCancel: Boolean);
begin
// Block navigation to unwanted domains
if Pos('ads.example.com', aURI) > 0 then
aCancel := True;
end;
Use OnNavigationCompleted to check whether the navigation succeeded or failed.
procedure TFormMain.sgcWebView21NavigationCompleted(Sender: TObject;
aIsSuccess: Boolean; aWebErrorStatus: Integer);
begin
if aIsSuccess then
StatusBar1.SimpleText := 'Page loaded: ' + sgcWebView21.URL
else
StatusBar1.SimpleText := 'Navigation failed, error: ' +
IntToStr(aWebErrorStatus);
end;
When the page opens a popup or a link with target="_blank", the OnNewWindowRequested event fires. Set aHandled to True to prevent the default popup and navigate in the same window instead.
procedure TFormMain.sgcWebView21NewWindowRequested(Sender: TObject;
const aURI: string; aIsUserInitiated: Boolean;
var aHandled: Boolean);
begin
// Navigate in the same window instead of opening a popup
aHandled := True;
sgcWebView21.Navigate(aURI);
end;