写了一个简单的管理代码的软件,C/S的,发现webbrowser内嵌的HTML都不能运行JS代码了,但单独在页面上运行是正常的,在IE里面又能正常运行JS..大家知道原因的请告知一下,多谢了.代码: wbShow.Document.Body.InnerHtml = 代码;哎,弄了很久都不行。原因是不运行JS代码呀,很雷人。有知道怎么解决的吗,小弟非常感谢。
winformhtml

解决方案 »

  1.   

    自己以前用过的,查看webbrowser页面源文件的代码。      // 弹出网页源文件
          System.Windows.Forms.Form windowBrowserSource = new System.Windows.Forms.Form();
          System.Windows.Forms.RichTextBox browserSourceText = new System.Windows.Forms.RichTextBox();
          browserSourceText.Margin = new System.Windows.Forms.Padding(0);
          browserSourceText.Dock = System.Windows.Forms.DockStyle.Fill;
          browserSourceText.Text = wbShow.DocumentText;
          windowBrowserSource.Controls.Add(browserSourceText);
          windowBrowserSource.Show();看看与IE的查看源文件有什么不一样的么?
    编码是否一致,是否有乱码?
    有些时候标签未闭合也可能会出现一些错误,貌似IE有自动纠正功能,webbrowser没有。
      

  2.   

    是的,JS是在单独是文件里,下面是这个文件的源码:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <link rel="stylesheet" href="plugins/code/prettify.css" />
        <script type="text/javascript" charset="utf-8" src="kindeditor.js"></script>
        <script type="text/javascript" charset="utf-8" src="zh_CN.js"></script>
        <script type="text/javascript" charset="utf-8" src="plugins/code/code.js"></script>
        <script type="text/javascript" charset="utf-8" src="plugins/code/prettify.js"></script>
        <script type="text/javascript">
            var editor;
            KindEditor.ready(function (K) {
                editor = K.create('textarea[name="content"]', {
                    cssPath: 'plugins/code/prettify.css',
                    allowFileManager: true,
                    fullscreenMode: true
                });
                prettyPrint();
            });
        </script>
    </head>
    <body></body>
    </html>
      

  3.   

    不知道是不是因为HTML是嵌入到exe里面去了,没有发送HTTP的请求,所以界面内嵌的HTML不执行本身的JS代码,哎,还有没有另外的方法呢?
      

  4.   

    很感谢大家的回答,最后问题算是解决了。因为我用的是KindEditor编辑器自带的代码高亮的方法,直接调用prettyPrint这个JS就行了。所以在webbrowser插入代码时,写多一句如下:
     
    wbShow.Document.Body.InnerHtml = sb.ToString();
    wbShow.Document.InvokeScript("prettyPrint"); 意思是重新加载这个方法,就可以实现内嵌HTML时再加载自己要加载的JS。不过,如果是引用JS文件的话,我没有好的办法。用SyntaxHighlighter着色的话,SyntaxHighlighter.all();这个方法调用不成功,写法如下:
     
      function initMenu() {
                SyntaxHighlighter.config.clipboardSwf = 'plugins/code/clipboard.swf';
                SyntaxHighlighter.all();
                alert("wq");
            } code=csharp]wbShow.Document.InvokeScript("initMenu");[[/code]
    JS是执行了,最后也弹出wq,但就是不着色。单独出来是着色的,估计是.all这样写是不行的。谢谢大家!