简体   繁体   English

WebBrowser链接项目资源中的JavaScript文件

[英]WebBrowser link JavaScript file from Project Resources

I have index.html and script.js in my project resources. 我的项目资源中有index.htmlscript.js And in html file, I try to link script script.js with it: 在html文件中,我尝试将脚本script.js与它链接:

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

Also I have Form which have a WebBrowser control and its url is index.html . 我还有一个具有WebBrowser控件的Form ,其url是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. 问题是当我测试应用程序并运行WebBrowser时 ,它给我一个脚本错误,这意味着没有文件名script.js ,并且无法与它链接。

What I should type here instead of ???? 我应该在这里输入什么而不是???? ?? ??

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

Here's the error: 这是错误: 脚本错误

You can use either of following options: 您可以使用以下任一选项:

  • Include the js file content in the same html file. 在同一个html文件中包含js文件内容。
  • Copy both html and js file into the same directory at run-time, for example a temp directory. 在运行时将html和js文件复制到同一目录中,例如临时目录。

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);
}

Okay, since you requested it, the basic idea is to read your JS file into a string, then create a script tag element and then insert it on the body. 好的,既然你要求它,基本的想法是将你的JS文件读成一个字符串,然后创建一个脚本标记元素,然后将其插入到正文中。 Also remember to set the JS file to Copy to Output Directory from the properties window if you are using visual studio. 还记得如果使用visual studio,则从属性窗口将JS文件设置为Copy to Output Directory

You have a JS file that looks like this: 你有一个JS文件,如下所示:

alert("Include me");

You have CS file that looks like this: 你有CS文件,如下所示:

using System.Windows.Forms;
using System.IO;
namespace stackoverflow
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            var path = Path.Combine(System.Environment.CurrentDirectory, "test.html");
            var page = System.IO.File.ReadAllText(path);
            webBrowser1.Navigate(path);
            webBrowser1.DocumentCompleted += WebBrowser1_DocumentCompleted;
        }
        private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            var newScript = webBrowser1.Document.CreateElement("script");
            webBrowser1.Document.Body.InsertAdjacentElement(HtmlElementInsertionOrientation.AfterEnd, newScript);
            var path =Path.Combine(System.Environment.CurrentDirectory, "test.js");
            var script =File.ReadAllText(path);
            newScript.InnerText = script;
        }
    }
}

I used an HTML file that looks like this: 我使用了一个看起来像这样的HTML文件:

<!doctype html>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>Test</title>
    </head>
    <body>
        <div id="container">

          <h1>Test Text</h1>
        </div><!-- content container -->

        <script>
        </script>
    </body>
</html>

When I did this, I ended up with a result that looks like this: 当我这样做时,我得到了一个看起来像这样的结果: 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM