Home  >  Article  >  Web Front-end  >  Summarize some js custom functions_javascript skills

Summarize some js custom functions_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:27:16885browse

1. dayin()
Function: Create a new page and print the content with the id of dayin, which can solve the problem of printing part of the content on a page.
Usage: Include the content to be printed by , and then define the
event


function dayin()
{
var code=""
code =document.all.dayin.innerHTML;
code=code.toUpperCase();
code=code.replace(/]*>Delete code=code.toLowerCase();
var newwin=window.open('','','');
newwin.opener = null;
newwin.document.write(code);
newwin.document.close();
}


where code=code.replace(/] *>Delete/gi, "");
is to filter out all deleted connections in the content

2. isNumber(st)
Function: Determine whether the variable st is It consists of numbers (including negative numbers and decimals). If it is, it returns true, otherwise it returns false.
function isNumber(st)
{
var Letters = "1234567890-.";
var i;
var c;
if(st.charAt( 0 )==' .')
return false;
if(st.charAt( 0 )=='-'&&st.charAt( 1 )=='.')
return false;
if( st. charAt( st.length - 1 ) == '-' )
return false;
for( i = 0; i < st.length; i )
{
c = st.charAt ( i );
if (Letters.indexOf( c ) < 0)
return false;
}
return true;
}


3. createCookie(name,value,days)
Function: Create a cookie with name name, value value, and validity period of days. Can be modified at the same time.
function createCookie(name,value,days){
var expires = "";
if (days) {
var date = new Date();
date.setTime(date. getTime() (days*24*60*60*1000));
expires = "; expires=" date.toGMTString();
};
document.cookie = name "=" value expires "; path="/";
};

4. readCookie(name)
Function: Read the value of the cookie based on the name. If none, returns null.
function readCookie(name){
var nameEQ = name "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i ) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
};
return null;
};

5. request(st)
Function: Get the value of a certain parameter in the browser address bar (not a perfect solution, for example, if there are spaces, you will get it, but it supports
Chinese)
function request(st) {
var ustr=document.location.search;
var intPos = ustr.indexOf("?");
var strRight = ustr.substr(intPos 1);
var arrTmp = strRight.split( "&");
for(var i = 0; i < arrTmp.length; i )
{
var arrTemp = arrTmp[i].split("=");
if (arrTemp[0].toUpperCase() == st.toUpperCase()) return arrTemp[1];
}
return "";
}

6. hideObject(obj)
Function: Hide obj
function hideObject(obj) {
obj.style.display = "none";
}

7. showObject(obj)
Function: Show obj
function showObject(obj) {
obj.style.display = "block";
}

8. trim(str)
Function: remove spaces on both sides of str

function trim(str)
{
return str.replace(/^s*|s*$/g,"");
}

9. function bj_date(d1,d2)
Function: Compare the size of d1,d2 dates
function bj_date(d1,d2)
{
/*
author:wxg
Function: Compare dates Size
Parameters: d1 d2
Character year-month-day type, such as 2005-01-22
Return value: 0/1/2
Numeric type
d1>d2 returns 0
d1=d2 returns 1
d1*/
if(d1==""&&d2==""){
return 3
}
if(d1==""||d2==""){
return 4
}
d1=d1.split("-")
d2=d2.split("-" )
var a = new Date(Number(d1[0]),Number(d1[1]),Number(d1[2]))
var b=new Date(Number(d2[0]) ,Number(d2[1]),Number(d2[2]))
a = a.valueOf()
b=b.valueOf()
if(a-b>0)
return 0
if(a-b==0)
return 1
if(a-b<0)
return 2
}


10. Format Convert numbers into currency format
function setCurrency(s){
if(/[^0-9.-]/.test(s)) return "invalid value";
s=s.replace( /^(d*)$/,"$1.");
s=(s "00").replace(/(d*.dd)d*/,"$1");
s= s.replace(".",",");
var re=/(d)(d{3},)/;
while(re.test(s))
s=s .replace(re,"$1,$2");
s=s.replace(/,(dd)$/,".$1");
return s.replace(/^./,"0 .")
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn