Created
August 27, 2012 19:55
-
-
Save pikotea/3491756 to your computer and use it in GitHub Desktop.
jQuery Mobileでホワイトリスト使って読み込むページを制限する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var a = document.createElement('a'); | |
function getInfo(url) { | |
a.href = url; | |
return { | |
host: a.host, | |
pathname: a.pathname | |
}; | |
} | |
$(document).on('pagebeforeload', function(e, data){ | |
// ホワイトリスト | |
var urls = [ | |
'\/app1\/.*', | |
'\/app2\/.*' | |
]; | |
var info = getInfo(data.url), | |
isValid = false; | |
// 同一ドメインのみ許可 | |
if ( location.host == info.host ) { | |
// ホワイトリストと比較 | |
for ( var i = 0, len = urls.length; i < len; i++ ) { | |
var reg = new RegExp('^' + urls[i] + '$', 'i'); | |
if ( info.pathname.match(reg) ) { | |
isValid = true; | |
break; | |
} | |
} | |
} | |
if ( !isValid ) { | |
console.log('denied', data.url); | |
e.preventDefault(); | |
data.deferred.reject(data.absUrl, data.options); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment