Pass boolean from javascript

Hi Everyone,

I am using vaadin 6 and looking for the easiest way to pass a boolean to java from javascript. This is a hack, so bare with me.

Basically I needed a way to see if a website is reachable on the client side. I don’t want to check the HTTP status code from the server side, because I am using this feature to test firewalls on the client side.

Normally this isn’t possible because of same origin policy, however, executing the following does the trick:


var node=document.getElementById('[LAYOUT ID]
');
var script_tag = document.createElement('script');
script_tag.setAttribute('type', 'text/javascript');
script_tag.setAttribute('src', '[URL TO TEST]
');
script_tag.onerror = function() {
alert("Unreachable");
 }
script_tag.onload = function() {
alert("Success");
}
script_tag.setAttribute('id', '[SCRIPT ID]
');
node.appendChild(script_tag);

I would like to take the success or unreachable messages and pass them to vaadin, so that I can change an icon in my app. I know this is easy with vaadin 7, but we currently don’t have time to update from 6 to 7. Any ideas?

Welp this is now a hack on a hack. For now I am going with a 0px by 0px textField and I am monitoring for value changes. If anyone can think of a slicker solution that won’t require a lot of coding please let me know.

var node=document.getElementById('[LAYOUT _FOR_ATTACHING_SCRIPT]
');var script_tag = document.createElement('script');
script_tag.setAttribute('type', 'text/javascript');
script_tag.setAttribute('src', '[URL_TO_TEST]
');
script_tag.onerror = function() {var field=document.getElementById('[HIDDEN_TEXT_FIELD');field.focus();field.value="";field.blur();field.focus();field.value='{"requestId":"5501064b-4e9c-4ab5-8407-82c2e894b76f","itemId":"c331adb2-6e6c-4b17-9f43-be468b519b66","result":false,"scriptId":"9b5ef06d62264bccb760764fc63c9005"}';field.blur();}
script_tag.onload = function() {var field=document.getElementById('[HIDDEN_TEXT_FIELD}');field.focus();field.value="";field.blur();field.focus();field.value='{"requestId":"5501064b-4e9c-4ab5-8407-82c2e894b76f","itemId":"c331adb2-6e6c-4b17-9f43-be468b519b66","result":true,"scriptId":"9b5ef06d62264bccb760764fc63c9005"}';field.blur();}
script_tag.setAttribute('id', '9b5ef06d62264bccb760764fc63c9005');
node.appendChild(script_tag);

I check to make sure the request matches a requestId that was registered when the button was pressed, update my icon, and then push the changes with ICE Push.