javascript common function (2)

  • 2020-09-28 08:41:53
  • OfStack

Main contents list:
16, remove array duplicates
17. Operate cookie
18. Determine the browser type
19. Decide whether to turn on cookie
20. Switch on JavaScript or not
21. JavaScript typewriter effect
22. Simple printing
23. Right clicking is prohibited
24. Prevent spam
25. Copy (javaeye flash edition)
26. Prevent bubbling events or default browser behavior
27, close or jump window prompt
28. Get the address bar parameters with javascript
29. Calculate the length of stay
30. div is empty. When there is only background, the background will be automatically increased

Main Contents:
16, remove array duplicates


<script> 
Array.prototype.remove = function(){ 
 var $ = this; 
 var o1 = {}; 
 var o2 = {}; 
 var o3 = []; 
 var o; 
 
 for(var i=0;o = $[i];i++){ 
  if(o in o1){ 
   if(!(o in o2)) o2[o] = o; 
   delete $[i]; 
  }else{ 
   o1[o] = o; 
  } 
 } 
 
 $.length = 0; 
 
 for(o in o1){ 
  $.push(o); 
 } 
 
 for(o in o2){ 
  o3.push(o); 
 } 
 
 return o3; 
 
} 
 
var a = [2,2,2,3,3,3,4,4,5,6,7,7]; 
 
a.remove (); 
 
document.write(a); 
 
</script> 

17. Operate cookie


// 1.  Set up the COOKIE 
 
//  A simple type  
 
