Take a look at this:
if(session.getAttribute("mode")!=null){
mode = (String)session.getAttribute("mode");
}
The first time value for the mode is empty so I set the mode value to a script variable like this:
var mode='<%=mode%>';
below is the method in which I call on load of the form, but it says mode is undefined
bodyOnLoad();
var mode='<%=mode%>';
alert("mode : "+mode);
function bodyOnLoad() {
if(mode.length < 0){
alert("mode empty 111111");
document.getElementById("functiontype").value="view";
document.getElementById("page").value="1";
document.forms["frmTempcard"].submit();
return;
}
}
Can anyone help me with this?
Declare the variable first. The mode is undefined when you call the function bodyOnLoad.
var mode='<%=mode%>';
bodyOnLoad();
Related
This is my JavaScript function
function movements(remove) {
var op = remove ? 'remove' : 'add';
crossvent[op](documentElement, 'selectstart', preventGrabbed); // IE8
crossvent[op](documentElement, 'click', preventGrabbed);
} function move(value) {
And this is how it's called
movements();
You can find reference for in jkanban.js file.
Now I have to change it to Typescript and I got this error on function calling,
Expected 1 arguments, but got 0
How can I resolve this problem in typescript ?
Simply add the question mark to the argument that requires your function, example:
function movements(remove?) {
// ...
}
You need to specify the input for calling movements().
You can set default to the variable using this:
function movements(remove = null) {
so that the function won't break even if you don't give it the input.
You can default it to anything you like though.
<script>
var tarih = tarih();
alert(tarih);
function tarih() {
var s=emrah;
return(s);
}
</script>
Bu fonksion neden undefined dönüyor ?
Why "undefined" returns?
The reason it returns undefined, is because of the assigment
var s = emrah
is confusing JS. So, it looks for a var name emrah and doesn't find it. That is the reason, you get undefined. Plus, if you are looking at your console, it would already have given you an error message, that
emrah is not defined
You might want to try: (If that's what you wanted)
var s = 'emrah'
You are calling the method before its defined.
Change the code to something like this.
<script>
function tarih() {
var s='emrah';
return(s);
}
var tarih = tarih();
alert(tarih);
</script>
the following code works perfect of Firefox but crashes on Chrome, with the following error: Uncaught TypeError: Property 'pos' of object [object Object] is not a function
Here is the code, with comments:
var CantidadMenu = $('div[class^=container_menu_]').length;
var position = $("#menu_unidades").position();
var IzAdd = 0;
var w = $("#menu_unidades").width();
var h = $("#menu_unidades").height();
for (i=0;i<CantidadMenu;i++){
var pos = 'pos'+(i+1); //I create a variable that will hold a string like: pos1,pos2...
IzAdd = IzAdd+25;
function pos(div){ //on this line I use the variable I created, which crashes on Chrome
var estilo1 = $(div).css({'left':IzAdd+25,'top':position.top+(IzAdd-25)});
return estilo1;
}
pos('.container_menu_'+(i+1));
$('.container_menu_'+(i+1)).css({'z-index':297+i,'width':w,'height':h});
}
Here you define a function named pos:
function pos(div){ //on this line I use the variable I created, which crashes on Chrome
var estilo1 = $(div).css({'left':IzAdd+25,'top':position.top+(IzAdd-25)});
return estilo1;
}
console.log(pos) // function ....
Here you overwrite it with a string:
var pos = 'pos'+(i+1);
console.log(pos) // string....
You should name either the function or the string to something else.
PS: I know that in your code the order is reversed, but function declarations are hoisted to the top of the scope, so the JS interpreter "sees" them in the order i wrote them in: first function, then the string.
PSS: The crash is actually on this line:
pos('.container_menu_'+(i+1));
function pos(div) is the same as var pos = function(div)... (except the former is defined at the parse-time, and the latter at the run-time, but that's irrelevant for your code), so if you expected by defining that pos = 'pos1';, for example, you'd get function pos(div) to become function pos1(div), it won't.
It will just overwrite the pos variable, and it will no longer be a string, but a function.
To fix your code, write a single function at the top of your code, outside of the for loop, add another parameter to it (IzAdd) and make sure you fix the function calls appropriately.
The function should look something like this:
function pos(div, IzAdd){
return $(div).css({'left':IzAdd+25,'top':position.top+(IzAdd-25)});
}
I've got a contact field on my website, and tests whether the value inserted is good or not. That part works fine. If a value is right, there is a var made with value = true.
I'm also calling a second function, which tests if all the contacts fields have got a true. If so, the send button will be abled, otherwise it keeps disabled. Sadly enough I get an error in my console, which says: Uncaught ReferenceError: nameIsGoed is not defined.
Hope you can help me out! :)
One of the tree functions which are quietly the same
function checkEmptyMessage(field) {
if (field.value != '') {
document.getElementById("message").style.borderColor="#91bc1e";
var messageIsGoed = true;
}
else if (field.value == ''){
document.getElementById("message").style.borderColor="#f15a24";
var messageIsGoed = false;
}}
The function that checks whether the value is true or not, if so: disable get's false.
function checkDisable(){
if ((nameIsGoed == true) && (messageIsGoed == true) && (mailIsGoed == true)){
document.getElementById("submit").disabled=false;
alert("mooizo");
}
else{
alert("er missen nog gegevens");
}
}
You have a scope problem, var messageIsGoed; should be (declared) outside your function, so the value you give it is available to other functions.
var messageIsGoed; // outside the function
function checkEmptyMessage(field) {
if (field.value != '') {
document.getElementById("message").style.borderColor="#91bc1e";
messageIsGoed = true;
}
else if (field.value == ''){
document.getElementById("message").style.borderColor="#f15a24";
messageIsGoed = false;
}}
I added (declared) var messageIsGoed; outside the function and removed the var inside the function so you don't declare it again (which actually makes a new variable only available inside that function).
Read more here about declaring variables: MDN:var
You cant use messageIsGoed outside of function if you declared it using var.
Just declare it outside both functions and use it inside without var so it will be global.
That's a local variable.
It only exists inside the function it's declared in.
You want to make a global variable, which will exist everywhere.
Declare the variable outside the function.
I have the following module pattern in the javascript for a webpage:
var swf_debugger = swf_debugger || {};
(function($, swf_debugger) {
swf_debugger.pageSetup = (function() {
var
swfType = null,
formData = {},
// ... unimportant code continues ...
initChangeEvents = function() {
$.each(formElements, function(index, $el) {
if ($el.hasClass("swfToLoad")) {
// THIS EVENT IS WHERE I MAKE THE ASSIGNMENT TO 'swfType'
$el.change(function() {
swfType = $("option:selected", this).val();
console.log("swfToLoad has triggered");
console.log(swfType);
});
return;
}
// NO ISSUES HERE WITH THESE EVENTS...
switch($el.prop("tagName")) {
case "SELECT":
$el.change(function() {
formData[$el.attr('id')] = $("option:selected", this).val();
});
break;
case "INPUT":
switch ($el.attr('type')) {
case "text" :
$el.change(function() {
formData[$el.attr('id')] = $(this).val();
});
break;
case "checkbox" :
$el.change(function() {
formData[$el.attr('id')] = $(this).prop("checked");
});
break;
default:
}
break;
default:
}
});
},
init = function() {
$(function() {
addFormComponents();
populateDropdowns();
initCachedData();
initChangeEvents();
});
};
init();
return {
swfType: swfType,
formData: formData
};
}());
}($, swf_debugger));
Essentially I am attaching an event to a series of jquery selected elements, with the callback simply storing the contents of a particular form element (specifically select, text, and checkbox elements) in a variable or an object.
I know my events are attaching properly because when I add console.log statements to them I can see them firing. Also, whenever I call swf_debugger.pageSetup.formData in the console I see valid contents of the object that each of those events are populating, so those events are doing what they're supposed to.
My troubles are happening whenever I try to access swf_debugger.pageSetup.swfType it always returns null and I am not understanding why. I know that the particular event feeding this value, is firing and I know that at least within the function scope of the callback, swfType is valid because of what is returned in my console.log statements. However, whenever I try to access the contents of swfType through the closure, (i.e. typing swf_debugger.pageSetup.swfType in the console), It always returns null.
I am guessing that I am running into the difference between an objects reference being passed and a variables value being passed, but I am not sure. Can someone please help me along here and explain why swfType is always returning null through the closure.
why is swfType always returning null
Because that's the value which you assigned to the property (gotten from the swfType variable which had that value at the time of the assignment). A property is not a reference to the variable assigned to it - you can only assign a value.
What you can do:
make the object property a getter method which returns the value of the local swfType variable whenever it is called
don't use a variable but assign to the property of the swf_debugger.pageSetup object each time