Since the session ID (JSESSIONID) is set as a cookie, we only need to figure how to retrieve the cookie in JMeter and pass it to the JavaScript. The first step is to enable cookies in JMeter so that they are accessible by using variables. In jmeter.properties, set
CookieManager.save.cookie=true.
Next, make sure you add a HTTP Cookie Manager to your Thread Group. Add a BSF PostProcessor and write your JavaScript in the text area provided. You can also externalize your script to a file and specify the file name in the text field above the text area. You can access the JSESSIONID cookie this way:
var sid = "${COOKIE_JSESSIONID}";
To invoke a JavaScript function and set the return value as a variable:
var val = compute(sid); vars.put("someKey", val);
You can access the value later as
${someKey}
.