一、window.external是什么?
window.external
是JavaScript与外部应用程序交互的接口。外部应用程序可以向该接口暴露自己的方法和属性,以便JavaScript可以调用,反之也可以。
下面是一个简单的应用示例:
// C#应用程序中暴露的方法
[ComVisible(true)]
public class TestClass
{
public void TestMethod(string message)
{
MessageBox.Show(message);
}
}
<!-- HTML页面中的JavaScript代码 -->
<script type="text/javascript">
var testObj = window.external;
testObj.TestMethod("Hello World!");
</script>
二、window.external的使用场景
window.external
主要用于JavaScript与外部应用程序的交互。例如,在浏览器中嵌入应用程序时,可以使用window.external
将浏览器中的JavaScript代码与应用程序进行交互。这种方式可以方便地实现网页与桌面应用程序之间的数据共享、消息传递等功能。
下面是一个在微软Word中嵌入HTML页面的示例:
// C#应用程序中的代码
private void button1_Click(object sender, EventArgs e)
{
object missing = Type.Missing;
object fileName = @"D:\test.docx";
Word.Application WordApp = new Word.Application();
Word.Document doc = WordApp.Documents.Open(ref fileName,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
doc.ActiveWindow.Selection.TypeText("This is a test.");
doc.ActiveWindow.Selection.TypeParagraph();
object range = doc.Range(doc.Content.End - 1, doc.Content.End);
string html = "<html><body><p>Hello World!</p></body></html>";
range = html;
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();
string htmlFormat = data.GetData("HTML Format") as string;
Clipboard.Clear();
doc.Close();
WordApp.Quit();
webBrowser1.DocumentText = htmlFormat;
}
<!-- HTML页面中的JavaScript代码 -->
<script type="text/javascript">
function showMessage() {
var message = window.external.GetMessage();
alert(message);
}
</script>
三、window.external方法和属性
1. 方法
外部应用程序可以通过调用window.external
的方法来向JavaScript发送消息或者获取JavaScript中的值。
以下是window.external
提供的常用方法:
Alert(message)
:弹出消息框并显示message。Confirm(message)
:弹出确认框并显示message,返回值为布尔值。Prompt(message, [defaultValue])
:弹出输入框并显示message,defaultValue
为可选参数,默认为空字符串。GetVariable(variableName)
:获取JavaScript中变量的值。SetVariable(variableName, variableValue)
:设置JavaScript中变量的值。 下面是一个示例,向JavaScript传递消息并弹出提示框:
// C#应用程序中的代码
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.ObjectForScripting = this;
webBrowser1.Document.InvokeScript("showMessage");
}
public void ShowMessage(string message)
{
MessageBox.Show(message, "From JavaScript");
}
<!-- HTML页面中的JavaScript代码 -->
<script type="text/javascript">
function showMessage() {
window.external.ShowMessage("Hello from C#");
}
</script>
2. 属性
外部应用程序可以通过暴露自己的属性,以便JavaScript可以调用和修改。
以下是window.external
提供的属性:
Version
:外部应用程序的版本。Name
:外部应用程序的名称。IsReady
:表示外部应用程序是否已经准备就绪。 下面是一个示例,向JavaScript传递数据并修改外部应用程序的属性:
// C#应用程序中的代码
[ComVisible(true)]
public class TestClass
{
public string Message { get; set; }
public int Count { get; set; }
}
<!-- HTML页面中的JavaScript代码 -->
<script type="text/javascript">
var testObj = window.external;
testObj.Message = "Hello world!";
testObj.Count = 100;
alert(testObj.Message + " - " + testObj.Count);
</script>
四、window.external的安全性
由于window.external
的能力很强,因此在使用时需要格外注意其安全性。
其中一项常见的安全措施是,在外部应用程序中对JavaScript传递的参数进行类型和长度的校验,以防止传递恶意代码。
另外,在JS中调用window.external
的方法时,需要先判断window.external
是否存在。如果不存在,说明应用程序未正常运行,此时应给出相应的提示。
五、总结
以上是关于window.external
的详细介绍。window.external
可以方便地实现JavaScript与外部应用程序的交互,但使用时需要格外注意安全性。