VB如何读取网页变量的值,求高手
WebBrowser1.document.getElementById("name").innerTextVB通过WebBrowser1 ,调取JS变量"name"等值并显示在网页文本框中,
现在的问题是:VB如何将这些值读出来,比如读取到控件TEXT1中
[img=http://b301.photo.store.qq.com/psb?/48e7b5cb-d5bc-43cb-8e70-b638d3404d83/QID9CHfZ9WSpN6qhXdUBMxepRKfm6roq9ZjeWepBFAY!/b/YVupc7PVBgAAYmyyc7PlBgAA][/img] 方法还是有的。
<!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 href="common/skins/default/common.css" rel="stylesheet" type="text/css">
<script language="javascript" src="common/js/prototype.js"></script>
<script language="javascript" src="common/js/common.js"></script>
<script language="javascript" src="common/js/validator.js"></script>
<script language="javascript" src="citizen/js/citizen.js"></script>
<script language="javascript" src="disabled/js/r_oper.js"></script>
<script language="javascript" src="differ/js/differ.js"></script>
<script>
var _photoid='';
</script>
</head>
<body>
<div style="text-align:left;padding-left:20px;"><span id="citizenImgSpan" style="POSITION: absolute"></span></div>
<div style="text-align:left;padding-left:20px;align:center;"><span id="differImgSpan" style="POSITION: absolute"></span></div>
<center>
<div style="padding-top:10px;">
<table border="0" cellspacing="0" cellpadding="0" id=label>
<tr>
<td width="10"><img src="common/skins/default/images/label_left.gif"></td>
<td width="90" align=left nowrap background="common/skins/default/images/label_bg.gif" >基本信息</td>
<td align=left><img src="common/skins/default/images/label_right.gif"></td>
</tr>
</table>
<form name="form2" id="form2" method="post" onSubmit="loadService();return false;">
<input type="submit" name="conSubmit" style="position:absolute;top:-10000px;left:-10000px;index:0" />
<div id=formTable>
<table >
<tr>
<td>
姓名:<input class="namefield" style="font-family:方正宋体-人口信息;" name="cname" id="cname" datatype="Require" msg="请输入姓名" size=10 maxlength=20 />
身份证号:<input name="cid" id="cid" size=20 maxlength=18 datatype="IdCard" msg="身份证号码格式错误" />
<span id=submitId>
<input class="cssBtn" type="button" onclick="javascript:loadService();" value="调取易做图部人口数据" name="submitCheck" title="你可以在输入姓名和身份证号码后按“调取易做图部人口数据”来获得该申请人的基本信息" />
<input id="clrBtn" class="cssBtn" type="button" onclick="javascript:frmReset();" value="清空" name="submitCheck" />
<input id="addBtn" class="cssBtn" type="button" onclick="javascript:save_citz();" value="保存" name="submitCheck" />
</span>
</td>
</tr>
</table>
</div>
</form>
<form name="form1" id="form1" method="post">
<input type="hidden" name="uuid" id="uuid">
<input type=hidden name="tSessionId" id="tSessionId">
<input type=hidden name="postAjax" id="postAjax" value="true">
<div id=formTable>
<table id=cdpf_citizen>
<tr>
<td colspan=8 class=clsth style="text-align:center;height:20px">中华人民共和国残疾人证申请表</td>
</tr>
<tr>
<th width="10%" rowspan="5" style="text-align:center;"> 申 <br />
请 <br />
人 <br />
基 <br />
本 <br />
情 <br />
况 </th>
<th width="10%"><span class="nNull">*</span>姓名 </th>
<td><input class="namefield" name="name" type="text" id="name" disabled="disabled" /></td>
<th width="10%"><span class="nNull">*</span>性别 </th>
<td>
<span class="errorLabel">
</span>
<select name="gender"
disabled="disabled" id="gender" >
<option value="" >请选择</option>
<option value="1" >男</option>
<option value="2" >女</option>
</select>
</form>
</center>
</body>
</html>
建立c:\test.htm 内容为:
<script>var i=18;<script>
vb中读取这里的i的值到text1中:
Private Sub Form_Load()WebBrowser中获得脚本中的变量值
WebBrowser1.Navigate "c:\test.htm"
End Sub
Private Sub Command1_Click()
WebBrowser1.Document.parentWindow.execscript "location.replace('mytag='+i)"
End Sub
Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)
If InStr(URL, "mytag") > 0 Then
Text1.Text = Mid(URL, InStr(URL, "=") + 1)
Cancel = True
End If
End Sub
//项目中添加Micrsoft.mshtml引用
--c:\temp\temp.htm--
<html>
<script language="JavaScript">
var Temp = "Zswang 路过 :)";
</script>
</html>
using mshtml;
Private Sub Form_Load()
webBrowser1.Navigate(@"c:\temp\temp.htm");
End Sub
Private Sub button1_Click()
IHTMLDocument2 vDocument = webBrowser1.Document.DomDocument as IHTMLDocument2;
IHTMLWindow2 vWindow = vDocument.parentWindow;
vWindow.execScript("document.title=Temp;", "JavaScript");
Text = vDocument.title;
End Sub 是个办法,不过可以直接设置当前的title,最多修改后再恢复过来。本质是通过dhtml中的变量做为交换。
可是总觉得有办法可以直接访问到,研究了下方向确实可以的。相当easy,一句话即可:
Private Sub Command1_Click()
Text1.Text = WebBrowser1.Document.script.i
End Sub
补充:VB , 网络编程