直接調用JavaScript
webBrowser1.Document.InvokeScript("FunctionName");
調用帶有參數的JavaScript
webBrowser1.Document.InvokeScript("FunctionName",new object[]{obj1});
向webBrowser1中添加新的javascript,將javascript放在txt中,代碼容易寫
TXT內容:
function SubmitUser()
{
document.Login.USER.value = 'USER';
document.Login.PASSWORD.value = 'PASSWORD';
document.Login.target.value = 'https://www.myprs.com';
document.Login.submit();
}
調用代碼
HtmlDocument document = webBrowser1.Document;
HtmlElement htmlElement = document.CreateElement("Script");
htmlElement.SetAttribute("Language", "JavaScript");
FileStream fs = new FileStream(strFile, FileMode.Open);
StreamReader sr = new StreamReader(fs);
string strjs = sr.ReadToEnd();
htmlElement.SetAttribute("text", strjs);
document.Body.AppendChild(htmlElement);
object ss = document.InvokeScript(FunctionName);
sr.Close();
fs.Close();
return ss;
有復雜參數的,可以新建Function調用注意FunctionName大小寫問題
<script language="javascript">
var fun = function()
{
.......
}
onLoadFunctionStack(window, fun);
</script>
<script language="javascript">
function CALLFUN() {
var fun = function()
{
}
onLoadFunctionStack(window, fun);
}
</script>
將復雜參數在Function中利用Javascript代碼直接賦值
<form name="QueryForm" method="post" action="/myprs/internal/myprsQuery.do;jsessionid=4CB44026578AC5443249606A945259D5.g1u0117c"
onsubmit="return validateForm(this) && onSubmitBasket(this) && displayPnLimitWarningNoParams();">
調用onsubmit中的這段代碼 (this代表這個form)可以改寫成
function reValue()
{
var frms=document.forms["QueryForm"];
return validateForm(frms) && onSubmitBasket(frms) && displayPnLimitWarningNoParams();
}
Javascript中document很有用
