To test on another domain, put this code in <script> tag:
window.onload = function () { document.getElementById('ajax').onclick = function () { var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://localhost:12345/'); xhr.send(); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { if (xhr.status === 200 && window.location.protocol === 'file:') { alert('Test passed, the request from local file system not blocked'); } else if (xhr.status !== 200 && window.location.protocol !== 'file:') { alert('Test passed, the request from another domain is blocked'); } else { alert('Test not passed, something gone wrong'); } } }; }; document.getElementById('ws').onclick = function () { var socket = new WebSocket('ws://localhost:12345/echo', ['echo']); socket.onerror = function () { if (window.location.protocol !== 'file:') { alert('Test passed, the request from another domain is blocked'); } else { alert('Test not passed, something gone wrong'); } }; socket.onopen = function () { socket.send('echo'); }; socket.onmessage = function (event) { alert('Test passed, the request from local file system not blocked and a message is received back'); socket.close(); }; }; };
and this one in <body> tag:
AJAX
WS
To test in this local file system, press the buttons:
AJAX
WS