JavaScript 对象&& window

来源:互联网 发布:vs code php 开发 编辑:程序博客网 时间:2024/06/05 05:01

//实现两个数组的合并 concat

<body>

       <script>

              var mycars =new Array();

              mycars[0]= "Saab";

              mycars[1]= "Volvo";

              mycars[2]= "BMW";

              var my =new Array();

              my[0]= "Saab";

              my[1]= "Volvo";

              my[2]= "BMW";

              document.write(mycars.concat(my));

       </script>

</body>

//实现将数组中的内容转换为字符串格式

<body>

       <script>

              var mycars =new Array();

              mycars[0]= "Saab";

              mycars[1]= "Volvo";

              mycars[2]= "BMW";

              document.write(mycars.join());

              document.write("</br>");

              document.write(mycars.join("."));

       </script>

</body>

//实现对数组文本内容的排序

<body>

       <script>

              var mycars =new Array();

              mycars[0]= "Saab";

              mycars[1]= "Volvo";

              mycars[2]= "BMW";

              mycars[3]= "AudiA4L";

              document.write(mycars);

              document.write("</br>");

              document.write(mycars.sort());

       </script>

</body>

//实现对数组的数字排序

<body>

       <script>

              function sortNumber(a, b) {

                     return a - b;

              }

              var mycars =new Array();

              mycars[0]= "1";

              mycars[1]= "10";

              mycars[2]= "20";

              mycars[3]= "7";

              document.write(mycars);

              document.write("</br>");

              document.write(mycars.sort(sortNumber));

       </script>

</body>

//创建了boolean false

<script>

              var myBoolean =new Boolean();

              document.write(myBoolean);

              document.write("</br>");

             

              var myBoolean =new Boolean(0);

              document.write(myBoolean);

              document.write("</br>");

             

              var myBoolean =new Boolean(null);

              document.write(myBoolean);

              document.write("</br>");

             

              var myBoolean =new Boolean("");

              document.write(myBoolean);

              document.write("</br>");

             

              var myBoolean =new Boolean(false);

              document.write(myBoolean);

              document.write("</br>");               

       </script>

//实现了boolean true

<script>

              var myBoolean =new Boolean(1);

              document.write(myBoolean);

              document.write("</br>");

             

              var myBoolean =new Boolean(true);

              document.write(myBoolean);

              document.write("</br>");

             

              var myBoolean =new Boolean("true");

              document.write(myBoolean);

              document.write("</br>");

             

              var myBoolean =new Boolean("false");

              document.write(myBoolean);

              document.write("</br>");

             

              var myBoolean =new Boolean("Bill Gates");

              document.write(myBoolean);

              document.write("</br>");

                           

       </script>

//实现四舍五入

<body>

       <script>

              document.write(Math.round(0.60)+"</br>");

              document.write(Math.round(0.50)+"</br>");

              document.write(Math.round(0.49)+"</br>");

              document.write(Math.round(-4.40)+"</br>");

              document.write(Math.round(-4.60)+"</br>");

       </script>

</body>

//随机数 0-1

<body>

       <script>

              document.write(Math.random());

       </script>

</body>

//比大小

<body>

       <script>

              document.write(Math.max(10,11));

              document.write("</br>")

              document.write(Math.min(10,11));

       </script>

</body>

//正则表达式,g代表找到所有前面元素var regex =new RegExp("e","g");

需要一个do循环,直到最后找不到e就会返回null

<body>

       <script>

              var regex =new RegExp("e");

              document.write(regex.test("the world is very beautiful,i am very jealous")+"</br>")

              document.write(regex.exec("the world is verybeautiful, i am very jealous")+"</br>")

              regex.compile("z");

              document.write(regex.test("the world is verybeautiful, i am very jealous")+"</br>")

       </script>

</body>

//window中的方法

<body>

       <script>

              var h = window.innerHeight;  //获取浏览器的高

              document.write(h);

              document.write("</br>");

              document.write(window.innerWidth);//获取浏览器的宽

              //window.open();  //打开新窗口

              //window.close();  //关闭当前窗口

              //window.moveTo();  //移动当前窗口

              //window.resizeTo();  //调整当前窗口的尺寸

       </script>

