This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

sending ctx tnside javascript script tag

 
Ranch Hand
Posts: 458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my script tag of a JSP page, I have the following:




 
 
In JSP page, I am calling some of my files like this:


 


So ${ctx} is taking care of the domain name for me whether I'm running my code locally or on a server with a different domain.


Question is - How can I use it inside addIframe function such that I could use something like  x.setAttribute("src",  "${ctx}/boxlabelpdf.htm"); instead of x.setAttribute("src",  "http://localhost:8080/myapp/boxlabelpdf.htm"); ?
 
Saloon Keeper
Posts: 28325
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"${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.

 
Jack Tauson
Ranch Hand
Posts: 458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote: However, you could pass "${ctx}" as a parameter (including the quotes!) to your addIFrame() function. and access it as a Javascript parameter variable.


Thanks. This is what I ended up doing and it worked.
 
Those are the largest trousers in the world! Especially when next to this ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic