Here is my Code:
<%#page contentType="text/html"%>
<%#page pageEncoding="UTF-8"%>
<%#page import="java.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Example of Java Server Page with JDBC</title>
</head>
<script>
function myFunction() {
var x='<% request.getParameter("ServerName"); %>';
alert(x);
</script>
<body>
<form>
ServerName: <input type="text" name="ServerName" required> <br><br>
<input type="submit" id="btnSubmit" name="btnSubmit" />
</div>
</form>
</body>
</html>
Here in the above function onclick of a button i want to execute the scriptlets which is inside javascript?
you can also use this:
<%#page contentType="text/html"%>
<%#page pageEncoding="UTF-8"%>
<%#page import="java.sql.*"%>
<%
String ServerName = (String)request.getParameter("ServerName");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Example of Java Server Page with JDBC</title>
</head>
<script>
function myFunction() {
var x='<%=ServerName%>';
alert(x);
</script>
<body>
ServerName: <input type="text" name="ServerName" required> <br><br>
<input type="submit" id="btnSubmit" name="btnSubmit" />
</div>
</form>
</body>
</html>
You can, but if you want the result to be passed to the JavaScript you have to output something.
var x='<%= request.getParameter("ServerName"); %>';
^ output!
… and unless you take measures to escape that data, you render yourself vulnerable to XSS attacks.
(And, obviously, this won't work until the form is actually submitted)
Related
I am trying to include an EJS file in my XHTML file so that I could pass on the data to the managed bean.
I tried by including withing in script tag as but i am not getting the value from the EJS file to XHTML file.
Could anyone please help me in the proper way to include an EJS file in HTML/ XHTML file.
Thanks,
ind.ejs
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<h1> Date is: </h1> <%= new Date() %>
Room Type is: <%= checkindt %> <br />
Room Type is: <%= roomtype %>
</body>
</html>
app.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<head>
<script type="text/javascript" src="/ind.ejs" > </script>
<title>My App</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<script type="text/javascript" >
function setValue(){
alert('app.xhtml');
alert('cindt is: '+ checkindt);
document.getElementById('myForm.cindt').value = checkindt;
}
</script>
</head>
<body>
<div>My app</div>
<form name="myForm" >
Date: <input type="text" name="cindt" />
<button id="submit" onclick="setValue();" />
</form>
</body>
</html>
There may be a better solution than using ejs, but I will attempt to answer this question anyways.
1) Download a browser compatible version of EJS. Try clicking Here to download or go here.
2) Inlude it in you page:
<script src="ejs.js"></script>
<script>
// your code here
</script>
3) Render/Compile it:
From text:
<script src="ejs.js"></script>
<script>
var ejsResult = new EJS({
text: "<div>Room number is: <%= roomnumber %> <br /> Room Type is: <%= roomtype %> </div>"
}).render({roomnumber: "111", roomtype: "Suite"})
someElement.innerHTML = ejsResult;
</script>
From a url:
<script src="ejs.js"></script>
<script>
var ejsResult = new EJS({
url: "templateLocation.ejs"
}).render({roomnumber: "111", roomtype: "Suite"});
someElement.innerHTML = ejsResult;
</script>
I have a checkbox and a text input. I want that the text input is only enabled if the checkbox is checked. I found an answer to this problem here, but the following code did not work:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>title</title>
<script type="text/javascript">
document.getElementById('yourBox').onchange = function() {
document.getElementById('yourText').disabled = !this.checked;
};
</script>
</head>
<body>
<input type="text" id="yourText" disabled />
<input type="checkbox" id="yourBox" />
</body>
</html>
However, I noticed that the code works if I move the < script > environment below the < input > boxes like that
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>title</title>
</head>
<body>
<input type="text" id="yourText" disabled />
<input type="checkbox" id="yourBox" />
<script type="text/javascript">
document.getElementById('yourBox').onchange = function() {
document.getElementById('yourText').disabled = !this.checked;
};
</script>
</body>
</html>
Why does the position of the < script > environment play a role for the onchange attribute?
That's because the page is parsed from the top, and each element is added as soon as it is parsed.
When the script tag has been parsed the code will run right away. At that time the input tags hasn't been parsed, so the input elements doesn't exist yet.
By placing the script tag below the input tags, the script runs after the input elements has been created.
Instead of rearranging the code, you can use the onload event to make sure that the code runs after the page is completely parsed:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>title</title>
<script type="text/javascript">
window.onload = function() {
document.getElementById('yourBox').onchange = function() {
document.getElementById('yourText').disabled = !this.checked;
};
};
</script>
</head>
<body>
<input type="text" id="yourText" disabled />
<input type="checkbox" id="yourBox" />
</body>
</html>
Can anyone explain me why this piece of code does not work in Eclipse. I tried to compile the code on this site http://jsfiddle.net and it's working.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<script type="text/javascript">
$(document).ready(function () {
var checkbox = $('#checker');
var dependent = $('#day_number');
if (checkbox.attr('checked') !== undefined){
dependent.show();
} else {
dependent.hide();
}
checkbox.change(function(e){
dependent.toggle();
});
});
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<input type="checkbox" id="checker"><br>
<input type="text" id="day_number">
</body>
</html>
Not returning form field values
var formJSON = $.toJSON($('#form1'));
JSP is:
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="js/ebs.js"></script>
<script src="js/jquery-ui.js" type="text/javascript"></script>
<script src="js/jquery-1.8.2.js" type="text/javascript"></script>
<script src="js/jquery.min.js" type="text/javascript"></script>
<script src="js/jquery.json-2.4.min.js" type="text/javascript"></script>
<script src="js/jquery.json-2.4.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" onclick="validateField()" method="post">
Text Field 1 : <input id="txt1" name="txt1" type="text" />
<br />
<input type="submit" value="SUBMIT" />
</form>
</body>
</html>
My JavaScript is:
function validateField() {
alert('fn called');
var formJSON = $.toJSON($('#form1'));
alert('after');
alert('formJSON' + formJSON);
alert("method ends here.");
}
I am getting:
form JSON{"length":1,"0":{"txt1":{},"1":{}},"context":{"location":{}},"selector":"#form1"}
(There is no content for txt1 field even if we type something there.)
$.toJSON() converts an ordinary Javascript object to JSON.
It does not get values of DOM elements or jQUery objects.
You want .serializeArray().
I need a simple javascript that reads the value of the textbox (input) and possibly save it to a txt file or display on a blank page.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled 1</title>
<script type="text/javascript">
<!--
function getdata()
{
var val=document.getElementById('t1').value;
var win=open("");
win.document.write(val);
}
//-->
</script>
</head>
<body>
<input id="t1" type="text" />
<input id="B1" type="button" value="getdata" onclick="getdata()"/>
</body>
</html>