Some JavaScript topics

Source: Internet
Author: User
Tags html form

Q: How can I copy the contents of an array to another array? A:jscript.array.copyarray = function (Issrcarray,indestarray) {var i; for (i = 0; i < insrcarray.length; i++) {Indestar Ray.push (Insrcarray[i]); } return Indestarray;} End Copyarray () adds each element of the Insrcarry to the Indestarry. Returns Indestarray. Q: How do I find the specified element in an array? Suppose the user enters a series of values in the page. It seems reasonable to put them in an array.. A:jscript.array.findinarray = function (inarray,invalue) {var i; for (i = 0;i<inarray.length;i++) {if (inarray[i] = = InV Alue) {return i;}} return-1;} Iterates over all the values of the array equal to the value we are looking for, and returns the subscript value. Could not find return-1. This is almost any type of lookup in the "No, not found" common return value. Q: Suppose I have an array, how do I calculate the average of all the values in an array?  A:jscript.array.arrayaverage = function (InArray) {var accmulatot = 0; var i = 0; for (i = 0; i< inarray.length;i++) { Accmulator + = Inarray[i]; } return accmulator/inarray.length;} Accumulates all the values in the array, divided by the length. Q: How do I get the information for the browser that is using the application? a:jscript.browser.getbrowseridentity = function () {return navigator.appname + "" + navigator.appversion;} Returns a string that consists of the name and version of the browser. Q: How can I easily know how many days a month is given? For example, user input 31. I need to make sure the input month is 31 days? A:jscript.datetime.getnumberdaysinmonth = function (inmonth,inyear) {inmonth = inMonth-1; var leap_year = This.inleapyea R (Inyear);  if (leap_year) {leap_year = 1;} else {leap_year = 0;} if (Inmonth = = 3 | | inmonth = = 5 | | inmonth = = 8 | | Inmonth ==10) {return,} else if (Inmonth = = 1) {return + Leap_year;} else {return 31;}} Determine if the input year is a leap years, to achieve this function, we will write a function called isleapyear () for detection. February of the leap year by 29 days. We have to remember April, June, September. November has 31 days.

Jscript.datetime.isLeapYer = function (inyear) {

if ((inyear% 4 = = 0 &&! Inyear%100==0)) | | Inyear%400 ==0)

{

return true;

}

else {return false;}

}

If the year can be divisible by 4 and cannot be divisible by 100, or it can be divisible by 400, then it is leap years. Q: How do i show all the properties of an arbitrary object and their values? Jscript.debug.enumProps = function (inobj) {var props = "", var i;for (i in inobj) {props + = i + "=" + inobj[i] + "\ n";} alert (prop);} Q: How to implement a robust automatic logging mechanism? Similar to Jakarta comons Logging? I often find that I want to put some log information in the code, Q: How can I center an arbitrary DOM element? When a form is submitted, the application pops up a "please wait" message. It is just a z-index set to a large number of <div&gt, so it can be displayed on top of everything else. Unfortunately, the temporary worker who wrote the code did not know how to center the <div>. So it is usually in the upper left corner. Jscript.dom.layerCenterH = function (inobj) {var lca;var lcb;var lcx;var Iebody;var dsocleft;if (window.innerwidth) {LCA = Window.innerwidth;} else{LCA = Document.body.clientWidth;} LCB = INOBJ.OFFSETWIDTH;LCX = (Math.Round (LCA/2))-(Math.Round (LCB/2)); iebody = (Document.compatmode && Document.compatmode! = "Backcompat")? Dscoleft =document.all? Iebody.scrollleft:window.pagexoffset;inobj.style.left = lcx +dsocleft + "px";} Q: How can I refer to any number of DOM elements? a:jscript.dom.getdomelements = function () {if (arguments.length = = 0) {return null;} if (Arguments.length ==1) {return document.getElementById (Arguments[0]);} var elems = new Array (), for (var i = 0;i<arguments.length;i++) {Elems.push (document.getElementById (arguments[i])) '} return elems;} This function accepts a variable number of arguments. Q: How do I generate an XML from an HTML form? The 86-page code is not excerpted. Q:select improve the full selection function? A:jscript.form.selectselectall = function (inselect) {if (Inselect = = NULL | |!inselect.options | | inselect.options.length==0) {return;} var i;for (i = 0;i<inselect.options.length;i++) {inselect.options[i].selected =true;}} Q: How do I get the properties of an object and copy them to another object? A:jscript.lang.copyproperties = function (insrcobj,indestobj,inoverride) {var prop;for (prop in inscrobj) {if ( Inoverride | | !omdestobj[prop]) {Indestobj[prop] = Insrcobj[prop];}} return indestobj;} Use the For ... in loop to iterate through the properties of the Insrcobj. For each property, see if it already exists in indestobj, if present, by passing in true as the value of the Inoverride parameter, to see if the bar is used to overwrite an existing property. If it exists yes, we rewrite it, and if it does not exist, we use the array notation to set the value of the Indestobj property. Q: How can I generate random numbers within a specified range? A:jscript.math.genrandomnumber = function (Inmin,inmax) {if (Inmin >inmax) {return 0;} return inmin + (inmax-inmin) *math.random ();} Q: How do I use JavaScript to break a framework? It's just not the iframeset!. A:jscript.page.breakoutoffframes = function () {if (self!=top) {top.location = self.location;}}

Some JavaScript topics

Related 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.