Substring function in JSP does not respond in an alert function - javascript

The following code does not show the alert of "abc" but there is no any error message? Any idea to solve it?
<%# include file="includes.jsp"%>
<script src="js/bootstrap-datepicker.js"></script>
<link href="css/bootstrap-datepicker.css" rel="stylesheet" />
<script src="js/jquery.dataTables.min.js"></script>
<link href="css/jquery.dataTables.min.css" rel="stylesheet" />
...
...
$("input[name=rnrInd]").change(
function() {
if ($(this).is(":checked")) {
alert(${fn:substring("abcxyz", -1,3)});
$("input[name=mnpCustName]").val("PREPAID SIM");
$("input[name=mnpCustName]").attr("readonly", false);
$("input[name=prepaidSimInd]").prop("checked", false) ;
} else {
//$("input[name=prepaidSimInd]").prop("checked", false) ;
}
}).change();

You can use JavaScript substring() function instead.
alert("abcxyz".substring(0, 3));
The JS code is evaluated when JSP have already been rendered. The alert() function expects the argument of type string that could be printed in the dialog, but instead it is looking for variable abc which is not available in the code. If you still want to call JSP function on the server then a better approach is to add quotes to returned value.
alert('${fn:substring("abcxyz", -1,3)}');

Related

How to make javascript code to be embed into html button?

I am a noob for learning javascripts
Here I have a javascripts code
I made a button named "click"
when you click the button
it will send a value xx=1 to x
if the value is match with the condition
then it will print "Y", otherwise it will be "N"
but it always cannot shows the messagebox.
<html>
<head>
<title>test</title>
<script language="javascript">
function jclass(xx)
{
var x=xx;
if x==1
alert("Y");
else
alert("N");
}
</script>
</head>
<head>
<h1><a href="javascript:jclass(1);">click</h1>
</head>
</html>
I have also tried other ways like this.
but it still cannot work.
<html>
<head>
<title>test</title>
<script language="javascript">
function jclass()
{
var x=1;
if x==1
alert("Y");
else
alert("N");
}
</script>
</head>
<head>
<h1><a href="javascript:jclass();">click</h1>
</head>
</html>
You have syntax error in if condition. The basic if...else syntax requires condition inside parenthesis after if.
function jclass(xx){
var x=xx;
if (x==1)
alert("Y");
else
alert("N");
}
<h1><a href="javascript:jclass(1);">click</h1>
You must use the brackets
if(x == 1)
You are missing the brackets for comparison,its not python:)
You can check these type of errors in console and act upon exact statement
You should also put curly braces even for a single line in a block or condition to avoid any legacy or formatting issues when running code
Happy Programming
<html>
<head><title>test</title>
<script language="javascript">
function jclass(xx)
{
var x=xx;
if (x==1) {
alert("Y");
}
else {
alert("N");
}
}
</script>
</head>
<head>
<h1><a href="javascript:jclass(1);">click</h1>
</head>
</html>

Javascript confirm is not working

I have one strange problem with javasctipt confirm method, whenever I call this, its throwing error like "Uncaught TypeError: Property 'confirm' of object [object Object] is not a function"
var confirmVal = window.confirm("Please confirm?");
if( confirmVal == true ){
return true;
}
and my js files are in the html as like below
<link rel="stylesheet" href="css/vendor/bootstrap.min.css">
<link rel="stylesheet" href="css/vendor/jquery-ui.css">
<script src="js/vendor/jquery-2.1.3.min.js"></script>
<script src="js/vendor/jquery-ui.js"></script>
<link rel="stylesheet" href="css/vendor/jquery.mobile-1.4.5.min.css">
<link rel="stylesheet" href="css/vendor/star-rating.css">
<link rel="stylesheet" href="css/vendor/toastr.min.css">
<script type="text/javascript" src="cordova.js"></script>
<script src="js/vendor/bootstrap.js"></script>
<script src="js/vendor/jquery.mobile-1.4.5.min.js"></script>
<script src="js/vendor/star-rating.min.js"></script>
<script type="text/javascript" src="js/vendor/toastr.min.js"></script>
Strange is, in my other html file its working fine..
Please help me, what am I doing wrong here..
The code is correct except for the return statement which must be in a function.
function confirmation(){
var confirmVal = window.confirm("Please confirm?");
if( confirmVal == true ){
return true;
}
}
navigator.notification.confirm(
'ARE YOU SURE!', // message
myCallBck, // callback to invoke with index of button pressed
'CONFIRM', // title
['CONFIRM','CANCEL'] // buttonLabels
);
function myCallBck(index){
console.log(index);
}
You are using return statement but who's taking care of that statement if you want to use return statement you need to wrap your code into function
var confirmed = confirm("yes or cancel");
function confirmMe (){
if (confirmed){
return true;
}
}
you can also do this way
function confirmMe (){
return confirmed;
}
//going to return true or false
You can check if you are not overriding window object. Open console and type window and you can see if window has the confirm method.
Remove "return true" as it is must be wrapped inside a function
Learn more about return here
var confirmVal = window.confirm("Please confirm?");
if( confirmVal == true ){
alert("alright");
}