function setCookie(c_name,value,expiredays) 
{ 
var exdate=new Date() 
exdate.setDate(exdate.getDate()+expiredays) 
 
document.cookie=c_name+ "=" +escape(value)+ 
((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) 
} 
 
//  The complete model  
function SetCookie(name,value,expires,path,domain,secure) 
{ 
var expDays = expires*24*60*60*1000; 
 
var expDate = new Date(); 
expDate.setTime(expDate.getTime()+expDays); 
 
var expString = ((expires==null) ? "" : (";expires= " +expDate.toGMTString())) 
var pathString = ((path==null) ? "" : (";path="+path)) 
var domainString = ((domain==null) ? "" : (";domain="+domain)) 
var secureString = ((secure==true) ? ";secure" : "" ) 
document.cookie = name + "=" + escape(value) + expString + pathString + domainString + secureString; 
} 
 
 
// 2 . Gets the specified name cookie Value:  
 
function getCookie(c_name) 
{ 
if (document.cookie.length>0) 
 { 
 c_start=document.cookie.indexOf(c_name + "=") 
 if (c_start!=-1) 
 { 
 c_start=c_start + c_name.length+1 
 c_end=document.cookie.indexOf(";",c_start) 
 if (c_end==-1) c_end=document.cookie.length 
 return unescape(document.cookie.substring(c_start,c_end)) 
 } 
 } 
return "" 
} 
 
 
// 3 . Deletes the specified name cookie :  
 
function ClearCookie(name) 
{ 
var expDate = new Date(); 
expDate.setTime(expDate.getTime()-100); 
 
document.cookie=name+ " =;expires= " +expDate.toGMTString(); 
 
} 
 
// 4.  detection cookie :  
 
function checkCookie() 
{ 
username=getCookie('username') 
if (username!=null && username!="") 
 {alert('Welcome again '+username+'!')} 
else 
 { 
 username=prompt('Please enter your name:',"") 
 if (username!=null && username!="") 
 { 
 setCookie('username',username,365) 
 } 
 } 
}

18. Get coordinates


<!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>payment</title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
</head> 
 
<body style="font-size:12px;"> 
 
<script> 
var strInfo=""; 
strInfo += " Wide visibility area for web pages :" + document.body.clientWidth + "<br>"; 
strInfo += " High visibility area for web pages :" + document.body.clientHeight + "<br>"; 
strInfo += " Wide visibility area for web pages :" + document.body.offsetWidth + "( Including the width of the edges )<br>"; 
strInfo += " High visibility area for web pages :" + document.body.offsetHeight + "( Including the width of the edges )<br>"; 
strInfo += " Wide text of web pages :" + document.body.scrollWidth + "<br>"; 
strInfo += " The full text of the web page is high :" + document.body.scrollHeight + "<br>"; 
strInfo += " The page is rolled high :" + document.body.scrollTop + "<br>"; 
strInfo += " The page is scrolled off to the left :" + document.body.scrollLeft + "<br>"; 
strInfo += " On the body of the web page :" + window.screenTop + "<br>"; 
strInfo += " The left part of the body of the web page :" + window.screenLeft + "<br>"; 
strInfo += " High screen resolution :" + window.screen.height + "<br>"; 
strInfo += " Width of screen resolution :" + window.screen.width + "<br>"; 
strInfo += " Screen available workspace height :" + window.screen.availHeight + "<br>"; 
strInfo += " Screen available workspace width :" + window.screen.availWidth + "<br>"; 
 
document.write(strInfo); 
</script> 
 
<br><br> 
<p> 
clientX  Sets or gets the mouse pointer position relative to the window client area  x  Coordinates, where the client area does not include the window's own controls and scroll bars.  <br> 
clientY  Sets or gets the mouse pointer position relative to the window client area  y  Coordinates, where the client area does not include the window's own controls and scroll bars. <br> 
offsetX  Sets or gets the mouse pointer position relative to the object that triggered the event  x  Coordinates. <br> 
offsetY  Sets or gets the mouse pointer position relative to the object that triggered the event  y  Coordinates. <br> 
screenX  Sets or gets the position of the mouse pointer relative to the user screen  x  Coordinates. <br> 
screenY  Sets or gets the mouse pointer position relative to the user's screen  y  Coordinates. <br> 
x  Sets or gets the mouse pointer position relative to the parent document  x  Pixel coordinates. <br> 
y  Sets or gets the mouse pointer position relative to the parent document  y  Pixel coordinates. <br> 
 
event.clientX Returns when the event occurred ,mouse Relative to the client window X Coordinates, event.X also 1 The sample. <br> 
 But if you set the location property value of the event object to relative . event.clientX The same, while event.X Returns the coordinates of the event object relative to the ontology. <br> 
</p> 
</body> 
</html> 

18. Determine the browser type
Js code


<script type="text/javascript"> 
  var Sys = {}; 
  var ua = navigator.userAgent.toLowerCase(); 
  var s; 
  (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] : 
  (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] : 
  (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] : 
  (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] : 
  (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0; 
 
  // Test the following  
  if (Sys.ie) document.write('IE: ' + Sys.ie); 
  if (Sys.firefox) document.write('Firefox: ' + Sys.firefox); 
  if (Sys.chrome) document.write('Chrome: ' + Sys.chrome); 
  if (Sys.opera) document.write('Opera: ' + Sys.opera); 
  if (Sys.safari) document.write('Safari: ' + Sys.safari); 
 </script> 

jquery version


<script src="jquery-latest.js"></script> 
<script type="text/javascript">  
$(document).ready(function(){ 
 var bro=$.browser; 
 var binfo=""; 
 if(bro.msie) {binfo="Microsoft Internet Explorer "+bro.version;} 
 if(bro.mozilla) {binfo="Mozilla Firefox "+bro.version;} 
 if(bro.safari) {binfo="Apple Safari "+bro.version;} 
 if(bro.opera) {binfo="Opera "+bro.version;} 
 alert(binfo); 
}) 
</script> 

19. Decide whether to turn on cookie


<script> 
 function checkCookie() { 
  var result=false; 
  if(navigator.cookiesEnabled){ return true; } 
  document.cookie = "testcookie=yes;"; 
 
  var setCookie = document.cookie; 
 
  if (setCookie.indexOf("testcookie=yes") > -1){ 
   result=true; 
  }else{ 
   document.cookie = ""; 
  } 
 
  return result; 
 } 
 
  if(!checkCookie()){ 
  alert(" Sorry, your browser's Cookie The function is disabled, please turn it on ");  
  }else{ 
  alert("Cookie  Successful opening "); 
  } 
</script> 
 

20. Switch on JavaScript or not


//  plan  1 
 
<span id="js_enable"> You shut down JavaScript</span> 
<script type="text/javascript"> 
<!-- 
 document.getElementById("js_enable").innerHTML=' You open the JavaScript'; 
--> 
</script> 
 
//  plan  2 
 
<div id="NoJs" > You are disabled javascript . </div> 
<div id="YesJs" style="display:none;"> your Javascript Is open </div>  
<script>  
 var NoJs= document.getElementById("NoJs"); 
 var YesJs= document.getElementById("YesJs"); 
 NoJs.style.display="none"; 
 YesJs.style.display="block"; 
</script> 
 
//  plan  3 
 
<!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> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title> Check that the browser is open JavaScript</title> 
</head> 
 
<body> 
 If your browser supports it, nothing will be displayed on this page. If not, a prompt will appear!  
<noscript>  
<body scroll=no style="text-align: center"> 
<center> 
 <table border="0" style="height: 100%; width: 100%; right: 1%; left: 1%; background: black; position: fixed"> 
  <tr> 
   <td align="center"> 
    <div style="position: fixed; font-size: 18px; z-index: 2; cursor: help; background: #F8F8FF; width: 480px; color: black; padding: 5px 5px 5px 5px; border: 1px solid; border-color: maroon; height: auto; text-align: left; left: 20%"> 
    <span style="font: bold 20px Arial; color:#F8F8FF; background: maroon; vertical-align: middle"> Sorry, your browser is not open JavaScript Script support! </span></div> 
   </td> 
  </tr> 
 </table> 
</center> 
</noscript> 
</body> 
</html> 

HTML < noscript > The label
Definition and usage The noscript element is used to define alternate content (text) when the script is not executed.
This tag can be used for identification < script > The browser TAB cannot support the script in it.

Note: If the browser supports scripting, it will not display the text in the noscript element.
Note: Not recognized < script > The browser for the tag displays the contents of the tag on the page. To prevent the browser from doing this, you should hide the script in the comment tag. Old-fashioned (unrecognizable < script > Browsers ignore comments so they don't write them to the page, and newer browsers know how to execute these scripts, even if they're wrapped in comment tags!
21. Effect of JavaScript typewriter
Example 1


<html> 
<title>JavaScript  A typewriter </title> 
<head> 
<style type="text/css"> 
 body{ 
  font-family: Trebuchet MS, Lucida Sans Unicode, Arial, sans-serif; 
  margin-top:0px; 
  background-image:url('../../images/heading3.gif'); 
  background-repeat:no-repeat; 
  padding-top:100px; 
 } 
 #myContent, #myContent blink{ 
  width:500px; 
  height:200px; 
  background:black; 
  color: #00FF00; 
  font-family:courier; 
 }  
 blink{ 
  display:inline; 
 
 } 
 </style> 
 <script type="text/javascript"> 
 var charIndex = -1; 
 var stringLength = 0; 
 var inputText; 
 function writeContent(init){ 
  if(init){ 
   inputText = document.getElementById('contentToWrite').innerHTML; 
  } 
  if(charIndex==-1){ 
   charIndex = 0; 
   stringLength = inputText.length; 
  } 
  var initString = document.getElementById('myContent').innerHTML; 
  initStringinitString = initString.replace(/<SPAN.*$/gi,""); 
   
  var theChar = inputText.charAt(charIndex); 
  var nextFourChars = inputText.substr(charIndex,4); 
  if(nextFourChars=='<BR>' || nextFourChars=='<br>'){ 
   theChar = '<BR>'; 
   charIndex+=3; 
  } 
  initStringinitString = initString + theChar + "<SPAN id='blink'>_</SPAN>"; 
  document.getElementById('myContent').innerHTML = initString; 
 
  charIndexcharIndex = charIndex/1 +1; 
  if(charIndex%2==1){ 
    document.getElementById('blink').style.display='none'; 
  }else{ 
    document.getElementById('blink').style.display='inline'; 
  } 
     
  if(charIndex<=stringLength){ 
   setTimeout('writeContent(false)',150); 
  }else{ 
   blinkSpan(); 
  } 
 } 
  
 var currentStyle = 'inline'; 
 function blinkSpan(){ 
  if(currentStyle=='inline'){ 
   currentStyle='none'; 
  }else{ 
   currentStyle='inline'; 
  } 
  document.getElementById('blink').style.display = currentStyle; 
  setTimeout('blinkSpan()',500); 
   
 } 
 </script> 
 
<body> 
 
<div id="myContent"> 
</div> 
<div id="contentToWrite" style="display:none"> 
 <!-- Put the typewriter content here--> 
 sharejs.com 
 Login : username<br> 
 password : ******<br> 
 Access is granted<br> 
 <!-- End typewriter content --> 
 </div> 
 <script type="text/javascript"> 
 writeContent(true); 
</script> 
 
 
</body> 
</html> 

Example 2 (compatible with IE, FX)


<html> 
<head> 
<title> Typewritten effects with linked headlines </title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<style type="text/css"> 
body{font-size:14px;font-weight:bold;} 
</style> 
</head> 
<body> 
 Latest Content: <a id="HotNews" href="" target="_blank"></a> 
<script language="JavaScript"> 
var NewsTime = 2000; // Stay time for each piece of news  
var TextTime = 50; // The shorter the wait time, the faster the headline will appear  
 
var newsi = 0; 
var txti = 0; 
var txttimer; 
var newstimer; 
 
var newnewstitle = new Array(); // News headlines  
var newnewshref = new Array(); // News link  
 
newstitle[0] = "javascript Commonly used functions "; 
newshref[0] = "https://www.ofstack.com/article/74365.htm"; 
 
newstitle[1] = "https://www.ofstack.com/"; 
newshref[1] = "https://www.ofstack.com/"; 
 
function shownew() 
{ 
 var endstr = "_"; 
 hwnewstr = newstitle[newsi]; 
 newslink = newshref[newsi]; 
 if(txti==(hwnewstr.length-1)){endstr="";} 
 if(txti>=hwnewstr.length){ 
 clearInterval(txttimer); 
 clearInterval(newstimer); 
 newsi++; 
 if(newsi>=newstitle.length){ 
 newsi = 0 
 } 
 newstimer = setInterval("shownew()",NewsTime); 
 txti = 0; 
 return; 
 } 
 clearInterval(txttimer); 
 document.getElementById("HotNews").href=newslink; 
 document.getElementById("HotNews").innerHTML = hwnewstr.substring(0,txti+1)+endstr; 
 txti++; 
 txttimer = setInterval("shownew()",TextTime); 
} 
shownew(); 
</script> 
</body> 
</html> 

22. Simple printing


<style type="text/css" media=print> 
.noprint{display : none } 
</style> 
 
<input id="btnPrint" type="button" value=" print " onclick="javascript:window.print();" /><br> 
 Here's where it's printed, here's where it's printed, here's where it's printed, here's where it's printed, here's where it's printed, <br> 
 Here's where it's printed, here's where it's printed, here's where it's printed, here's where it's printed, here's where it's printed, <br> 
 Here's where it's printed, here's where it's printed, here's where it's printed, here's where it's printed, here's where it's printed, <br> 
<p class="noprint"> This is where you don't need to print </p> 

23. Right clicking is prohibited


// 1.  Set up the COOKIE 
 
//  A simple type  
 
function setCookie(c_name,value,expiredays) 
{ 
var exdate=new Date() 
exdate.setDate(exdate.getDate()+expiredays) 
 
document.cookie=c_name+ "=" +escape(value)+ 
((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) 
} 
 
//  The complete model  
function SetCookie(name,value,expires,path,domain,secure) 
{ 
var expDays = expires*24*60*60*1000; 
 
var expDate = new Date(); 
expDate.setTime(expDate.getTime()+expDays); 
 
var expString = ((expires==null) ? "" : (";expires= " +expDate.toGMTString())) 
var pathString = ((path==null) ? "" : (";path="+path)) 
var domainString = ((domain==null) ? "" : (";domain="+domain)) 
var secureString = ((secure==true) ? ";secure" : "" ) 
document.cookie = name + "=" + escape(value) + expString + pathString + domainString + secureString; 
} 
 
 
// 2 . Gets the specified name cookie Value:  
 
function getCookie(c_name) 
{ 
if (document.cookie.length>0) 
 { 
 c_start=document.cookie.indexOf(c_name + "=") 
 if (c_start!=-1) 
 { 
 c_start=c_start + c_name.length+1 
 c_end=document.cookie.indexOf(";",c_start) 
 if (c_end==-1) c_end=document.cookie.length 
 return unescape(document.cookie.substring(c_start,c_end)) 
 } 
 } 
return "" 
} 
 
 
// 3 . Deletes the specified name cookie :  
 
function ClearCookie(name) 
{ 
var expDate = new Date(); 
expDate.setTime(expDate.getTime()-100); 
 
document.cookie=name+ " =;expires= " +expDate.toGMTString(); 
 
} 
 
// 4.  detection cookie :  
 
function checkCookie() 
{ 
username=getCookie('username') 
if (username!=null && username!="") 
 {alert('Welcome again '+username+'!')} 
else 
 { 
 username=prompt('Please enter your name:',"") 
 if (username!=null && username!="") 
 { 
 setCookie('username',username,365) 
 } 
 } 
}
0

24. Prevent spam


// 1.  Set up the COOKIE 
 
//  A simple type  
 
function setCookie(c_name,value,expiredays) 
{ 
var exdate=new Date() 
exdate.setDate(exdate.getDate()+expiredays) 
 
document.cookie=c_name+ "=" +escape(value)+ 
((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) 
} 
 
//  The complete model  
function SetCookie(name,value,expires,path,domain,secure) 
{ 
var expDays = expires*24*60*60*1000; 
 
var expDate = new Date(); 
expDate.setTime(expDate.getTime()+expDays); 
 
var expString = ((expires==null) ? "" : (";expires= " +expDate.toGMTString())) 
var pathString = ((path==null) ? "" : (";path="+path)) 
var domainString = ((domain==null) ? "" : (";domain="+domain)) 
var secureString = ((secure==true) ? ";secure" : "" ) 
document.cookie = name + "=" + escape(value) + expString + pathString + domainString + secureString; 
} 
 
 
// 2 . Gets the specified name cookie Value:  
 
function getCookie(c_name) 
{ 
if (document.cookie.length>0) 
 { 
 c_start=document.cookie.indexOf(c_name + "=") 
 if (c_start!=-1) 
 { 
 c_start=c_start + c_name.length+1 
 c_end=document.cookie.indexOf(";",c_start) 
 if (c_end==-1) c_end=document.cookie.length 
 return unescape(document.cookie.substring(c_start,c_end)) 
 } 
 } 
return "" 
} 
 
 
// 3 . Deletes the specified name cookie :  
 
function ClearCookie(name) 
{ 
var expDate = new Date(); 
expDate.setTime(expDate.getTime()-100); 
 
document.cookie=name+ " =;expires= " +expDate.toGMTString(); 
 
} 
 
// 4.  detection cookie :  
 
function checkCookie() 
{ 
username=getCookie('username') 
if (username!=null && username!="") 
 {alert('Welcome again '+username+'!')} 
else 
 { 
 username=prompt('Please enter your name:',"") 
 if (username!=null && username!="") 
 { 
 setCookie('username',username,365) 
 } 
 } 
}
1

25. Copy (javaeye flash version)


// 1.  Set up the COOKIE 
 
//  A simple type  
 
function setCookie(c_name,value,expiredays) 
{ 
var exdate=new Date() 
exdate.setDate(exdate.getDate()+expiredays) 
 
document.cookie=c_name+ "=" +escape(value)+ 
((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) 
} 
 
//  The complete model  
function SetCookie(name,value,expires,path,domain,secure) 
{ 
var expDays = expires*24*60*60*1000; 
 
var expDate = new Date(); 
expDate.setTime(expDate.getTime()+expDays); 
 
var expString = ((expires==null) ? "" : (";expires= " +expDate.toGMTString())) 
var pathString = ((path==null) ? "" : (";path="+path)) 
var domainString = ((domain==null) ? "" : (";domain="+domain)) 
var secureString = ((secure==true) ? ";secure" : "" ) 
document.cookie = name + "=" + escape(value) + expString + pathString + domainString + secureString; 
} 
 
 
// 2 . Gets the specified name cookie Value:  
 
function getCookie(c_name) 
{ 
if (document.cookie.length>0) 
 { 
 c_start=document.cookie.indexOf(c_name + "=") 
 if (c_start!=-1) 
 { 
 c_start=c_start + c_name.length+1 
 c_end=document.cookie.indexOf(";",c_start) 
 if (c_end==-1) c_end=document.cookie.length 
 return unescape(document.cookie.substring(c_start,c_end)) 
 } 
 } 
return "" 
} 
 
 
// 3 . Deletes the specified name cookie :  
 
function ClearCookie(name) 
{ 
var expDate = new Date(); 
expDate.setTime(expDate.getTime()-100); 
 
document.cookie=name+ " =;expires= " +expDate.toGMTString(); 
 
} 
 
// 4.  detection cookie :  
 
function checkCookie() 
{ 
username=getCookie('username') 
if (username!=null && username!="") 
 {alert('Welcome again '+username+'!')} 
else 
 { 
 username=prompt('Please enter your name:',"") 
 if (username!=null && username!="") 
 { 
 setCookie('username',username,365) 
 } 
 } 
}
2

26. Prevent bubbling events or default browser behavior


// Prevent bubbling events  
 function stopBubble(e) { 
 if (e && e.stopPropagation) {// non IE 
  e.stopPropagation(); 
 } 
 else {//IE 
  window.event.cancelBubble = true; 
 } 
 } 
 
function stopDefault(e) { 
 // Block default browser actions (W3C) 
 if (e && e.preventDefault) 
  e.preventDefault(); 
 //IE Block the default action of the function  
 else 
  window.event.returnValue = false; 
 return false; 
 } 

27, close or jump window prompt


// 1.  Set up the COOKIE 
 
//  A simple type  
 
function setCookie(c_name,value,expiredays) 
{ 
var exdate=new Date() 
exdate.setDate(exdate.getDate()+expiredays) 
 
document.cookie=c_name+ "=" +escape(value)+ 
((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) 
} 
 
//  The complete model  
function SetCookie(name,value,expires,path,domain,secure) 
{ 
var expDays = expires*24*60*60*1000; 
 
var expDate = new Date(); 
expDate.setTime(expDate.getTime()+expDays); 
 
var expString = ((expires==null) ? "" : (";expires= " +expDate.toGMTString())) 
var pathString = ((path==null) ? "" : (";path="+path)) 
var domainString = ((domain==null) ? "" : (";domain="+domain)) 
var secureString = ((secure==true) ? ";secure" : "" ) 
document.cookie = name + "=" + escape(value) + expString + pathString + domainString + secureString; 
} 
 
 
// 2 . Gets the specified name cookie Value:  
 
function getCookie(c_name) 
{ 
if (document.cookie.length>0) 
 { 
 c_start=document.cookie.indexOf(c_name + "=") 
 if (c_start!=-1) 
 { 
 c_start=c_start + c_name.length+1 
 c_end=document.cookie.indexOf(";",c_start) 
 if (c_end==-1) c_end=document.cookie.length 
 return unescape(document.cookie.substring(c_start,c_end)) 
 } 
 } 
return "" 
} 
 
 
// 3 . Deletes the specified name cookie :  
 
function ClearCookie(name) 
{ 
var expDate = new Date(); 
expDate.setTime(expDate.getTime()-100); 
 
document.cookie=name+ " =;expires= " +expDate.toGMTString(); 
 
} 
 
// 4.  detection cookie :  
 
function checkCookie() 
{ 
username=getCookie('username') 
if (username!=null && username!="") 
 {alert('Welcome again '+username+'!')} 
else 
 { 
 username=prompt('Please enter your name:',"") 
 if (username!=null && username!="") 
 { 
 setCookie('username',username,365) 
 } 
 } 
}
4

28. Get the address bar parameters with javascript
// Address of this page: alert(ES124en.location);
Method 1:


// 1.  Set up the COOKIE 
 
//  A simple type  
 
function setCookie(c_name,value,expiredays) 
{ 
var exdate=new Date() 
exdate.setDate(exdate.getDate()+expiredays) 
 
document.cookie=c_name+ "=" +escape(value)+ 
((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) 
} 
 
//  The complete model  
function SetCookie(name,value,expires,path,domain,secure) 
{ 
var expDays = expires*24*60*60*1000; 
 
var expDate = new Date(); 
expDate.setTime(expDate.getTime()+expDays); 
 
var expString = ((expires==null) ? "" : (";expires= " +expDate.toGMTString())) 
var pathString = ((path==null) ? "" : (";path="+path)) 
var domainString = ((domain==null) ? "" : (";domain="+domain)) 
var secureString = ((secure==true) ? ";secure" : "" ) 
document.cookie = name + "=" + escape(value) + expString + pathString + domainString + secureString; 
} 
 
 
// 2 . Gets the specified name cookie Value:  
 
function getCookie(c_name) 
{ 
if (document.cookie.length>0) 
 { 
 c_start=document.cookie.indexOf(c_name + "=") 
 if (c_start!=-1) 
 { 
 c_start=c_start + c_name.length+1 
 c_end=document.cookie.indexOf(";",c_start) 
 if (c_end==-1) c_end=document.cookie.length 
 return unescape(document.cookie.substring(c_start,c_end)) 
 } 
 } 
return "" 
} 
 
 
// 3 . Deletes the specified name cookie :  
 
function ClearCookie(name) 
{ 
var expDate = new Date(); 
expDate.setTime(expDate.getTime()-100); 
 
document.cookie=name+ " =;expires= " +expDate.toGMTString(); 
 
} 
 
// 4.  detection cookie :  
 
function checkCookie() 
{ 
username=getCookie('username') 
if (username!=null && username!="") 
 {alert('Welcome again '+username+'!')} 
else 
 { 
 username=prompt('Please enter your name:',"") 
 if (username!=null && username!="") 
 { 
 setCookie('username',username,365) 
 } 
 } 
}
5

Method 2:


// 1.  Set up the COOKIE 
 
//  A simple type  
 
function setCookie(c_name,value,expiredays) 
{ 
var exdate=new Date() 
exdate.setDate(exdate.getDate()+expiredays) 
 
document.cookie=c_name+ "=" +escape(value)+ 
((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) 
} 
 
//  The complete model  
function SetCookie(name,value,expires,path,domain,secure) 
{ 
var expDays = expires*24*60*60*1000; 
 
var expDate = new Date(); 
expDate.setTime(expDate.getTime()+expDays); 
 
var expString = ((expires==null) ? "" : (";expires= " +expDate.toGMTString())) 
var pathString = ((path==null) ? "" : (";path="+path)) 
var domainString = ((domain==null) ? "" : (";domain="+domain)) 
var secureString = ((secure==true) ? ";secure" : "" ) 
document.cookie = name + "=" + escape(value) + expString + pathString + domainString + secureString; 
} 
 
 
// 2 . Gets the specified name cookie Value:  
 
function getCookie(c_name) 
{ 
if (document.cookie.length>0) 
 { 
 c_start=document.cookie.indexOf(c_name + "=") 
 if (c_start!=-1) 
 { 
 c_start=c_start + c_name.length+1 
 c_end=document.cookie.indexOf(";",c_start) 
 if (c_end==-1) c_end=document.cookie.length 
 return unescape(document.cookie.substring(c_start,c_end)) 
 } 
 } 
return "" 
} 
 
 
// 3 . Deletes the specified name cookie :  
 
function ClearCookie(name) 
{ 
var expDate = new Date(); 
expDate.setTime(expDate.getTime()-100); 
 
document.cookie=name+ " =;expires= " +expDate.toGMTString(); 
 
} 
 
// 4.  detection cookie :  
 
function checkCookie() 
{ 
username=getCookie('username') 
if (username!=null && username!="") 
 {alert('Welcome again '+username+'!')} 
else 
 { 
 username=prompt('Please enter your name:',"") 
 if (username!=null && username!="") 
 { 
 setCookie('username',username,365) 
 } 
 } 
}
6


Method 3:


// 1.  Set up the COOKIE 
 
//  A simple type  
 
function setCookie(c_name,value,expiredays) 
{ 
var exdate=new Date() 
exdate.setDate(exdate.getDate()+expiredays) 
 
document.cookie=c_name+ "=" +escape(value)+ 
((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) 
} 
 
//  The complete model  
function SetCookie(name,value,expires,path,domain,secure) 
{ 
var expDays = expires*24*60*60*1000; 
 
var expDate = new Date(); 
expDate.setTime(expDate.getTime()+expDays); 
 
var expString = ((expires==null) ? "" : (";expires= " +expDate.toGMTString())) 
var pathString = ((path==null) ? "" : (";path="+path)) 
var domainString = ((domain==null) ? "" : (";domain="+domain)) 
var secureString = ((secure==true) ? ";secure" : "" ) 
document.cookie = name + "=" + escape(value) + expString + pathString + domainString + secureString; 
} 
 
 
// 2 . Gets the specified name cookie Value:  
 
function getCookie(c_name) 
{ 
if (document.cookie.length>0) 
 { 
 c_start=document.cookie.indexOf(c_name + "=") 
 if (c_start!=-1) 
 { 
 c_start=c_start + c_name.length+1 
 c_end=document.cookie.indexOf(";",c_start) 
 if (c_end==-1) c_end=document.cookie.length 
 return unescape(document.cookie.substring(c_start,c_end)) 
 } 
 } 
return "" 
} 
 
 
// 3 . Deletes the specified name cookie :  
 
function ClearCookie(name) 
{ 
var expDate = new Date(); 
expDate.setTime(expDate.getTime()-100); 
 
document.cookie=name+ " =;expires= " +expDate.toGMTString(); 
 
} 
 
// 4.  detection cookie :  
 
function checkCookie() 
{ 
username=getCookie('username') 
if (username!=null && username!="") 
 {alert('Welcome again '+username+'!')} 
else 
 { 
 username=prompt('Please enter your name:',"") 
 if (username!=null && username!="") 
 { 
 setCookie('username',username,365) 
 } 
 } 
}
7

29. Calculate the length of stay


<html> 
<form name="myform"> 
<td vAlign="top" width="135"> You are staying here:  
<input name="clock" size="8" value=" Online time "></td> 
</form> 
<script language="JavaScript"> 
var id, iM = 0, iS = 1; 
start = new Date(); 
function go(){ 
 now = new Date(); 
 time = (now.getTime() - start.getTime()) / 1000; 
 time = Math.floor( time); 
 iS = time % 60; 
 iM = Math.floor( time / 60); 
 if ( iS < 10) 
 document.myform.clock.value = " " + iM + "  points  0" + iS + "  seconds "; 
 else 
 document.myform.clock.value = " " + iM + "  points  " + iS + "  seconds "; 
 id = setTimeout( "go()", 1000); 
} 
go(); 
</script> 
</body> 
</html> 

30. div is empty. When there is only background, the background is automatically increased


// 1.  Set up the COOKIE 
 
//  A simple type  
 
function setCookie(c_name,value,expiredays) 
{ 
var exdate=new Date() 
exdate.setDate(exdate.getDate()+expiredays) 
 
document.cookie=c_name+ "=" +escape(value)+ 
((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) 
} 
 
//  The complete model  
function SetCookie(name,value,expires,path,domain,secure) 
{ 
var expDays = expires*24*60*60*1000; 
 
var expDate = new Date(); 
expDate.setTime(expDate.getTime()+expDays); 
 
var expString = ((expires==null) ? "" : (";expires= " +expDate.toGMTString())) 
var pathString = ((path==null) ? "" : (";path="+path)) 
var domainString = ((domain==null) ? "" : (";domain="+domain)) 
var secureString = ((secure==true) ? ";secure" : "" ) 
document.cookie = name + "=" + escape(value) + expString + pathString + domainString + secureString; 
} 
 
 
// 2 . Gets the specified name cookie Value:  
 
function getCookie(c_name) 
{ 
if (document.cookie.length>0) 
 { 
 c_start=document.cookie.indexOf(c_name + "=") 
 if (c_start!=-1) 
 { 
 c_start=c_start + c_name.length+1 
 c_end=document.cookie.indexOf(";",c_start) 
 if (c_end==-1) c_end=document.cookie.length 
 return unescape(document.cookie.substring(c_start,c_end)) 
 } 
 } 
return "" 
} 
 
 
// 3 . Deletes the specified name cookie :  
 
function ClearCookie(name) 
{ 
var expDate = new Date(); 
expDate.setTime(expDate.getTime()-100); 
 
document.cookie=name+ " =;expires= " +expDate.toGMTString(); 
 
} 
 
// 4.  detection cookie :  
 
function checkCookie() 
{ 
username=getCookie('username') 
if (username!=null && username!="") 
 {alert('Welcome again '+username+'!')} 
else 
 { 
 username=prompt('Please enter your name:',"") 
 if (username!=null && username!="") 
 { 
 setCookie('username',username,365) 
 } 
 } 
}
9


iframe version


<iframe src="iframe_b.html" scrolling="no" frameborder="0" width="100%" onload="this.height=this.contentWindow.document.documentElement.scrollHeight"></iframe> 

These are the common javascript functions that this site has compiled for you, I hope to help you with your study, and the last article on the common javascript functions will be published soon, continue to pay attention.


Related articles: