【问题标题】:WebBrowser link JavaScript file from Project Resources来自项目资源的 WebBrowser 链接 JavaScript 文件
【发布时间】:2018-08-04 10:29:03
【问题描述】:

我的项目资源中有 index.htmlscript.js。在 html 文件中,我尝试将脚本 script.js 与它链接:

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

我还有 Form,它有一个 WebBrowser 控件,它的 url 是 index.html。这里没问题。

问题是当我测试应用程序并运行 WebBrowser 时,它给了我一个脚本错误,这意味着没有文件名 script.js,并且无法链接它。

我应该在这里输入什么而不是 ???? ??

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

这是错误:

【问题讨论】:

  • 需要将js文件包含到输出目录中。
  • 这正是我不想做的。
  • 所以将它包含在html文件中。
  • 作为另一种选择,您可以将这两个文件复制到 runtiem 的临时目录并从临时路径加载它们。
  • 我不想为此写一个答案,但另一种方法是简单地将您的 JS 文件作为字符串读入内存,然后通过标记名获取脚本标记,然后有效地执行 + = 在 InnerText 属性上并将您的 JS 文件附加为字符串。

标签: javascript c# html winforms webbrowser-control


【解决方案1】:

好吧,既然你要求了,基本思路就是把你的JS文件读成一个字符串,然后创建一个script标签元素,然后插入到body上。如果您使用的是 Visual Studio,请记住在属性窗口中将 JS 文件设置为 Copy to Output Directory

你有一个如下所示的 JS 文件:

alert("Include me");

您的 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;
        }
    }
}

我使用的 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>

当我这样做时,我最终得到了如下所示的结果:

【讨论】:

    【解决方案2】:

    您可以使用以下任一选项:

    • 将js文件内容包含在同一个html文件中。
    • 在运行时将 html 和 js 文件复制到同一目录中,例如临时目录。

    示例

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

    【讨论】:

      猜你喜欢
      • 2013-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多