代码保留 IE8中如何添加Activity

来源:岁月联盟 编辑:zhuzhu 时间:2008-04-08
IE8中添加Activity的JavaScript脚本(这是一个来自淘宝网的例子)、

<script type="text/javascript"> function addActivity() { if (navigator.userAgent.indexOf('MSIE 8') == -1) { alert('请首先安装IE8.'); } else { window.external.addService('activities/taobao-search.xml'); } } </script>

相关XML描述文件:http://ie8.taobao.com/activities/taobao-search.xml
<?xml version="1.0" encoding="UTF-8" ?> <openServiceDescription xmlns="http://www.microsoft.com/schemas/openservicedescription/1.0"> <homepageUrl>http://www.taobao.com</homepageUrl> <display> <name>淘宝搜索更多细节请查看活动白皮书。

写了一个简单的Google搜索的Activity XML:

<?xml version="1.0" encoding="UTF-8" ?> <openServiceDescription xmlns="http://www.microsoft.com/schemas/openservicedescription/1.0"> <homepageUrl>http://www.google.com</homepageUrl> <display> <name>Google Search </name> <icon>http://www.google.com/favicon.ico</icon> </display> <activity category="Find"> - <activityAction context="selection"> <preview action="http://www.google.com/search?q={selection}" /> <execute method="get" action="http://www.google.com/search?q={selection}" /> </activityAction> </activity> </openServiceDescription>

其中有句用于描述homepageUrl节点的说明:The main URL of the Activity where the user can access the service through browsing. All URLs expressed in this format must match the domain of the homepageUrl.也就是后续的所有的URLs都必须与主页Url在相同的域中。比如上例中就必须都是google.com域中。

openServiceDescription是专门用于Activity的xml节点名。

形如selection的位置,若在尾部加“?”则表示若该标记为空,将以空格代替。如: <execute method="get" action="http://example.com/done.aspx?url={documentUrl}&title={documentTitle?}" />

其中documentUrl是必选项,而documentTitle因为带了“?”,所以是可选项。

另外以下几种简单的标记在“预览”窗体中是不会有效的。
· <select>
· <input type="text">
· <text area>


关于脚本:

//JavaScript API for Activities
//检查服务是否已经安装了
//参数:Service XML,name
window.external.IsServiceInstalled("http://maps.live.com/GetMap.xml", "map");
//添加服务
//参数:Service XML
window.external.AddService("http://maps.live.com/GetMap.xml");

下面是白皮书中所给出的示例,相对元素更齐全一些:
<?xml version="1.0" encoding="UTF-8"?> <openServiceDescription xmlns="http://www.microsoft.com/schemas/openservicedescription/1.0"> <homepageUrl>http://maps.live.com</homepageUrl> <display> <name>Map with Live Maps</name> <icon>http://www.live.com/favicon.ico</icon> </display> <activity category="map"> <activityAction context="selection" > <preview action=" http://maps.live.com/geotager.aspx"> <parameter name="b" value="{selection}" /> <parameter name="clean" value="true" /> <parameter name="w" value="320" /> <parameter name="h" value="240" /> <parameter name="format" value="full" /> </preview> <execute action=" http://maps.live.com/default.aspx"> <parameter name="where1" value="{selection}" type="text" /> </execute> </activityAction> </activity> </openServiceDescription>

试试下面这个功能吧:(选中之后是利用“博客园”的博问搜索进行搜索……)

下面是上面这个按钮的代码: <div> <script type="text/javascript"> function addActivity(xml) { if (navigator.userAgent.indexOf('MSIE 8') == -1) { alert('请首先安装IE8.'); } else { window.external.addService(xml); } } </script>

试试下面这个功能吧:(选中之后是利用“博客园”的博问搜索进行搜索……) <div> <div><input onclick="addActivity('http://www.cnblogs.com/Files/volnet/cnblogsactivity.xml');" value="将博客园添加到IE8的活动中" type="button"> </div> <?xml version="1.0" encoding="UTF-8" ?> <openServiceDescription xmlns="http://www.microsoft.com/schemas/openservicedescription/1.0"> <homepageUrl>http://www.cnblogs.com添加后若不喜欢,可以在IE8的“管理活动”中进行移除。