I have the following code:
<html><body>
<script type="text/javascript"> function date1(){
????
alert(<%=d%>);}</script>
<input type="button" value="date" onclick="date1()"/>
</body></html>
Instead of the ????, what will present the current date each time I’ll push the button?
<%String d = (new Date()).toString())%><%String d = "new Date()"%><%= Date d = new Date() %>;
Since this is homework I’ll give you an explanation without explicitly stating which option is correct:
Remember that any server-side Java code will run only at the point when the JSP is requested/served, essentially producing text that will be output to the browser. The code in the line with the alert:
Will output the contents of the server-side (Java)
dvariable between the parentheses of thealertstatement. So the correct answer is whichever one will get a new JavaScript date each time the button is clicked, so you need to work out which one results in this output to the browser:Only one of the answers does that.
Note: if you can’t figure it out, try actually running the code with each answer and see which works. I think you’ll find that one of the answers produces invalid JavaScript, one produces a server-side compilation error, and the other works.