</body>

//返回可用屏幕的宽高

<script>

              document.write("可用屏幕宽度:"+screen.availWidth);

              document.write("</br>");

              document.write("可用屏幕高度:"+screen.availHeight);

</script>

//JS Window Location

<script>

       document.write(location.href+ "</br>");

       document.write(location.pathname+ "</br>");

       function newDoc() {

              window.location.assign("http://tmooc.cn/web/index_new.html?tedu");

       }

</script>

</head>

<body>

       <inputtype="button"value="加载新文档"onclick="newDoc()">

</body>

1.     实现了返回当前当前页面的URL

2.     实现了返回URL的路径名

3.     加载新的文档

 

//实现加载到历史列表中的下一个URL

<script>

    function goBack(){

        window.history.back();

    }

    function goForward(){

        window.history.forward();

    }

   

</script>

</head>

<body> 

    <inputtype="button"value="back"onclick="goBack()">

    <inputtype="button"value="forward"onclick="goForward()">

</body>

//

<body>

    <divid="example"></div>

    <script>   

        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;

    </script>

</body>

来自 navigator对象的信息具有误导性,不应该被用于检测浏览器版本,这是因为:

1.navigator 数据可被浏览器使用者更改

2.浏览器无法报告晚于浏览器发布的新操作系统

navigator是在JavaScript中的一个独立的对象,他用于提供用户所使用的浏览器以及操作系统等信息,以navigator对象属性的形式来提供。

 

//alert&& confirm && prompt

<script>

    function onalert(){

        alert("我爱王莹");

    }

    function onconfirm(){

        confirm("我爱王莹");

    }

    function onprompt(){

        prompt("我爱王莹","");

    }

</script>

</head>

<body> 

    <inputtype="button"value="alert"onclick="onalert()">

警告框经常用于确保用户可以得到某些信息。

当警告框出现后,用户需要点击确定按钮才能继续进行操作。

 

    <inputtype="button"value="confirm"onclick="onconfirm()">

确认框用于使用户可以验证或者接受某些信息。

当确认框出现后,用户需要点击确定或者取消按钮才能继续进行操作。

如果用户点击确认,那么返回值为 true。如果用户点击取消,那么返回值为false。

 

    <inputtype="button"value="prompt"onclick="onprompt()">

提示框经常用于提示用户在进入页面前输入某个值。

当提示框出现后,用户需要输入某个值,然后点击确认或取消按钮才能继续操纵。

如果用户点击确认,那么返回值为输入的值。如果用户点击取消,那么返回值为null。

</body>

 

//实现5s弹出警示框

<script>

    function timedMsg(){

        var x = setTimeout("alert('5 second')",5000);

    }

</script>

</head>

<body>

    <inputtype="button"value="我爱王莹"onclick="timedMsg()">

</body>

//实现文本框中的值自增加

<scripttype="text/javascript">

    var c = 0

    var t

    function timedCount() {

        document.getElementById('txt').value =c

        c= c + 1

        t= setTimeout("timedCount()", 1000)

    }

</script>

</head>

<body>

    <form>

        <inputtype="button"value="Startcount!"onClick="timedCount()">

        <inputtype="text"id="txt">

    </form>

</body>

 

//实现计数暂停的办法

加一个button 加一个方法clearTimeout(t);

 

//什么是cookie?

cookie 是存储于访问者的计算机中的变量。每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie。你可以使用 JavaScript 来创建和取回 cookie 的值。

//实现了当用户从没有登陆的时候要求用户输入提示,如果用户登陆过的时候就提示欢迎您

<scripttype="text/javascript">

    functiongetCookie(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

                returnunescape(document.cookie.substring(c_start, c_end))

            }

        }

        return""

    }

 

    functionsetCookie(c_name, value, expiredays) {

        var exdate =new Date()

        exdate.setDate(exdate.getDate()+ expiredays)

        document.cookie= c_name

                +"="

                +escape(value)

                +((expiredays == null)?"": ";expires="

                        + exdate.toGMTString())

    }

 

    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)

            }

        }

    }

</script>

</head>

<bodyonLoad="checkCookie()">

</body>

 

0 0
原创粉丝点击