gpt4 book ai didi

.net - 浏览器控件处理 javascript : popup

转载 作者:行者123 更新时间:2023-12-04 04:41:29 25 4
gpt4 key购买 nike

我的软件通过这个函数处理导航到 target=_new

Private Sub Web1_NewWindow(...) Handles Web1.NewWindow
Web1.Navigate(Web1.StatusText)
e.Cancel = True
End Sub

这允许我在同一个 webbrowser 控件中打开任何新窗口。但是,当导航到创建新弹出窗口的“javascript:”链接时,我收到以下消息,因为它正在尝试导航到该页面:
The requested resource is in use. (Exception from HRESULT: 0x800700AA)

如何让它在 webbrowser 控件中打开一个 javascript 弹出窗口?

该链接如下所示:
javascript:Dpy.ITQPopup('100',255,'2932 NTYwNDUwMTA0MDYzMDM);3094 V0FZ','-357933312',0,0)

最佳答案

取决于 javascript: 里面到底是什么链接,它可能有效,也可能无效。例如:

<body>

<!-- These work -->

<a href="http://www.example.com" target="_blank">Test 0</a><br>
<a href="javascript:navigate('http://www.example.com')" target="_blank">Test 1</a><br>
<a href="javascript:'<b>hello</b> from new page!'" target="_blank">Test 2</a><br>
<a href="javascript:''" target="_blank">Test 3</a><br>
<a href="javascript:TestFunc()" target="_blank">Test 4</a><br>

<!-- This does not work -->

<a href="javascript:open('http://www.example.com')" target="_blank">Test 5</a><br>


<script>
function TestFunc()
{
return "<b>TestFunc</b> result.";
}
</script>

</body>

这是处理 NewWindow 的代码事件。这段代码是用 C# 编写的,不幸的是我不知道 VB.NET 足够好,无法正确翻译它。
private void Form1_Load(object sender, EventArgs e)
{
// this code depends on SHDocVw.dll COM interop assembly,
// generate SHDocVw.dll: "tlbimp.exe ieframe.dll",
// and add as a reference to the project

var activex = (SHDocVw.WebBrowser_V1)this.webBrowser.ActiveXInstance;

activex.NewWindow += delegate(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
{
Processed = true;
object flags = Flags;
object targetFrame = Type.Missing;
object postData = PostData != null ? PostData : Type.Missing;
object headers = !String.IsNullOrEmpty(Headers)? Headers.ToString() : Type.Missing;
activex.Navigate(URL, ref flags, ref targetFrame, ref postData, ref headers);
};

this.webBrowser.Navigate("http://localhost:81/new-window-test.html");
}

现在,如果你真的需要制作 测试 5 工作,还是有可能的。它的问题是实际上有两个导航,第二个是嵌套的,这可能是错误 0x800700AA 的原因。 .诀窍是从 NewWindow 返回先事件,然后做导航:
private void Form1_Load(object sender, EventArgs e)
{
// this code depends on SHDocVw.dll COM interop assembly,
// generate SHDocVw.dll: "tlbimp.exe ieframe.dll",
// and add as a reference to the project

var activex = (SHDocVw.WebBrowser_V1)this.webBrowser.ActiveXInstance;

activex.NewWindow += delegate(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
{
Processed = true;
object flags = Flags;
object targetFrame = Type.Missing;
object postData = PostData != null ? PostData : Type.Missing;
object headers = !String.IsNullOrEmpty(Headers) ? Headers.ToString() : Type.Missing;
SynchronizationContext.Current.Post(delegate
{
activex.Navigate(URL, ref flags, ref targetFrame, ref postData, ref headers);
}, null);
};

this.webBrowser.Navigate("http://localhost:81/new-window-test.html");
}

用IE10测试。

[更新]

试试这个(已验证):
Public Class Form1
Dim ActiveX As SHDocVw.WebBrowser_V1

Private Sub NavigateOnNewWindow(NewWindowUrl As Object)
Me.ActiveX.Navigate(NewWindowUrl.ToString())
End Sub

Private Sub NewWindow(URL As String,
Flags As Integer,
TargetFrameName As String,
ByRef PostData As Object,
Headers As String,
ByRef Processed As Boolean)
Processed = True
System.Threading.SynchronizationContext.Current.Post(AddressOf NavigateOnNewWindow, URL)
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Init DocumentText, otherwise Me.WebBrowser1.ActiveXInstance is null, this is different from C#
Me.WebBrowser1.DocumentText = String.Empty
Me.ActiveX = Me.WebBrowser1.ActiveXInstance
AddHandler Me.ActiveX.NewWindow, AddressOf NewWindow
Me.WebBrowser1.Navigate("http://localhost:81/new-window-test.html")
End Sub
End Class

关于.net - 浏览器控件处理 javascript : popup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18817513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com