Tag Archive for 'javascript'

Javascript延时

1
2
3
      setTimeout(function() {
         homes.removeClass('shot');
      }, 30000);

jQuery removeClass

1
2
3
4
$('td[class^="input_"]').removeClass(function (index, class) {
   var matches = class.match (/spf_c\d+/g) || [];
   return (matches.join (' '));
});

编译V8遇到libstdc++问题解决方法

sudo ln -s /usr/lib32/libstdc++.so.6 /usr/lib32/libstdc++.so

 

用的Javascript函数var_dump(),implode()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// 检测是否为整数
function isInteger( str ){
  var regu = /^[-]{0,1}[0-9]{1,}$/;
  return regu.test(str);
}

function implode (glue, pieces) {
    var i = '', retVal='', tGlue='';
    if (arguments.length === 1) {
        pieces = glue;
        glue = '';
    }
    if (typeof(pieces) === 'object') {
        if (pieces instanceof Array) {
            return pieces.join(glue);
        }
        else {
            for (i in pieces) {
                retVal += tGlue + pieces[i];
                tGlue = glue;
            }
            return retVal;
        }
    }
    else {
        return pieces;
    }
}

function print_r(x, max, sep, l) {

  l = l || 0;
  max = max || 10;
  sep = sep || ' ';

  if (l > max) {
    return "[WARNING: Too much recursion]\n";
  }

  var
    i,
    r = '',
    t = typeof x,
    tab = '';

  if (x === null) {
    r += "(null)\n";
  } else if (t == 'object') {

    l++;

    for (i = 0; i < l; i++) {
      tab += sep;
    }

    if (x && x.length) {
      t = 'array';
    }

    r += '(' + t + ") :\n";

    for (i in x) {
      try {
        r += tab + '[' + i + '] : ' + print_r(x[i], max, sep, (l + 1));
      } catch(e) {
        return "[ERROR: " + e + "]\n";
      }
    }

  } else {

    if (t == 'string') {
      if (x == '') {
        x = '(empty)';
      }
    }

    r += '(' + t + ') ' + x + "\n";

  }

  return r;

};
var_dump = print_r;

JS字符截取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<script LANGUAGE="JavaScript">
<!--
String.prototype.Sub = function(n)
{
var r = /[^\x00-\xff]/g;
if(this.replace(r, "mm").length <= n) return this;
n = n - 3;
var m = Math.floor(n/2);
for(var i=m; i<this.length; i++)
{
if(this.substr(0, i).replace(r, "mm").length>=n)
{
return this.substr(0, i) +"...";
}
}
return this;
};
var s = "javascript如何实现字符串截取";
alert(s.Sub(20));
//-->
</script>

Continue reading ‘JS字符截取’

在一个HTML页面弹出窗口

有时候需要在装载一个HTML页面的时候弹出一个简单的HTML页面窗口,但有不想去新建立一个HTML,可以使用JAVASCRIPT在现有的HTML页面中实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<script type="text/javascript">
function openwin() {
  OpenWindow=window.open("", "newwin", "height=250, width=250,toolbar=no,scrollbars="+scroll+",menubar=no");
  OpenWindow.document.write("<TITLE>维护公告</TITLE>")
  OpenWindow.document.write("<BODY style=\"background-image: url(/picsec/bj.gif)\">")
  OpenWindow.document.write("<h1>维护公告</h1>")
  OpenWindow.document.write("因博客数据升级,博客将于今日(07年03月24日)晚上22:00暂停服务30分钟,请各位网友注意调整时间。 ")
  OpenWindow.document.write("</BODY>")
  OpenWindow.document.write("</HTML>")
  OpenWindow.document.close()
}
</script>

</head>
<body onload="openwin()">

这样就实现了 ;)

Javascript 删除确认的方法

1
2
3
4
5
6
7
8
9
10
<SCRIPT LANGUAGE=javascript>
function p_del() {
     var msg = "您真的确定要删除吗?\n\n请确认!";
     if (confirm(msg)==true){
         return true;
     }else{
     return false;
     }
}
</SCRIPT>

在HTML中调用

1
<a href="del.jsp?id=<%=id%>" onclick="javascript:return p_del()">删 除</a>

Javascript document.write

对于javascript的初学者来说,document.write 是一个非常实用的函数,它可以把Javascript中的变量等写入到HTML上

1
2
3
4
5
<script type="text/javascript">
var name = "Hege"
document.write(name)
document.write("<h1>"+name+"</h1>")
</script>

JavaScript Cookies

What is a Cookie?
A cookie is a variable that is stored on the visitor’s computer. Each time the same computer requests a page with a browser, it will send the cookie too. With JavaScript, you can both create and retrieve cookie values.

Examples of cookies:

  • Name cookie – The first time a visitor arrives to your web page, he or she must fill in her/his name. The name is then stored in a cookie. Next time the visitor arrives at your page, he or she could get a welcome message like “Welcome John Doe!” The name is retrieved from the stored cookie
  • Password cookie – The first time a visitor arrives to your web page, he or she must fill in a password. The password is then stored in a cookie. Next time the visitor arrives at your page, the password is retrieved from the cookie
  • Date cookie – The first time a visitor arrives to your web page, the current date is stored in a cookie. Next time the visitor arrives at your page, he or she could get a message like “Your last visit was on Tuesday August 11, 2005!” The date is retrieved from the stored cookie

Create and Store a Cookie
In this example we will create a cookie that stores the name of a visitor. The first time a visitor arrives to the web page, he or she will be asked to fill in her/his name. The name is then stored in a cookie. The next time the visitor arrives at the same page, he or she will get welcome message.

First, we create a function that stores the name of the visitor in a cookie variable:

1
2
3
4
5
6
7
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 parameters of the function above hold the name of the cookie, the value of the cookie, and the number of days until the cookie expires.

In the function above we first convert the number of days to a valid date, then we add the number of days until the cookie should expire. After that we store the cookie name, cookie value and the expiration date in the document.cookie object.

Then, we create another function that checks if the cookie has been set:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 ""
}

The function above first checks if a cookie is stored at all in the document.cookie object. If the document.cookie object holds some cookies, then check to see if our specific cookie is stored. If our cookie is found, then return the value, if not – return an empty string.

Last, we create the function that displays a welcome message if the cookie is set, and if the cookie is not set it will display a prompt box, asking for the name of the user:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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)
    }
  }
}

All together now:
Continue reading ‘JavaScript Cookies’

JavaScript的Cookie操作函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//获得Cookie解码后的值
function GetCookieVal(offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
  endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}


//设定Cookie值
function SetCookie(name, value) {
  var expdate = new Date();
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
  document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))
  +((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
  +((secure == true) ? "; secure" : "");
}


//删除Cookie
function DelCookie(name) {
  var exp = new Date();
  exp.setTime (exp.getTime() - 1);
  var cval = GetCookie (name);
  document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
}


//获得Cookie的原始值
function GetCookie(name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen)
  {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
    return GetCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}