1. 程式人生 > >設定html網頁只能在微信中開啟,並禁用分享功能

設定html網頁只能在微信中開啟,並禁用分享功能

方法:用js判斷

(function () {
	/* begin禁用微信分享功能 */
	function onBridgeReady() {
	    WeixinJSBridge.call('hideOptionMenu');
	}

	if (typeof WeixinJSBridge == "undefined") {
	    if (document.addEventListener) {
	        document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
	    } else if (document.attachEvent) {
	        document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
	        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
	    }
	} else {
	    onBridgeReady();
	}
	/* end禁用微信分享功能 */
	
	 // 對瀏覽器的UserAgent進行正則匹配,不含有微信獨有標識的則為其他瀏覽器
    var useragent = navigator.userAgent;
    if (useragent.match(/MicroMessenger/i) != 'MicroMessenger') {
        // 這裡警告框會阻塞當前頁面繼續載入
        // 以下程式碼是用javascript強行關閉當前頁面
        var opened = window.open('about:blank', '_self');
        /*opened.opener = null;
        opened.close();*/
    }
    else{
    	window.alert = function(name){
    		var iframe = document.createElement("IFRAME");
			iframe.style.display="none";
			iframe.setAttribute("src", 'data:text/plain,');
			document.documentElement.appendChild(iframe);
			window.frames[0].window.alert(name);
			iframe.parentNode.removeChild(iframe);
		}
    }
	
})