Member Avatar for feoperro

Hi,

I have pasted my code below, please paste it into a JSP and tell me why it gives the error "Unterminated String Constant"

<html>
    <head>
        <title>Debugging</title>
        <script language="javascript">
            function changeOn(object, objectId) {
                alert('in function');
                if (object == 'bookingsButton') {
                    alert('first if');
                    document.getElementById(objectId).src = 'Images/BookingsOn.png';
                } else if (object == 'detailsButton') {
                    alert('2nd if');
                    document.getElementById(objectId).src = 'Images/DetailsOn.png';
                }
            }
            function changeOff(object, objectId) {
                if (object == 'bookingsButton') {
                    document.getElementById(objectId).src = 'Images/Bookings.png';
                } else if (object == 'detailsButton'){
                    document.getElementById(objectId).src = 'Images/Details.png';
                }
            }
        </script>
    </head>
    <body>
        <%
                    for (int i = 0; i < 2; i++) {
        %>
        <table>
            <tr>
                <td>
                    <a href="Bookings.jsp"><img src="Images/Bookings.png" id="bookings<%out.println("" + i);%>" onmouseover="changeOn('bookingsButton','bookings<%out.println("" + i);%>')" onmouseout="changeOff('bookingsButton','bookings<%out.println("" + i);%>')"/></a>
                </td>
                <td>
                    <a href="Details.jsp"><img src="Images/Details.png" id="details<%out.println("" + i);%>"/></a>
                </td>
            </tr>
        </table>
        <% }%>
    </body>
</html>

Thanks,
-Ashton

The use of out.println() adds a newline character causing erroneous syntax of the generated output.
The generated html is,

<img src="Images/Bookings.png" id="bookings0
" onmouseover="changeOn('bookingsButton','bookings0
')" onmouseout="changeOff('bookingsButton','bookings0
')"/>

There is a newline character after bookings0

Use out.print() instead. Which will generate html like,

<img src="Images/Bookings.png" id="bookings0" onmouseover="changeOn('bookingsButton','bookings0')" onmouseout="changeOff('bookingsButton','bookings0')"/>
Member Avatar for feoperro

You're a genius. Thanks a lot man.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.