现在的位置: 首页 > 综合 > 正文

js note I

2013年10月16日 ⁄ 综合 ⁄ 共 3107字 ⁄ 字号 评论关闭

设置Textbox文本全选:
HTML控件   <INPUT  onFocus=this.select()   type="text"   value="asdf">
服务器控件 TextBox1.Attributes.Add("onFocus", "this.select()");
----------------------------------------
Cookies一词用在程序设计中是一种能够让网站服务器把少量数据储存到客户端的硬盘或内存,或是从客户端的硬盘读取数据的一种技术。当你浏览某网站时,由Web服务器置于你硬盘上的一个非常小的文本文件,它可以记录你的用户ID、密码、浏览过的网页、停留的时间等信息。当你再次来到该网站时,网站通过读取Cookies,得知你的相关信息,就可以做出相应的动作,如在页面显示欢迎你的标语,或者让你不用输入ID、密码就直接登录等等。
escape()和unescape().//字符串处理函数
例子---将姓名保存为一个cookie:
function setCookie()
{
   var the_name = prompt("What's your name?","");
    var the_cookie ="wm_javascript=username:" + escape(the_name);
    document.cookie =the_cookie;
   //编写多重cookies
   //var the_cookie0 ="wm_javascript0=username:" + escape(the_name+"000");
   //document.cookie =the_cookie0;
}
function readCookie()
{
    var the_cookie = document.cookie;
    var broken_cookie = the_cookie.split(":");
    var the_name = broken_cookie[1];
    var the_name = unescape(the_name);
    alert("Your name is: " + the_name);
}
    基本的cookie常常在用户关闭他的浏览器时会被自动删除.有时候这样最好因为通常的域只允许在用户的机器上保留20个cookie.但是如果你希望将cookie保存在用户的机器上你需要设置一个cookie失效的时间,它的格式是一种叫做GMT的特殊格式.
例如:  Mon, 27-Apr-1998 00:00:00 GMT
Javascript 方法--toGMTString
var the_date = new Date("December 31, 2023");
var the_cookie_date =the_date.toGMTString();
function setCookie()
{
    var the_name = prompt("What's your name?","");
    var the_date = new Date("December 31, 2023");
    var the_cookie_date =the_date.toGMTString();
    var the_cookie = "my_cookie=" + escape(the_name);
    the_cookie = the_cookie +";expires=" + the_cookie_date;
    document.cookie = the_cookie;
}
----------------------------------------
给事件定时
设置定时  setTimeout()  取消定时 clearTimeout()
var the_timeout = setTimeout("some javascript  statement", some_number_of_milliseconds);
在setTimeout中用到的第1个变量是一个JavaScript 语句的字符串。setTimeout返回一个值。想取消该setTimeout的定时,你只需引用该变量即可。
例:
var the_timeout = setTimeout("alertAndRedirect ();",3000);
function alertAndRedirect()
{
 alert('ok!  exhale!');
 window.location.replace("timing.htm");//window.location获取WEB的路径
}
clearTimeout(the_timeout);//取消定时
//使用递归设置无限循环定时时钟
var the_count = 0;
var the_timeout;
function doTimer()
{
 window.document.timer_form.the_text.value = the_count;
 the_count += 2;
 the_timeout = setTimeout("doTimer();", 2000);
}
给定时器加入变量 注意:
function alertInAMinute()
{
 var the_string = "hello";
 the_timeout = setTimeout("alert(" + the_string + ");",60000);
}
----------------------------------------
识别用户的浏览器
每种浏览器除了有Document(文件)对象之外还有Navigator对象,Navigator有两个属性包含着你所使用的浏览器类型的所有信息:appName and appVersion
appName是浏览器的类型    appVersion是浏览器的版本
function browserSizeUp()
{
var browser = navigator.appName;
var version = versionNumber();
var the_string = browser + " " + version;
if ((browser  == "Netscape" || browser == "Microsoft Internet Explorer") &&
(version >= 4))
{
alert("The browser doctor says: " + the_string + "?  Now that's a beautiful browser!");
} else {
alert("The browser doctor says: " + the_string + ", Hmm. Maybe it's time to upgrade.");
}
}
function versionNumber() {
  return parseFloat(navigator.appVersion)
}
----------------------------------------
History包含着你的浏览器访问过的URL的记录列表.History对
象使你能利用window.history.back()和
window.history.forward()在列表各项记录中来回切换。back
的功能和你的浏览器上的back按钮的作用是一致的。
----------------------------------------
----------------------------------------
----------------------------------------
----------------------------------------

 

 

 

 

 

 

 

 

 

 

抱歉!评论已关闭.