JavaScript自学代码--(四)

//JavaScript Window - 浏览器对象模型
window.document.getElementById("header");
//等价于
document.getElementById("header");
/*
Window 尺寸
有三种方法能够确定浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)。
对于Internet Explorer、Chrome、Firefox、Opera 以及 Safari:
window.innerHeight - 浏览器窗口的内部高度
window.innerWidth - 浏览器窗口的内部宽度
对于 Internet Explorer 8、7、6、5:
document.documentElement.clientHeight
document.documentElement.clientWidth
或者
document.body.clientHeight
document.body.clientWidth
实用的 JavaScript 方案(涵盖所有浏览器):
* */
function get_Browser(){
    var w=window.innerWidth
    || document.documentElement.clientWidth
    || document.body.clientWidth;

    var h=window.innerHeight
    || document.documentElement.clientHeight
    || document.body.clientHeight;

    x=document.getElementById("demo");
    x.innerHTML="Browser inner window width: " + w + ", height: " + h + "."
}
/*
其他 Window 方法
一些其他方法:
window.open() - 打开新窗口
window.close() - 关闭当前窗口
window.moveTo() - 移动当前窗口
window.resizeTo() - 调整当前窗口的尺寸
* */
function Screen(){
    //可用宽度
    document.write("Available Width: " + screen.availWidth);
    //可用高度
    document.write("Available Height: " + screen.availHeight);
    //    Window Location Pathname
    //location.pathname 属性返回 URL 的路径名。
    document.write(location.pathname);
    //location.assign() 方法加载新的文档。
    function newDoc()
    {
        window.location.assign("www.baidu.com");
        //window.history 对象包含浏览器的历史。
        window.history.back();  //返回
        /*
        <html>
        <head>
        <script>
        function goBack()
          {
          window.history.back()
          }
        </script>
        </head>
        <body>

        <input type="button" value="Back" onclick="goBack()">

        </body>
        </html>
        */

        window.history.forward();//前进
        /*
        <html>
        <head>
        <script>
        function goForward()
          {
          window.history.forward()
          }
        </script>
        </head>
        <body>

        <input type="button" value="Forward" onclick="goForward()">

        </body>
        </html>
         */
        //window.navigator 对象包含有关访问者浏览器的信息。
        function Get_Browser(){
            txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
            txt+= "<p>Browser Name: " + navigator.appName + "</p>";
            txt+= "<p>Browser Version: " + navigator.appVersion + "</p>";
            txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
            txt+= "<p>Platform: " + navigator.platform + "</p>";
            txt+= "<p>User-agent header: " + navigator.userAgent + "</p>";
            txt+= "<p>User-agent language: " + navigator.systemLanguage + "</p>";
            document.getElementById("example").innerHTML=txt;
            /*
            来自 navigator 对象的信息具有误导性,不应该被用于检测浏览器版本,这是因为:
            navigator 数据可被浏览器使用者更改
            一些浏览器对测试站点会识别错误
            浏览器无法报告晚于浏览器发布的新操作系统
             */
            //JavaScript 弹窗
            //警告框
            //警告框经常用于确保用户可以得到某些信息。
            //当警告框出现后,用户需要点击确定按钮才能继续进行操作。
            function myFunction(){
                alert("Confirm!");
            }
            /*
            确认框
            确认框通常用于验证是否接受用户操作。
            当确认卡弹出时,用户可以点击 "确认" 或者 "取消" 来确定用户操作。
            当你点击 "确认", 确认框返回 true, 如果点击 "取消", 确认框返回 false。
             */
            function myFunction()
            {
            var x;
            var r=confirm("Press a button!");
            if (r==true)
            {
                  x="You pressed OK!";
            }
            else
            {
                 x="You pressed Cancel!";
            }
                document.getElementById("demo").innerHTML=x;
            }
            /*
            提示框
            提示框经常用于提示用户在进入页面前输入某个值。
            当提示框出现后,用户需要输入某个值,然后点击确认或取消按钮才能继续操纵。
            如果用户点击确认,那么返回值为输入的值。如果用户点击取消,那么返回值为 null。
             */
            function myFunction()
            {
            var x;
            var person=prompt("请输入你的名字","Harry Potter");
            if (person!=null)
              {
              x="你好 " + person + "! 今天感觉如何?";
              document.getElementById("demo").innerHTML=x;
              }
            }
            /*
            JavaScript 计时事件
            通过使用 JavaScript,我们有能力作到在一个设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行。我们称之为计时事件。

            在 JavaScritp 中使用计时事件是很容易的,两个关键方法是:

            setInterval() - 间隔指定的毫秒数不停地执行指定的代码。
            setTimeout() - 暂停指定的毫秒数后执行指定的代码
            Note: setInterval() 和 setTimeout() 是 HTML DOM Window对象的两个方法。
             */
            //每三秒弹出 "hello" :
            setInterval(function(){alert("Hello")},3000);

            //动态时钟显示
            var myVar=setInterval(function(){myTimer()},1000);
            function myTimer()
            {
            var d=new Date();
            var t=d.toLocaleTimeString();
            document.getElementById("demo").innerHTML=t;
            }

        }
    }

}

            //clearInterval() 方法用于停止 setInterval() 方法执行的函数代码。
            function StopTime(){
                var myVar=setInterval(function(){myTimer()},1000);
                function myTimer()
                {
                    var d=new Date();
                    var t=d.toLocaleTimeString();
                    document.getElementById("demo").innerHTML=t;
                }
                function myStopFunction()
                {
                     clearInterval(myVar);
                }
            }
