I am using javax.script in Java, and I'd like to be able to detect whether the current Javascript implementation is Rhino. I'm doing this because I need to script to work properly on web pages as well as in Rhino.
Javascript pseudocode:
function writeMessage(message) {
if (implementation is Rhino) {
print(message);
}
else if (script is running in a web browser) {
document.write(message);
}
}
Ah, there we've got it in your comment. Just use the feature detection:
And then use
writeMessage(string)
all over your script. This is a short form ofwhich is better than what you suggested in the question, where the detection would be applied every time the function is invoked: