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:
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.
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.
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.
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).
You can use either of following options:
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With