JS build wheel, basic article

Source: Internet
Author: User

In JS, as long as the function is not declared inside the function is a global variable, if the code is large in the case of the global variable pollution is very scary, so need to build wheels to declare their own variables and their own global variables and function methods

One, declare an object

Simply declare an object tool={} so that you can do so, so that a simple global object is ready.

Second, declaring methods and variables

This is how you define methods and variables.

1Window.tool = {}2Window.tool.cip =Localstorage.cip;3 4 //URL5Tool.urlheader = '';6 7Tool.urlheader2 = '';8 9Tool.keydownenter =function(text, clickbtn) {Ten$ (text). On ("KeyDown",function(e) { One         if(E.keycode = = 13) { A $ (CLICKBTN). Click (); -         } -     }) the}

This way of definition and is not a global, oh, not at the time of the code to be quoted, so as to effectively prevent the pollution of global variables

Three, currently I encapsulate some of the useful methods and examples


Gets the URI of datatool.getquerystring = function (name) {var reg = new RegExp ("(^|&)" + name + "= ([^&]*) (&|$)"); var r = Window.location.search.substr (1). Match (reg); if (r = null) return unescape (r[2]); return null;} Get the ENTER key and click the button tool.keydownenter = function (text, CLICKBTN) {$ (text). On ("KeyDown", function (e) {if (E.keycode = = 13) {$ ( CLICKBTN). Click ();}})} Determines whether the value exists in the array tool.arrayindexof=function (Arr,sel) {for (Var F1 in arr) {if (Arr[f1].indexof (SEL) >-1) {return true;}} return false;} A simple and practical ajaxtool for encapsulation. Ajax = function (type, URL, Funcs, header, data, async,beforesend) {tool.setinfo (), if (type = = ' get ') $.ajax ({type: "Get", Da Tatype: "JSON", ContentType: ' Application/json ', url:url,headers:header = = null? Tool.headers:header,success:funcs,error:function (data) {Console.log (data); Console.log (localstorage.us)}, Beforesend:beforesend,async:async = = null? True:async}); Else$.ajax ({type: "Post", DataType: "JSON", ContentType: ' Application/json ', Url:url,data:data, Beforesend:beforesend, Headers:header = = null? Tool.headers:header,success:funcs,error:function (data) {Console.log (data) Console.log (localstorage.us)},async: Async = = null? True:async});} Using min and Max to generate random numbers, round is rounding Tool.getrandom=function (min,max,round) {if (round) {return Math.Round (Math.random () * ( max-min)) +min;} Else{return (Math.random () * (max-min)) +min;}      Allow class=double text box to only enter numbers and decimal points $ (". Double"). Bind ("KeyPress", function (event) {var event= event | | window.event;      var GetValue = $ (this). Val ();      Control first cannot enter a decimal point "."          if (Getvalue.length = = 0 && Event.which = =) {Event.preventdefault ();      Return      }//control can only enter a decimal point "."        if (Getvalue.indexof ('. ')! =-1 && Event.which = =) {Event.preventdefault ();      Return }//control can only enter the value if (Event.which && (Event.which < | | Event.which > $) && Event.which! = 8 &           amp;& Event.which! =) {Event.preventdefault ();          Return }      })    //The loss of focus is triggered by $ (". Double"). Bind ("Blur", function (event) {var value = $ (this). Val (), Reg =/\.$/;      if (reg.test (value)) {value = Value.replace (Reg, "");        $ (this). Val (value);             }})//Allow Class=number text box to enter only the number $ ("."). KeyPress (function (event) {var keycode = Event.which;             if (keycode >= && keycode <=57) return true;         else return false;            }). focus (function () {this.style.imemode= ' disabled '; });


JS build wheel, basic article

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.