"${ctx}" gets substituted when the JSP is converted to HTML and sent out.
In the case where you're pulling in JavaScript from a separated file (resource), "${ctx}" wouldn't work, because your webapp server doesn't evaluate EL found in javascript or CSS files. However, you could pass "${ctx}" as a parameter (including the quotes!) to your addIFrame() function. and access it as a Javascript parameter variable.
Alternatively,
you should be able to determine the value of
ctx totally in javaScript code, although the details escape me at the moment.
Note that what you call "context" is actually 2 different values concatenated: The server protocol/address and the actual URL Context. For example, say if we had a webapp deployed under the context path "/forums", then what you'd specify as "ctx" would be "https://coderanch.com/forums". A site-relative URL doesn't need the "https://coderanch.com" and can instead just look under "/forums", by the way. A totally relative URL (no leading slash or server protocol/location) is also possible, but rarely a good idea.