|
20金钱
Winform项目,这是我写的一段代码,在后台用C#执行JS,引入了Jquery,执行时报错:window未定义。
因为引用了Jquery,所以跟飞哥写的那个Demo有些区别
private void JSTest_Load(object sender, EventArgs e)
{
StringBuilder strsql = new StringBuilder();
strsql.AppendLine(" function CheckIsEmpty(str) {");
strsql.AppendLine(" if ($.trim(str).length == 0) {");
strsql.AppendLine(" alert(\"您输入的字符串为空!\");");
strsql.AppendLine(" }");
strsql.AppendLine(" }");
strsql.AppendLine("");
string scriptStr = strsql.ToString();
object[] paras = new object[] { "magic" };
string result = ExecuteScript(scriptStr, "CheckIsEmpty", paras);
MessageBox.Show(result);
}
public static string ExecuteScript(string jsString, string funName, object[] paras)
{
try
{
//加载Jquery1.7.1.js文件
StreamReader objReader = new StreamReader("c:\\jquery-1.7.1.js");
string JqueryString = objReader.ReadToEnd();
objReader.Close();
jsString = JqueryString + jsString;
MSScriptControl.ScriptControl scriptControl = new MSScriptControl.ScriptControl();
scriptControl.UseSafeSubset = true;
scriptControl.Language = "JScript";
scriptControl.AddCode(jsString);
string str = scriptControl.Run(funName, paras).ToString();
return str;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return "出现错误";
}
|
|