下面就是这个动作的完整源代码:
<html>
<head>
<title>behavior "Check Browser Brand"</title>
<meta http-equiv="Content-Type" content="text/html">
<script language="JavaScript">
// Your function goes here.
function checkBrowserBrand(netscapeURL,explorerURL) {
if (navigator.appName == "Netscape") {
if (netscapeURL) location.href = netscapeURL;
}else if (navigator.appName == "Microsoft Internet Explorer")
{
if (explorerURL) location.href = explorerURL;
}
}
//******************* API **********************
function canAcceptBehavior(){
return true;
}
function behaviorFunction(){
// Return the name of your function here.
return "checkBrowserBrand";
}
function applyBehavior() {
var nsURL = escape(document.theForm.nsURL.value);
var ieURL = escape(document.theForm.ieURL.value);
if (nsURL && ieURL) {
return "checkBrowserBrand(\'" + nsURL + "\',\'"
+ ieURL + "\')";
}else{
return "Please enter URLs in both fields."
}
}
function inspectBehavior(fnCall){
var argArray = getTokens(fnCall, "()',");
var nsURL = unescape(argArray[1]);
var ieURL = unescape(argArray[2]);
document.theForm.nsURL.value = nsURL;
document.theForm.ieURL.value = ieURL;
}
//***************** LOCAL FUNCTIONS ******************
function initializeUI(){
// Put the cursor in the first text field.
document.theForm.nsURL.focus();
document.theForm.nsURL.select();
}
//*************** END OF JAVASCRIPT *****************
</script>
</head>
<body>
<form method="post" action="" name="theForm">
Go to this URL if the browser is Netscape Navigator:
<input type="text" name="nsURL" size="50"><br>
Go to this URL is the browser is Microsoft Internet Explorer:
<input type="text" name="ieURL" size="50">
</form>
</body>
</html>
摘自《HTML Point》 Weber/文