TEdgeBrowser not executing local JavaScript files from HTML pages

I’m experiencing an issue with TEdgeBrowser in my Delphi project. When I try to load an HTML file that includes local JavaScript files, the scripts do not run at all. Here’s my setup:

<!DOCTYPE html>
<html>
    <head>
        <title>My Test Page</title>
    </head>
    <body>
        <h1>Testing JavaScript</h1>
        <script src="myScript.js"></script>
    </body>
</html>

The contents of my JavaScript file are:

console.log('Script loaded successfully');
alert('Welcome to my app');

It’s strange because when I open this HTML file directly in Chrome or Edge, it works just as expected. The alert appears, and I see the console log message. However, within TEdgeBrowser, there’s no response—no alerts, no console output, and importantly, no errors in the DevTools.

I’ve also noticed that external JavaScript files load correctly, but local ones don’t seem to work at all. Even though I’m running Delphi as an administrator, it doesn’t change anything. I’m using Delphi 10.4.2. Does anyone have any insights on what might be causing this?

Had this exact issue and it drove me crazy. The problem is WebView2 treats local files as untrusted by default. Don’t mess with browser arguments - that’s a security nightmare. Use the WebResourceRequested event handler instead. Just intercept requests for your local JS files and serve them through streams via the WebView2 engine. You keep security intact while controlling how local resources load. Set up a custom scheme (or stick with http) and handle requests directly in Delphi. Way more reliable than trying to hack around the security restrictions.

Been there with TEdgeBrowser and local scripts. WebView2 blocks local file access by default - it’s a security thing. Don’t bother fighting it or spinning up servers. Just embed your JavaScript straight into the HTML using <script> tags (no src attribute). Load your JS as a string resource in Delphi and inject it before the page loads. Or use AddWebResourceRequestedFilter to catch local file requests and serve them through WebView2 itself. Both ways keep everything self-contained without hacky workarounds that’ll probably break later.

Classic CORS security issue. TEdgeBrowser handles local file access way differently than regular browsers, especially with file:// URLs. I hit the same thing switching from TWebBrowser - the embedded engine has much stricter security by default. Set up a local web server instead of loading files directly. Even Python’s http.server on localhost will fix this. You could also try configuring WebView2 environment options for local file access, but I haven’t tested that myself. Since external scripts work fine, it’s definitely a local file policy thing, not JavaScript execution.

WebView2 security policies are brutal for local development. Most workarounds just add complexity or create security holes.

I hit this same issue on a project that needed dynamic content with local assets. Instead of fighting browser restrictions, I built an automation workflow that handles everything.

The workflow watches your HTML files, processes JS dependencies, bundles it all, and serves it through proper HTTP to TEdgeBrowser. No security flags, no embedded servers, no manual resource juggling.

When your app needs content, automation picks up the request, grabs your local HTML and JS, processes them together, and delivers a clean response WebView2 accepts. You get proper error handling, logging, and zero CORS headaches.

Scales better too. As your project grows and needs more complex asset processing, the workflow handles it automatically instead of you writing more TEdgeBrowser hacks.

The Problem:

You’re experiencing an issue where JavaScript files included locally in an HTML file using <script src="myScript.js"></script> do not execute within the TEdgeBrowser component in your Delphi 10.4.2 project, despite working correctly in other browsers like Chrome and Edge. The JavaScript files themselves are valid, and there are no apparent errors in the browser’s DevTools. External JavaScript files load correctly, suggesting the issue is specifically related to local file access.

:thinking: Understanding the “Why” (The Root Cause):

The TEdgeBrowser component in Delphi uses WebView2, which employs strict security policies to prevent malicious scripts from running. By default, WebView2 restricts access to local files to prevent vulnerabilities. This is why your local JavaScript files aren’t executing while external ones are—external scripts are sourced from different domains and aren’t subject to the same local file access restrictions. Simply running Delphi as administrator doesn’t override these built-in security measures.

:gear: Step-by-Step Guide:

Step 1: Add Browser Arguments to Allow Local File Access:

This is the most straightforward solution. Before navigating to your HTML file in TEdgeBrowser, you need to add specific browser arguments that explicitly grant permission to access local files. This is done by setting the additionalBrowserArguments property of your TEdgeBrowser component. Add the following arguments:

additionalBrowserArguments := '--allow-file-access-from-files --disable-web-security';

Important Note: --disable-web-security should be used cautiously, especially in production environments. It significantly weakens security, making your application vulnerable to exploits. This should be considered primarily for development and testing purposes only. For production, explore alternative solutions like using a local web server or a different approach, such as embedding the JS directly in your HTML.

Step 2: Verify TEdgeBrowser Initialization:

Ensure that your TEdgeBrowser component is correctly initialized and that the Navigate method is called after setting the additionalBrowserArguments. Incorrect order can prevent the arguments from taking effect. A typical sequence might look like this (replace 'file:///path/to/your/html/file.html' with your actual file path):

procedure TForm1.Button1Click(Sender: TObject);
begin
  EdgeBrowser1.AdditionalBrowserArguments := '--allow-file-access-from-files --disable-web-security';
  EdgeBrowser1.Navigate('file:///path/to/your/html/file.html');
end;

Step 3 (If Step 1 and 2 Fail): Consider Alternative Solutions:

If adding browser arguments doesn’t solve the problem, you might need to consider more robust but less convenient solutions: Serving your HTML and JavaScript files via a local HTTP server or embedding the JavaScript directly within your HTML page.

:mag: Common Pitfalls & What to Check Next:

  • Incorrect File Path: Double-check the path to your HTML and JavaScript files. WebView2 is very sensitive to exact paths. Use absolute paths when testing.
  • Typographical Errors: Carefully review the additionalBrowserArguments string for typos. Any errors will render the argument invalid.
  • Browser Version: Ensure you have a recent version of WebView2 installed. Outdated versions might not support these arguments.
  • Delphi Update: Make sure your Delphi 10.4.2 is up-to-date to benefit from the latest fixes and updates.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

Been there with TEdgeBrowser. Security restrictions are a nightmare for local files.

Yeah, you could set up a local server like others suggested, but that’s just more complexity. What happens when users install your app? You’re bundling a server, dealing with port conflicts, managing startup sequences.

I hit this same wall with a desktop app that needed interactive reports. Instead of fighting browser security policies, I switched to workflow automation.

Now when the app displays content, it processes the HTML and JavaScript through web automation, serves it properly, and delivers it back to the embedded browser. No local file issues, no CORS headaches, no server management.

You can handle file processing, script injection, and content delivery through automated workflows. Plus you get way better error handling and logging than debugging TEdgeBrowser security policies.

Set up a workflow that grabs your local HTML files, processes them with JavaScript, and serves them properly to your TEdgeBrowser component. Much cleaner than fighting WebView2 permissions.

This topic was automatically closed 6 hours after the last reply. New replies are no longer allowed.