覆盖Webbrowser控件上的window.location [英] Overriding window.location on Webbrowser Control

查看:174
本文介绍了覆盖Webbrowser控件上的window.location的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个奇怪的问题,但是有没有一种方法可以覆盖window.location而不使浏览器控件导航到它呢?我遇到的问题是我在控件中注入了html代码,并且window.location是about:blank-在某些javascript代码上引起错误.试图让他们认为自己不在URL中:about

This might seem a weird question, but is there a way to override the window.location without making the browser control navigate to it? The problem I am having is that i am injecting html code in the control, and the window.location is about:blank - causing errors on some javascript code. Trying to make them think they are in a URL other than about:blank

推荐答案

document.documentMode >= 9(IE9 +)上,我已经成功使用 Object.defineProperty().

I've had success with Object.defineProperty() when document.documentMode >= 9 (IE9+).

要以IE9 +模式获得WebBrowser控件,我首先加载以下URL:

To get the WebBrowser control in IE9+ mode, I first load the following URL:

about:<!doctype html><meta http-equiv="X-UA-Compatible" content="IE=edge">

这将在最新文档模式下提供一个大部分为空的文档.

This gives a mostly empty document in the latest document mode.

然后重新定义window.location,我通过eval()或document.write()执行一些脚本:

Then to redefine window.location, I execute some script via eval() or document.write():

Object.defineProperty(window, 'location', {value: {href: 'fubar'}});

完整的序列是这样的:

  • 加载控件.
  • 等待WebBrowser.ReadyState == 4DocumentComplete事件.
  • 致电document.open()(重要).
  • 评估或编写重新定义位置的脚本.
  • 编写HTML内容.
  • 调用document.close()(确保调用onload).
  • Load the control.
  • Wait for WebBrowser.ReadyState == 4 or the DocumentComplete event.
  • Call document.open() (important).
  • Eval or write the script redefining location.
  • Write the HTML content.
  • Call document.close() (ensures onload gets called).

注意:我使用ActiveX WebBrowser 控件,而不是.NET组件,它是该控件的包装器.在.NET中,它可能会工作相同.

Note: I use the ActiveX WebBrowser control, and not the .NET component which is a wrapper around that control. It will probably work the same in .NET.

这篇关于覆盖Webbrowser控件上的window.location的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