jQuery if inside a function undefined

I have this simple code that shows an image and a word and you have to complete the field. If you write the word correctly, a congrats popup comes up.
I was able to figure out how to randomize items in an array to display a random image.
Now I want to check on each keyup if it matches that random word.
It seems there's something wrong with my IF statement because if I remove it, popup works perfectly.
Error:
uncaught typeerror undefined is not a function
Code:
<html>
<head>
<title>AutistApp</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="js/jquery.min.js"></script>
</head>
<body>
<div id="container">
<div class="eval">CORRECTO</div>
<div class="siguiente">¡OTRA VEZ!</div>
<div class="dibujo"><img id="dibujito" src="img/apple.png" /></div>
<div class="palabra">MANZANA</div>
<div class="respuesta"><input type="text" id="resp" name="resp" value=""/>
</div>
</div>
<script type="text/javascript">
/***** RANDOM VALUES *****/
var messages = ["manzana", "pera", "banana"],
message = messages[Math.floor(Math.random() * messages.length)];
$('.palabra').text(message);
$('#dibujito').attr('src','img/'+message+'.png');
/***** KEYDOWN CHECK *****/
$(document).ready(function(){
$('#resp').keyup(function(){
if ($("#resp").value() == message) {
$('.eval').slideDown();
$('.eval').delay(3000).fadeOut();
$('.siguiente').delay(3000).slideDown();
}
});
});
</script>
</body>
</html>
You need to use .val() instead of .value() to get the value of input element, so you can do:
if ($("#resp").val() == message) {
instead of:
if ($("#resp").value() == message) {
You also need to wrap all of your code inside DOM ready handler:
$(document).ready(function () {
var messages = ["manzana", "pera", "banana"],
message = messages[Math.floor(Math.random() * messages.length)];
$('.palabra').text(message);
$('#dibujito').attr('src', 'img/' + message + '.png');
$('#resp').keyup(function () {
if ($("#resp").val() == message) {
$('.eval').slideDown();
$('.eval').delay(3000).fadeOut();
$('.siguiente').delay(3000).slideDown();
}
});
});
Folks you need to use
$("#resp").val()
in if condition.

What's wrong with this short Javascript code?

I'm trying to make the short Javascript code throw an alert and show 93 instead of 0-93 but it's not working ?
<html>
<head>
<script type="text/javascript">
function numberFromInput(value) {
return alert(Number(value.match(/[-]?(\d*)$/).pop()));
}
</script>
</head>
<body>
numberFromInput(0-93);
</body>
</html>
You have to call the function (you're just displaying its call code as content). And you have to pass the value as a string (you need quotes around 0-93):
<script type="text/javascript">
function numberFromInput(value) {
return alert(Number(value.match(/[-]?(\d*)$/).pop()));
}
numberFromInput("0-93");
</script>

Undefined error in JavaScript

I'm getting data from XML. I can successfully pick up a price from the XML but there is a unexpected error called undefined that shows up when I use the function given below;
<html>
<head>
<script type="text/javascript">
function myXml(origin, destination) {
var x=xmlDoc.getElementsByTagName("flights");
for(i=0;i<x.length;i++) {
if(x[i].getAttribute('FrTLAs')==origin && x[i].getAttribute('destination')==destination) {
document.write(x[i].getAttribute('price'))
}
}
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(myXml('SYD','Bali'));
</script>
</body>
</html>
myXml('SYD','Bali') call returns undefined, as you do not return anything in function body. So
document.write(myXml('SYD','Bali'));
will print "undefined" . Just replace above code with this:
myXml('SYD','Bali');
Engineer is correct, or better return the value from your myXml function.
so, document.write(undefined) wont occur and you may not get the above error.

Categories

Resources