Common javascript Functions (2) _ javascript skills

Source: Internet
Author: User
This article mainly introduces common javascript functions. The next 15 common functions are highly practical. If you are interested, please refer to them. Main content list:
16. Remove array duplicates
17. cookie operation
18. Determine the browser type
19. Determine whether to enable cookie
20. Disable JavaScript
21. JavaScript typewriter Effect
22. Simple Printing
23. Right-click prohibited
24. prevent spam
25. Copy (javaeye flash)
26. Prevent bubble events or prevent browser default Behaviors
27. Prompt when closing or redirecting to a window
28. use javascript to obtain parameters in the address bar
29. Calculate the stay time
30. p is null. The background increases automatically only when the background is used.

Main content:
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. cookie operation

// 1. set COOKIE // simple 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 ()} // complete 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. obtain the cookie value of the specified name: 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. delete the cookie with the specified name: function ClearCookie (name) {var expDate = new Date (); expDate. setTime (expDate. getTime ()-100); document. cookie = name + "=; expires =" + expDate. toGMTString ();} // 4. cookie Detection: 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. Obtain coordinates

   Payment 
    Script var strInfo = ""; strInfo + = "visible area width of the webpage:" + document. body. clientWidth +"
"; StrInfo + =" visible area of the webpage: "+ document. body. clientHeight +"
"; StrInfo + =" visible area width of the webpage: "+ document. body. offsetWidth +" (including the width of the edge)
"; StrInfo + =" visible area height of the webpage: "+ document. body. offsetHeight +" (including the width of the edge)
"; StrInfo + =" webpage body full text width: "+ document. body. scrollWidth +"
"; StrInfo + =" webpage text Height: "+ document. body. scrollHeight +"
"; StrInfo + =" webpage volume Height: "+ document. body. scrollTop +"
"; StrInfo + =" Left of the webpage volume: "+ document. body. scrollLeft +"
"; StrInfo + =" webpage body part: "+ window. screenTop +"
"; StrInfo + =" webpage body section left: "+ window. screenLeft +"
"; StrInfo + =" high screen resolution: "+ window. screen. height +"
"; StrInfo + =" screen Resolution width: "+ window. screen. width +"
"; StrInfo + =" available workspace height on the screen: "+ window. screen. availHeight +"
"; StrInfo + =" available workspace width: "+ window. screen. availWidth +"
"; Document. write (strInfo); script

ClientX sets or obtains the x coordinates of the mouse pointer position relative to the client area of the window. The client area does not include the control and scroll bar of the window.
ClientY sets or obtains the y coordinate of the mouse pointer position relative to the client area of the window. The client area does not include the control and scroll bar of the window.
OffsetX is used to set or obtain the x coordinate between the cursor position and the object that triggers the event.
OffsetY is used to set or obtain the y coordinate of the object relative to the trigger event.
ScreenX is used to set or obtain the x coordinates of the cursor position relative to the user's screen.
ScreenY is used to set or obtain the y coordinate of the mouse pointer position relative to the user's screen.
X sets or obtains the x-pixel coordinates of the cursor position relative to the parent document.
Y sets or obtains the y pixel coordinate of the cursor position relative to the parent document.
When event. clientX returns an event, the mouse is the same as the X coordinate of the client window.
However, if the property value of the event object is set to relative, event. clientX remains the same, and event. X returns the coordinates of the event object relative to the ontology.

18. Determine the browser type
Js Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.