Hi I am creating a webpage which is developed from a servlet using printwriter , I am appending HTML,css and js , but the javascript functions are not working , is this beacause of any syntax error or is it not supported js inside java?
my code
String htmlHeader = null;
htmlHeader = "<HTML><HEAD><TITLE>"
+ title
+"</TITLE>"
+"<link rel=stylesheet href=/resources/css/jquery-ui.css>"
+"<script src=//code.jquery.com/jquery-1.10.2.js></script>"
+"<script src=//code.jquery.com/ui/1.11.4/jquery-ui.js></script>"
+"<style type=text/css>"
+"body "
+"{background-image: url(resources/img/timer_page_new.png);"
+"-moz-background-size: cover;-webkit-background-size: cover;"
+"background-size: cover;background-position: top center !important;background-repeat: no-repeat !important;"
+"background-attachment: fixed;}"
+"#sel.ui-selecting { background: '#FECA40'; }"
+"#sel.ui-selected { background: '#F39814'; color: '#7e7d79'; }"
+"#sel { list-style-type: none; margin: 0; padding: 0; width: 100%; }"
+"#sel li { margin: 6px; padding: 0.4em; font-size: 1.4em; height: 28px; }</style>"
+"<script type=text/javascript>"
+"function init(){"
+"var ol = document.getElementById('selectable');"
+" var macId ="+macId+" ;"
+ "var entry = document.createElement('li');"
+"entry.appendChild(document.createTextNode(macId));"
+"ol.appendChild(entry);"
+"ol.addEventListener('click', function(e) {"
+"if (e.target.tagName === 'LI'){"
+"alert(e.target.id);"
+"}"
+"});"
+"}"
+"setTimeout(function () {"
+" location.reload();"
+" }, 20 * 1000);"
+"</script>"
+"</HEAD><BODY onload=init("+macId+","+tHr+","+tMin+")><div align=center>";
return htmlHeader;
js file with function init()
function init(macId,tHr,tMin){
var sTempTableRow='<tr><td width=150px><div align=center><font size=4 color=white face=verdana>'+macId+'</font></div></td>'
+'<td width=150px><div align=center><font size=4 color=white face=verdana>'+tHr+'</font></div></td></tr>'
+'<td width=150px><div align=center><font size=4 color=white face=verdana>'+tMin+'</font></div></td>';
$('#timerTable').append(sTempTableRow);
}
part of html
<BODY onload=init("+macId+","+tHr+","+tMin+")>
If you want to code your javascript in your html file that I would say you can use JSP instead of Servlet.
But if you want to use servlet then you can move your css
and javascript
part in separate .css
and .js
files respectively and just include those files using your PrintWriter
. And whatever dynamic fields you want to pass to javascript that you can pass as parameters to your javascript function (like function init(macId) {
).