//JavaScript 可以使用 document.cookie 属性来创建 、读取、及删除 cookies。
//创建cookies
document.cookie = "userName = Jone";
//您还可以为 cookie 添加一个过期时间(以 UTC 或 GMT 时间)。默认情况下,cookie 在浏览器关闭时删除:
document.cookie = "username = jone;expres = Thu,18,Dec 2013 12:00:00 GMT";
//您可以使用 path 参数告诉浏览器 cookie 的路径。默认情况下,cookie 属于当前页面。
document.cookie="username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 GMT; path=/";

//使用 JavaScript 读取 Cookie
var x = document.cookie;
//document.cookie 将以字符串的方式返回所有的 cookies,类型格式: cookie1=value; cookie2=value; cookie3=value;

//使用 JavaScript 修改 Cookie
//在 JavaScript 中,修改 cookies 类似于创建 cookies,如下所示:
document.cookie="username=John Smith; expires=Thu, 18 Dec 2013 12:00:00 GMT; path=/";
//旧的 cookie 将被覆盖。
//使用 JavaScript 删除 Cookie
//删除 cookie 非常简单。您只需要设置 expires 参数为以前的时间即可,如下所示,设置为 Thu, 01 Jan 1970 00:00:00 GMT:
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
//注意,当您删除时不必指定 cookie 的值。

