iT邦幫忙

1

【物件導向】跳轉網址插件 - javascript系列課程Days-2 (Api-插件開發)-(無需依賴jQuery)

作者:pratnket
版本:2017_Var3

javascript系列課程Days-1
javascript系列課程Days-2

JS頁
---###變數說明###---
Home 預設頁面
Cookie 預設cookie名稱

---###方法說明###---

修改設定值
	Redirect.config({
		Home:"index.php",
		Cookie:"Href_Url"
	});

儲存 Cookie
	Redirect.cookie_Save("index.php"); 
	Redirect.cookie_Save("pratnket","index.php"); 

讀取 Cookie
	Redirect.cookie_Load("URL");
	
alert訊息
	Redirect.msg("跳轉插件"); 

讀取 Cookie
	Redirect.load("URL"); 
跳轉網址
	Redirect.JumpURL();
	Redirect.config("").JumpURL();
	Redirect.config({}).msg("").JumpURL();
	

---###使用範例###---

修改預設變數範例
Redirect.config({
	Home:"index.php",
	Cookie:"Href_Url"
});

Redirect.save( "http://ithelp.ithome.com.tw/" );
Redirect.msg( "預設變數URL: " + Redirect.load("URL") );
Redirect.msg( "預設變數Href_Url: " + Redirect.load("Href_Url") );
Redirect.msg( "跳轉前可輸入訊息").JumpURL(Redirect.load("Href_Url") );

複製程式碼

<script>	
(function () {
	//預設值
	var options = {
		Home: 'index.php',
		Cookie: 'URL',
	}

	//API
	var api = {
		//修改預設值
		config: function (opts) {
			if(!opts) return options;
			for(var key in opts) {
				options[key] = opts[key];
			}
			return this;
		},
		
		//保存網址(COOKIE變數)
		msg: function (message) {
			if (typeof message === 'string') {
				alert(message);
			}
			return this;
		},
		
		//保存網址(COOKIE變數)
		cookie_Save: function () {	
			if (typeof arguments[0] === 'string') {
				if(arguments.length == 1)
					document.cookie = options.Cookie + "=" + arguments[0];
				if(arguments.length == 2)
					document.cookie = arguments[0] + "=" + arguments[1];
			}
			return this;
		},
		//讀取網址(COOKIE變數)
		cookie_Load: function (name) {
			var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
			 if(arr != null) 
				return unescape(arr[2]); 
		},
				
		//跳轉頁面
		//只可輸入字串
		JumpURL: function JumpURL(TargetPage) {
			//無輸入字串
			if (TargetPage == null) {
				//跳轉預設值頁面
				window.location = options.Home;
			}
			//輸入的變數 型態與數值 必需是 字串
			if (typeof TargetPage === 'string') {
				//返回上頁
				if (TargetPage == "back" || TargetPage == "return"){ 
					//歷史紀錄 大於 1
					if (history.length > 1){
						history.back(-1)
					}else{
						//返回預設值頁面
						window.location = options.Home ;
					}
				}
				//關閉【視窗】
				if (TargetPage == "close"){
					window.close();
				}
				//重新整理頁面
				if (TargetPage == "reflash" || TargetPage == "reload") {
					location.reload();
				}
				
				//跳轉指定頁面
				window.location = TargetPage;

			}
				
			return this;
		}

		
	}
	this.Redirect = api;
})();
</script>

使用頁

<script src="Redirect.js"></script>
<script>
Redirect.msg( "跳轉前可輸入訊息").JumpURL(Redirect.load("Href_Url") );
Redirect.msg( "跳轉前可輸入訊息").JumpURL("https://www.google.com.tw/");
</script>

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言