2

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);
    }
}
Anderson Green
  • 25,996
  • 59
  • 164
  • 297

2 Answers2

5

Ah, there we've got it in your comment. Just use the feature detection:

var writeMessage = document && document.write
  ? document.write.bind(document)
  : print;

And then use writeMessage(string) all over your script. This is a short form of

if (document && document.write)
    var writeMessage = function(message) { document.write(message); };
else
    var writeMessage = function(message) { print(message); };

which is better than what you suggested in the question, where the detection would be applied every time the function is invoked:

function writeMessage(message) {
    if (document && document.write) { // running in a web browser
        document.write(message);
    } else { // it will be Rhino
        print(message);
    }
}
Bergi
  • 513,640
  • 108
  • 821
  • 1,164
  • I don't see a declaration of the variable "pring." What does this refer to? – Anderson Green Aug 05 '12 at 18:38
  • @AndersonGreen it's a typo; he meant `print`. – obataku Aug 05 '12 at 18:39
  • So what does this code actually do? The syntax is somewhat unclear to me. Can it be re-written without the question-mark operator? – Anderson Green Aug 05 '12 at 18:43
  • Explanation of ternary operator syntax in Javascript (which is difficult to understand in this case): http://stackoverflow.com/questions/1771786/question-mark-in-javascript – Anderson Green Aug 05 '12 at 18:45
  • 2
    @AndersonGreen If `document` is defined and not null, and `document.write` is also, then it sets the `writeMessage` variable to a reference to a bound function, namely `document.write`; otherwise, it sets `writeMessage` to a reference to `print`. Essentially, if you can use `document.write`, it uses that (e.g. this would work in most browswers); if not, then it assumes you're using Rhino and chooses `print` instead. – obataku Aug 05 '12 at 18:45
  • So can it be written like this? if(document && document.write){ document.write.bind(document); } else{print;} – Anderson Green Aug 05 '12 at 18:46
-2

If it's only to be run in either the web browser or on top of Rhino, then surely they're mutually exclusive; that is, if the script is not running in a browser, then it's running via Rhino.

obataku
  • 27,809
  • 3
  • 38
  • 50
  • It's not either-or. I plan to use the script in Rhino as well as in some web pages. I would use "console.log" if the script were running in Chrome, and "write" if the script was running in Rhino. – Anderson Green Aug 05 '12 at 18:37
  • What is the alternate option? – obataku Aug 05 '12 at 18:38
  • He meant: there are only two options were it would be run, and when the script is executed it runs in exactly one of them. – Bergi Aug 05 '12 at 18:40
  • @Bergi, so, then, shouldn't they be mutually exclusive? Forgive me if I'm wrong. – obataku Aug 05 '12 at 18:41
  • @veer In this case, what does "they" refer to? – Anderson Green Aug 05 '12 at 18:49
  • @AndersonGreen 'they' refers to the two distinct ways this code would be run, namely via the web browser and via Rhino. My point was that you didn't need a way to check if you're executing via Rhino, only that you're executing via a web browser, since the options are exclusive. – obataku Aug 05 '12 at 18:54
  • 1
    @veer Why would the options be mutually exclusive? It is possible to write a script that will run properly in Rhino as well as in the browser (using feature detection). – Anderson Green Aug 05 '12 at 18:59
  • @AndersonGreen I don't think you understand what mutual exclusion is. My point was that the script at any time during execution is either being run via Rhino, __OR__ via a web browser. Thus, you only need to check for one to deduce the other -- if it's running in the browser, it's not via Rhino; conversely, if it's not in the browser, then surely it's via Rhino. That was my point. – obataku Aug 05 '12 at 19:06
  • Yes, but I'm going to use the script in a web page as well as Rhino (but not in both at the same time, of course.) I just want it to work properly in more than one javascript implementation. – Anderson Green Aug 05 '12 at 19:25
  • @AndersonGreen [Mutually exclusive events](http://en.wikipedia.org/wiki/Mutually_exclusive_events) -- I'm fully aware you're going to use it in both, which is why I suggested you only need to check whether the script runs in the browser and then you can infer whether running via Rhino as the events are [complementary](http://en.wikipedia.org/wiki/Complementary_event). – obataku Aug 05 '12 at 19:37