自编JQuery插件第十一个

按回车弹出新窗口,实现简单的点击搜索关键词按钮的效果 JQPI_02.js:

$(function(){
     $(".text").openSearch({fileName:"index.html",queryName:"key",cache:true});
});
function getEvent(){ 
 //ie ff
 if(document.all) return window.event;
	 func=getEvent.caller;
	 while(func!=null){
		 var arg0=func.arguments[0];
		 if(arg0){
			 if((arg0.constructor==Event || arg0.constructor ==MouseEvent) || (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation)){ return arg0; }
		 }
		 func=func.caller;
	 }
 return null;
}	
	
(function ($) {   
$.fn.openSearch = function(options){  
  var defaults = {  
      fileName:"",   //XXX.aspx
      queryName:"",  //取关键词或取所有参数,值可为字符串(即关键词所在的参数名称)或函数 例子 : key=kate&type=1 或 getUrl()
      target:"_blank" ,
      cache:false
  }  
  var o = $.extend(defaults, options);  
  $(this).keydown(function(){  
        var evt = getEvent();  
        var keyCode = evt.keyCode ? evt.keyCode : evt.charCode;  
        if(parseInt(keyCode) == 13)  
        {  
            var word = $(this).val();if(word.indexOf('-')!=-1)return; 
            var separator=""; 
            if(o.fileName.indexOf('(')!=-1){separator=eval(o.fileName);}else{separator=o.fileName;}
            if(separator.indexOf('?')!=-1){separator+="&";}else{separator+="?"; }
            if(o.queryName.indexOf('(')!=-1){separator+=eval(o.queryName);}else if(o.queryName.length>0) { separator+=o.queryName+"="+encodeURI(word);}
            if(word!="" && word!=null){  
                if($.browser.msie) {
                    if(!o.cache)document.body.innerHTML='';
                    var aid=new Date().getTime();
                    $("<a href='"+separator+"' target='"+o.target+"' id='"+aid+"' ></a>").appendTo("body");
                    $("#"+aid)[0].click();
                }else if(o.target.toLowerCase()=="_self"){ 
                    if(!o.cache)document.body.innerHTML='';
                    window.location.href=separator+"&t="+new Date().getTime();
                }else{
                    window.open(separator,o.target);
                }
            }
        }  
    }); 
};  
})(jQuery);



 页面布置如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title></title>
    <script language="JavaScript" type="text/javascript" src="jquery-1.6.js" ></script>
    <script src="JQPI_02.js" language="javascript"  type="text/javascript" ></script>
</head>
<body>
<input class="text" />
<input type="button" value="搜索" />
</body>
</html>


 

你可能感兴趣的:(自编JQuery插件第十一个)