在WinForms WebBrowser控件中使用LocalStorage - 更改事件未收到

时间:2015-06-15 08:54:14

标签: winforms local-storage

我正在尝试在Web浏览器控件中使用HTML代码中的LocalStorage,托管在WinForms应用程序中。但是,从未收到LocalStorage更改事件。我设法更改注册表并强制控件模拟IE10。

C#代码:

//Load the html source into the browser control
webBrowser1.Url = new Uri  ("http://localhost/YaronLocalStorageTest/myHtm.htm");

加载的Html:

  <!DOCTYPE html>
  <html lang="en">
      <head>
          <meta charset="utf-8"/>
          <meta name="viewport" content="width=620"/>
          <title>HTML5 Demo: Storage Events</title>

      </head>
      <body>
        <div>
            <p>
              <label for="data">Your test data:</label> 
                           <input type="text" name="data" value="" placeholder="change me" id="data" /> 
            </p>
            <p id="fromEvent">Waiting for data via<code>storage</code>event...
            </p>
       </div>
        <SCRIPT type="text/javascript">
        var addEvent = (function () {
      if (document.addEventListener) {
        return function (el, type, fn) {
          if (el && el.nodeName || el === window) {
            el.addEventListener(type, fn, false);
          } else if (el && el.length) {
            for (var i = 0; i < el.length; i++) {
              addEvent(el[i], type, fn);
            }
          }
        };
      } else {
        return function (el, type, fn) {
          if (el && el.nodeName || el === window) {
            el.attachEvent('on' + type, function () { return     fn.call(el, window.event); });
          } else if (el && el.length) {
            for (var i = 0; i < el.length; i++) {
              addEvent(el[i], type, fn);
            }
          }
        };
      }
    })();
</SCRIPT>
<script>
    var dataInput = document.getElementById('data'), output = document
            .getElementById('fromEvent');
    addEvent(window, 'storage', function(event) {
        alert('change notification');
        if (event.key == 'storage-event-test') {
            output.innerHTML = event.newValue;
        }
    });
    addEvent(dataInput, 'keyup', function() {
        localStorage.setItem('storage-event-test', this.value);
    });
</script>
   </body>
   </html>  

0 个答案:

没有答案