简体   繁体   中英

How to pass Object as parameter to java script function on onClick

My main goal: When i click the link, a new window should be opened and display the content of entire log file in that window and the window should not have an address bar and navigation buttons (Back, Forward).

Is there any other approach that i can reach my goal?

Here is what i am trying to do, I am trying to call the java script method by passing the url of log file and thought of implementing the code to open new window

<table id="serverLogsStats" class="serverLogsView">
   <c:forEach var="log" items="${logsList}" varStatus="i" >
      <tr>
        <td>${log.name}</td>            
        <td><a href="#" onclick="openLog('${log}');">Open log</a></td>        
      </tr>
   </c:forEach>
</table>


<script>
function openLog (log) {
    var abc = log.name;
    alert(abc);    
}
</script>

When i pass an object 'log' to the openLog function, the value is passing as a String. I want the log to be passed as an object.

Here my output is coming as undefined.

If i put 'alert(log);' it is printing the address of the object

This should work:

<table id="serverLogsStats" class="serverLogsView">
   <c:forEach var="log" items="${logsList}" varStatus="i" >
      <tr>
        <td>${log.name}</td>            
        <td><a href="#" onclick="openLog('${log.name}');">Open log</a></td>        
      </tr>
   </c:forEach>
</table>


<script>
function openLog(logName) {
    alert(logName);    
}
</script>

You can't use java objects and use them directly as JS object, you can only pass primitives.

For your use case, you could have an URL server side taking a log name as parameter and displaying it's content.

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