javascript小技巧(四)



      

function checkBrowser() 
{  
   this.ver=navigator.appVersion  
   this.dom=document.getElementById?1:0  
   this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom)?1:0;  
   this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;  
   this.ie4=(document.all && !this.dom)?1:0;  
   this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;  
   this.ns4=(document.layers && !this.dom)?1:0;  
   this.mac=(this.ver.indexOf('Mac') > -1) ?1:0;  
   this.ope=(navigator.userAgent.indexOf('Opera')>-1);  
   this.ie=(this.ie6 || this.ie5 || this.ie4)  
   this.ns=(this.ns4 || this.ns5)  
   this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns5 || this.ns4 || this.mac || this.ope)  
   this.nbw=(!this.bw)  
   return this; 
} 

        

<SCRIPT   language="javascript">   
function   test(obj)   
{   
    var   range   =   obj.createTextRange();   
     alert("     :   "   +   range.boundingWidth     
     +  "px\r
: " + range.boundingHeight + "px"); } </SCRIPT> <BODY> <Textarea id="txt" height="150">sdf</textarea><INPUT type="button" value=" " onClick="test(txt)"> </BODY>

        

function modelessAlert(Msg) 
{ 
    window.showModelessDialog("javascript:alert(\""+escape(Msg)+"\");window.close();","","status:no;resizable:no;help:no;dialogHeight:height:30px;dialogHeight:40px;"); 
} 

     

<html> 
<head> 
   <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
   <noscript><meta http-equiv="refresh" content="0;url=about:noscript"></noscript> 
   <title>      、Ctrl+N、Shift+F10、Alt+F4、F11、F5  、   </title> 
</head> 
<body> 
<script language="Javascript"><!-- 
  //      、Ctrl+N、Shift+F10、F11、F5  、    
  //Author: meizz(   ) 2002-6-18 
function document.oncontextmenu(){event.returnValue=false;}//       
function window.onhelp(){return false} //  F1   
function document.onkeydown() 
{ 
  if ((window.event.altKey)&& 
       ((window.event.keyCode==37)||   //   Alt+     ← 
        (window.event.keyCode==39)))   //   Alt+     → 
   { 
      alert("     ALT+          !"); 
      event.returnValue=false; 
   } 
     /*  :          Alt+    , 
         Alt+          ,   Alt    , 
              ,          。    
                 Alt     ,   。*/ 
  if ((event.keyCode==8)   ||                 //        
       (event.keyCode==116)||                 //   F5     
       (event.ctrlKey && event.keyCode==82)){ //Ctrl + R 
      event.keyCode=0; 
      event.returnValue=false; 
      } 
  if (event.keyCode==122){event.keyCode=0;event.returnValue=false;}  //  F11 
  if (event.ctrlKey && event.keyCode==78) event.returnValue=false;   //   Ctrl+n 
  if (event.shiftKey && event.keyCode==121)event.returnValue=false;  //   shift+F10 
  if (window.event.srcElement.tagName == "A" && window.event.shiftKey)  
       window.event.returnValue = false;             //   shift            
  if ((window.event.altKey)&&(window.event.keyCode==115))             //  Alt+F4 
   { 
       window.showModelessDialog("about:blank","","dialogWidth:1px;dialogheight:1px"); 
      return false; 
   } 
} 
</script> 
      、Ctrl+N、Shift+F10、Alt+F4、F11、F5  、    
</body> 
</html> 

     ,   


1.<span style='position:absolute;width:200;height:200;background:red' onmousedown=MouseDown(this) onmousemove=MouseMove() onmouseup=MouseUp()>meizz</span> 
<script language=javascript> 
var Obj; 
function MouseDown(obj) 
{ 
   Obj=obj; 
   Obj.setCapture(); 
   Obj.l=event.x-Obj.style.pixelLeft; 
   Obj.t=event.y-Obj.style.pixelTop; 
} 
function MouseMove() 
{ 
  if(Obj!=null) 
   { 
     Obj.style.left = event.x-Obj.l; 
     Obj.style.top = event.y-Obj.t; 
   } 
} 
function MouseUp() 
{ 
  if(Obj!=null) 
   { 
     Obj.releaseCapture(); 
     Obj=null; 
   } 
} 
</script> 
2. 
<div id="myDiv" src="logo.gif" ondrag="doDrag();" onmouseover="this.style.cursor='hand'" style="position:absolute;left=100;top=100;" onmousedown="doMouseDown();"> 
<a href="#" onclick="return false"><h1>wlecome</h1></a> 
</div> 
<script language="JavaScript" type="text/javascript"> 
var orgMouseX; 
var orgMouseY; 
var orgObjX; 
var orgObjY; 
function doDrag() 
{ 
var myObject=document.all.myDiv; 

var x=event.clientX; 
var y=event.clientY; 
myObject.style.left=x-(orgMouseX-orgObjX); 
myObject.style.top=y-(orgMouseY-orgObjY); 
  
} 
function doMouseDown() 
{ 
orgMouseX=event.clientX; 
orgMouseY=event.clientY; 
orgObjX=parseInt(document.all.myDiv.style.left); 
orgObjY=parseInt(document.all.myDiv.style.top); 
} 

</script> 

       


<iframe src="a.html" id="f" name="f" scrolling="no" frameborder=0 marginwidth=0 marginheight=0></iframe> 
<script> 
var doc=window.frames["f"].document; 
function s(){ 
if (doc.readyState=="complete"){ 
   document.all.f.style.height=doc.body.scrollHeight 
   document.all.f.style.width=doc.body.scrollWidth 
} 
} 
doc.onreadystatechange=s 
</script> 

  COOKIE 


function SetCookie(sName, sValue) 
{ 
document.cookie = sName + "=" + escape(sValue) + "; "; 
} 
function GetCookie(sName) 
{ 
var aCookie = document.cookie.split("; "); 
for (var i=0; i < aCookie.length; i++) 
{ 
   
  var aCrumb = aCookie[i].split("="); 
  if (sName == aCrumb[0])  
  return unescape(aCrumb[1]); 
} 
  
} 
function DelCookie(sName) 
{ 
document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 1999 23:59:59 GMT;"; 
} 

setTimeout     

<script> 
var _st = window.setTimeout; 
window.setTimeout = function(fRef, mDelay) { 
if(typeof fRef == 'function'){ 
  var argu = Array.prototype.slice.call(arguments,2); 
  var f = (function(){ fRef.apply(null, argu); }); 
  return _st(f, mDelay); 
} 
return _st(fRef,mDelay); 
} 
function test(x){ 
alert(x); 
} 
window.setTimeout(test,1000,'fason'); 
</script> 

    apply,call 

Function.prototype.apply = function (obj, argu) { 
if (obj) obj.constructor.prototype._caller = this;  
var argus = new Array(); 
for (var i=0;i<argu.length;i++) 
   argus[i] = "argu[" + i + "]"; 
var r; 
eval("r = " + (obj ? ("obj._caller(" + argus.join(",") + ");") : ("this(" + argus.join(",") + ");"))); 
return r; 
}; 
Function.prototype.call = function (obj) { 
var argu = new Array(); 
for (var i=1;i<arguments.length;i++) 
   argu[i-1] = arguments[i]; 
return this.apply(obj, argu); 
};   

     

function DownURL(strRemoteURL,strLocalURL) 
{ 
try 
{ 
  var xmlHTTP=new ActiveXObject("Microsoft.XMLHTTP"); 
   xmlHTTP.open("Get",strRemoteURL,false); 
   xmlHTTP.send(); 
  var adodbStream=new ActiveXObject("ADODB.Stream"); 
   adodbStream.Type=1;//1=adTypeBinary 
   adodbStream.Open(); 
   adodbStream.write(xmlHTTP.responseBody); 
   adodbStream.SaveToFile(strLocalURL,2); 
   adodbStream.Close(); 
   adodbStream=null; 
   xmlHTTP=null; 
   
} 
catch(e) 
{ 
   window.confirm("  URL  !"); 
} 
//window.confirm("    ."); 
} 

         

function getXML(URL)  
{ 
var xmlhttp = new ActiveXObject("microsoft.xmlhttp"); 
xmlhttp.Open("GET",URL, false);  
try 
{  
   xmlhttp.Send(); 
} 
catch(e){} 
finally  
{ 
  var result = xmlhttp.responseText; 
  if(result)  
   { 
   if(xmlhttp.Status==200) 
    { 
    return(true); 
    } 
   else  
    { 
    return(false); 
    } 
   } 
  else  
   { 
   return(false); 
   } 
} 
} 

(1)     
event.dataTransfer.setData("URL", oImage.src); 
sImageURL = event.dataTransfer.getData("URL") 
(2)     
window.clipboardData.setData("Text",oSource.innerText); 
window.clipboardData.getData("Text"); 

<HTML> 
<HEAD> 
<META NAME="save" CONTENT="history"> 
<STYLE> 
    .sHistory {behavior:url(#default#savehistory);} 
</STYLE> 
</HEAD> 
<BODY> 
<INPUT class=sHistory type=text id=oPersistInput> 
</BODY> 
</HTML>