user2439143
user2439143

Reputation: 3

Write java code in js file

I want to access a system property from my .js file. Initially I had accessed the system property from my jsp file using the below syntax which worked just fine:

<script type="text/javascript">
 function ChatWindow(){
        var property = "<%=System.getProperty("CHAT_WINDOW_URL") %>";
        alert(property);
    }
    </script>

However when I tried to use the same function in .js file, I am getting errors that :

Expected ';'

When I do add ';' as follows:

var property = "<%=System.getProperty("CHAT_WINDOW_URL"); %>";

or as

var property = "<%=System.getProperty('CHAT_WINDOW_URL') %>";

The error goes.. but the property value is not resolved. Could anybody please help me on this.

Upvotes: 0

Views: 9068

Answers (1)

Ankit
Ankit

Reputation: 3181

This is not a correct way to do it. Javascript is client side code whereas scriptlet is complied on server side. The best way to do this is by using a hidden input element.

<input type="hidden" value="<%=System.getProperty('CHAT_WINDOW_URL') %>" id="chatWindowURL" ../>

Now, in you javascript, write:

var systemURL = document.getElementById('chatWindowURL').value;

Upvotes: 2

Related Questions