Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebBrowser link JavaScript file from Project Resources

I have index.html and script.js in my project resources. And in html file, I try to link script script.js with it:

<script src="script.js"></script>

Also I have Form which have a WebBrowser control and its url is index.html. And no problem here.

The problem is when I test the application and run WebBrowser, it give me a script error which it's mean there's no file name script.js, and cannot link with it.

What I should type here instead of ???? ??

<script src="????/script.js"></script>

Here's the error: Script error

like image 539
Shady Boshra Avatar asked Feb 24 '18 13:02

Shady Boshra


People also ask

How do I link a JavaScript file?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

Can we run .JS file in browser?

The browser's built-in interpreter searches for <script> tag or . js file linked with HTML file while loading a web page, and then interpretation and execution starts. Example: In this approach, we have written JavaScript code within the HTML file itself by using the <script> tag.

How do I read a JavaScript file from a website?

For most browsers, to view inline JavaScript in the HTML source code, do one of the following. Press the Ctrl + U keyboard shortcut. Right-click an empty area on the web page and select the View page source or similar option in the pop-up menu.

How do I run a JavaScript file locally?

Running a JS program from the command line is handled by NodeJS. Start by installing NodeJS on local machine if necessary. Now simply open the command line in the same directory as the index. js script you created (VS Code will do this automatically with the integrated terminal).


1 Answers

You can use either of following options:

  • Include the js file content in the same html file.
  • Copy both html and js file into the same directory at run-time, for example a temp directory.

Example

private void Form1_Load(object sender, EventArgs e)
{
    var path = System.IO.Path.GetTempFileName();
    System.IO.File.Delete(path);
    System.IO.Directory.CreateDirectory(path);
    var indexPath = System.IO.Path.Combine(path, "index.html");
    var scriptPath = System.IO.Path.Combine(path, "script.js");
    System.IO.File.WriteAllText(indexPath, Properties.Resources.index);
    System.IO.File.WriteAllText(scriptPath, Properties.Resources.script);
    webBrowser1.Navigate(indexPath);
}
like image 71
Reza Aghaei Avatar answered Oct 08 '22 15:10

Reza Aghaei