Question

I came across some interesting behaviour of the javascript code on my XPages

//'rdoGeschlecht1' is present on page Basis (no problems there), 
//but not on page 'Stufe1'.    
var level = "Stufe1";
if(level == "Basis")
{
    alert("1");
// var rdoGeschlecht1 = '#{javascript:getClientId("rdoGeschlecht1")}'; 
}
else if(level == "Stufe1")
{
  alert("2");
}

The code above always ends in an error when executed on a page where the element is not present - "Ungültiger Komponentenname rdoGeschlecht1 kann in getClientId nicht aufgelöst werden." - it seems to me that Notes tries to resolve the object ID even if the line is not used in the actual execution and even when uncommented.

I have found a quick&dirty workaround of course, but I am surely not the first one to stumble upon this behaviour and I would really be interested in how experienced XPages programmers would be going about this?

Was it helpful?

Solution

The problem is you're commenting out the client-side JavaScript which is going to be run on the browser. But the server-side JavaScript code within #{javascript: (which needs to run on the server) is not commented out.

Perhaps it will help to explain what happens you put SSJS or EL in a string property or, in this case, a script block. Because the key is that the CSJS is not parsed on the server, it's just passed as a string to the browser.

The parser reads the string and looks for #{javascript: which tells it that the following code up to the closing } needs to be passed to the SSJS parser and the result added to the string that gets written to the browser. Any lines within that SSJS block that begin "//" will get omitted. But the parser will not take into account anything outside the #{javascript: because that is just text being passed to the browser. It is the browser that interprets the whole thing as client-side JavaScript.

Hopefully that clarified why it's working the way it is.

If you want to comment out a line in a script block that includes SSJS and you want to prevent the SSJS from running, you'll need to comment out the CSJS (so the browser doesn't run it) and the SSJS (so the server doesn't run it).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top