javascript
JS一些常用的类库
一、返回上一頁(history)
發覺有兩種用法:
? ? 1、javascript:history.back(-1);
? ? 2、javascript:history.go(-1);
它們倆的區別是:
? ? history.back(-1):直接返回當前頁的上一頁,數據全部消息,返回新頁面
? ? history.go(-1):也是返回當前頁的上一頁,表單里的數據依然還在
其語法為:
? ? history.go(num);
? ? history. back(num); ( num -- 負數為后退指定的頁數,正數為前進指定的頁數)
? ? 示例:<a href="javascript:history.go(-1);">返回上一頁</a>?
? ? ? ? ? ? 或
? ? ? ? ? <input type="button" value="Back" οnclick="javascript:history.back()" />
二、confirm 確認/取消
var flag=confirm("確定刪除?");//彈出 確認、取消對話框 點擊確認返回true 取消 返回false
? ? ? ? if(flag){
? ? ? ? ? ? ?alert("確定");
? ? ? ? }else{
? ? ? ? ? ? alert("取消");
? ? ? ? }
三、prompt 提示輸入框
var num=prompt("請輸入一個數字",0);//提示輸入框 點擊確定獲得輸入的值 取消返回null
alert(num);
四、open 打開新窗口
window.open("https://www.baidu.com/");//新建頁面打開百度
window.open("https://www.baidu.com/","baidu");//新建頁面并命名窗口 打開百度
window.open("https://www.baidu.com/","_parent");//在本頁面打開百度
window.open("https://www.baidu.com/","_blank");//新建頁面打開百度
window.open("https://www.baidu.com/","baidu","width=500,height=600,top=100,left=200,toolbar=yes");
五、定時任務/間歇任務?
//================定時調用===================
? ? ? ? //直接使用函數傳入的方法,擴展性好,性能更佳。
? ? ? ? window.setTimeout(box,5000);
? ? ? ? function box(){
? ? ? ? ? ? console.info("定時調用====》普通函數");
? ? ? ? }
? ? ? ? //匿名函數法 ECMA推薦使用
? ? ? ? window.setTimeout(function(){
? ? ? ? ? ? ? console.info("定時調用======》匿名函數");
? ? ? ? },2000);
? ? ? ? /*取消定時器
? ? ? ? ? setTimeout 返回一個數值ID,這個ID就是當前這個定時方法的唯一標識符,
? ? ? ? ? 通過它可以取消這個定時調用計劃? ??
? ? ? ? */
? ? ? ? var timeId=setTimeout(function(){
? ? ? ? ? ? alert("取消定時器");
? ? ? ? },3000);
? ? ? ? clearTimeout(timeId);
? ? ? ? //================間歇調用=================
? ? ? ? var intId=setInterval(function(){
? ? ? ? ? ? console.info("間歇調用");
? ? ? ? },3000);
? ? ? ? clearInterval(intId);//取消間歇任務
? ? ? ? /*
? ? ? ? ? 一般在開發環境中很少使用間歇任務,因為需要根據情況來取消ID(間歇任務),
? ? ? ? ? 并可能造成同步的一些問題,建議使用定時任務賴替代間歇任務。
? ? ? ? */
? ? ? ? //使用定時任務模擬間歇任務
? ? ? ? (function(){
? ? ? ? ? ? var num=0;
? ? ? ? ? ? var max=5;
? ? ? ? ? ? function box(){
? ? ? ? ? ? ? ? num++;
? ? ? ? ? ? ? ? if(num===max){
? ? ? ? ? ? ? ? ? ? alert("5秒后結束");
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? setTimeout(box,1000);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? setTimeout(box,1000);
? ? ? ? })();
?六、location 對象
? ? //http://localhost:8080/html/bom.html?a=123#hash
? ? ? ? var port=location.port;//獲取端口號? 8080
? ? ? ? var hash=location.hash;//獲取#和號后面的字符串? #hash
? ? ? ? var hostName=location.hostname;//獲取主機名? localhost
? ? ? ? var pathname=location.pathname;//獲取當前路徑 /html/bom.html
? ? ? ? var protocol=location.protocol;//獲取當前協議 http:
? ? ? ? var search=location.search;//獲取請求參數 ?a=123
? ? ? ? var href=location.href;//獲取當前URL(完整)路徑
? ? ? ?// location.assign("http://www.baidu.com");//跳轉到指定的URL
? ? ? ?// location.replace("http://www.baidu.com");//跳轉到指定的URL 清除之前的歷史記錄
? ? ? ? location.reload();//重新加載? 可能從緩存中加載
? ? ? ? location.reload(true);//重新加載? 從服務器加載
? ? }
? ? //獲取請求參數
? ? function getArgs(keyStr){
? ? ? ? var map=new Object();
? ? ? ? //獲取請求參數
? ? ? ? var searchs=location.search.length>0?location.search.substring(1):"";
? ? ? ? var items=searchs.split("&");
? ? ? ? if(searchs!=""){
? ? ? ? ? ? for(var i=0;i<items.length;i++){
? ? ? ? ? ? ? ? var items_t=items[i].split("=");
? ? ? ? ? ? ? ? map[items_t[0]]=items_t[1];
? ? ? ? ? ? }
? ? ? ? ? ? console.info(map);
? ? ? ? ? ? return map[keyStr];
? ? ? ? }
?
總結
- 上一篇: ceontos7安装mysql5.5_c
- 下一篇: gradle idea java ssm