//Cookies操作函数
function setCookie(cname,cvalue,exdays)
{
    var d = new Date();
    d.setTime(d.getTime()+(exdays*24*60*60*1000));
    var expires = "expires="+d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

function getCookie(cname)
{
    var name = cname + "=";
    var ca = document.cookie.split(‘;‘);
    for(var i=0; i<ca.length; i++)
      {
      var c = ca[i].trim();
      if (c.indexOf(name)==0) return c.substring(name.length,c.length);
}
    return "";
}

function checkCookie()
{
    var user=getCookie("username");
    if (user!="")
      {
      alert("Welcome again " + user);
      }
    else
      {
      user = prompt("Please enter your name:","");
      if (user!="" && user!=null)
        {
        setCookie("username",user,365);
        }
      }
}
时间: 2024-08-26 15:33:03

JavaScript自学代码--(四)的相关文章

JavaScript学习总结(四)——jQuery插件开发与发布

JavaScript学习总结(四)--jQuery插件开发与发布 目录 一.插件开发基础 1.1.$.extend 1.1.1.扩展属性或方法给jQuery 1.1.2.扩展对象 1.2.$.fn.extend 1.3.$.fn 二.插件开发 2.1.jQuery插件开发基本模式 2.2.获取上下文 2.3.第一个jQuery插件 2.4.链式编程 2.5.参数与默认值 2.5.1.默认值 2.5.2.参数对象 2.5.2.参数类型 2.6.命名空间与面向对象 2.7.插件与关联的CSS 2.8

浅谈javascript继承【读javascript设计模式第四章节继承有感】

javascript继承,无任是类式继承,原型式继承还是渗元式继承都是通过不同方法去围绕着prototype转,简单分析下三种不同继承方法是如何围绕prototype转的 一:类似继承,先上关键代码 function extend(subClass,supClass){ var fn = function(){}; fn.prototype = supClass.prototype; subClass.prototype = new fn(); subClass.prototype.constr

javascript常用代码大全

jquery选中radio //如果之前有选中的,则把选中radio取消掉 $("#tj_cat .pro_category").each(function() { if ($(this).attr('checked')){ $(this).attr('checked' ,false); } }); //获取被选中的标签的值 radio:(checkbox和这个一样) var val=$('input:radio[name="sex"]:checked').val(

JavaScript基础(四)

JavaScript基础(四) 冒泡排序 // 准备一个需要进行排序的数组 var arr = [12, 88, 154, 23, 32, 15, 72, 2, 1, 66]; // 根据分析出的规律进行代码实现 // - 外层循环控制轮数:length - 1 for (var i = 0; i < arr.length - 1; i++) { // - 内层循环控制每轮比较次数: // - 设置条件时发现只 - i 不够,少了1,再设置一个 - 1 凑数 for (var j = 0; j

CodeMirror:基于JavaScript的代码编辑器

官方网站定义: http://codemirror.net/ CodeMirror is a versatile text editor implemented in JavaScript for the browser. It is specialized for editing code, and comes with a number of language modes and addons that implement more advanced editing functionalit

JavaScript的代码库

JavaScript的代码库 本文主要是汇集了一些JavaScript中一些经常使用代码.方便以后查找和复用. javascript框架: <script language="javascript" type="text/javascript"> //javascript代码 </script> JS中自己定义函数的定义: function 函数名() { //函数体 } 三大结构: 1.顺序结构: 2.选择结构: if(结果为布尔类型的表达

写规范的javascript脚本代码 之单var

在函数顶部使用单var语句是比较有用的一种形式,其好处在于: 提供了一个单一的地方去寻找功能所需要的所有局部变量 防止变量在定义之前使用的逻辑错误 帮助你记住声明的全局变量,因此较少了全局变量//zxx:此处我自己是有点晕乎的- 少代码(类型啊传值啊单线完成) 单var形式长得就像下面这个样子: ? 1 2 3 4 5 6 var a=0,       b=3,      c=a+b,     myobject = {},        i,        j; 您可以使用一个var语句声明多个

添加浮动按钮点击滚动到网页底部的纯JavaScript演示代码 IE9、11,Maxthon 1.6.7,Firefox30、31,360极速浏览器7.5.3.308下测试正常

这两天想在网页中添加一个添加浮动按钮,点击该按钮滚动则到网页底部.在网上bing搜索了一下,大多是JQuery的. 我想要纯JavaScript的,只好DIY了.在IE9.11,Maxthon 1.6.7,Firefox30.31,360极速浏览器7.5.3.308下测试正常. 其中难点在于,setScrollBottom这个函数. 按常规写法: function setScrollBottom(value) { if (document.documentElement.scrollTop){

自己写的一点福利代码(四)

自己写的一点福利代码(四) 作者:vpoet 注:这个系列我只贴代码,代码不注释.有兴趣的自己读读就懂了,纯属娱乐,望管理员抬手 若有转载一定不要注明来源 #coding:utf-8 import requests import urllib2 import re import time def UpFun(Article_Id): url = 'http://blog.csdn.net/u013018721/article/digg?ArticleId=%s' % str(Article_Id