简体   繁体   中英

How can i use java variable value in JavaScript

I don't know if this is realizable, because i don't have strong knowledge in JavaScript.

I want to use Java variable in Javascript and XHTML code, i have a file path in java variable: applicationDadesVersioBB.rutaFitxerLog which i display as follows in XHTML page in a table as follows:

<tr>
  <th scope="row">#{literalsCore['Entorn.FitxerLog']}</th>
  <td>
     <a href="#{applicationDadesVersioBB.rutaFitxerLog}"  onClick="descarga([[${applicationDadesVersioBB.rutaFitxerLog}]])">#{applicationDadesVersioBB.rutaFitxerLog}</a>
  </td>
</tr>         

until now everything is fine, but what I want to do is once the path is clicked, the file should be downloaded, which i am not able to do with: onClick="descarga([[${applicationDadesVersioBB.rutaFitxerLog}]])

the JavaScript descarga function:

<script type="text/javascript">
    function descarga(file)
    {
        window.location=file;
    }
</script>

I think the problem I have is the conversion of java variable values to javascript values.

I tried to do it changing the onClick="descarga([[${applicationDadesVersioBB.rutaFitxerLog}]])" to onClick="descarga("#{applicationDadesVersioBB.rutaFitxerLog}")"

but no way, any help?

I'm not sure about what you're trying to do but I remeber a useful "hack" to do that in PHP for example... You have to create a hidden input in your HTML file and set its value to applicationDadesVersioBB.rutaFitxerLog. Then in your click handler you just get the input element thanks to its id for example and you read the value.

Hope it helps

您需要在字符串两边加上引号

onClick="descarga('[[${applicationDadesVersioBB.rutaFitxerLog}]]')"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM