Is there a way to make the following work?
function TimerEvent()
{
TIMER_OBJ = setTimeout('Ajaxsessioncheck();', '<%=Timer%>');
}
I am calling this function in the onload event but it is not calling the Ajaxsessioncheck function when the time has elapsed in Firefox. In IE and Chrome it works fine.
thanks for all for ur time.. i changed the code as sent timer as integer now i have a different problem. In the Ajaxsessioncheck() function i wil call a JSP page from i am not getting Response in Firefox.
You've specified '<%=Timer%>' as a string (denoted by the single quotes), where it should be an integer, like so: <%=Timer%>
You should also specify the first argument as a function reference rather than a string, so your final output would be:
setTimeout(Ajaxsessioncheck, <%=Timer%>);
you shouldn't pass the second parameter as string.
TIMER_OBJ = setTimeout('Ajaxsessioncheck();', <%=Timer%>);
should work fine. but to be even more correct, you should also avoid passing the first parameter as string, because otherwise is gets evaluated - a hidden execution of eval happens, and eval is evil. therefore, this is what you want:
TIMER_OBJ = setTimeout(Ajaxsessioncheck, <%=Timer%>);
PS. declaring a variable without using keyword var causes it to leak to the global scope. I'm not sure if you're aware of this fact.
'<%=Timer%>' is a string - it should be an int in milliseconds.
Almost all questions starting with X does not work in Y comes down to differences in browser implementation. Similar to
document.getElementById does not work in firefox and the element has a name but no ID. Works in IE but not in Fx
Related
I'm having this weird behavior and I'm not sure if it's me not misunderstanding variables or it's an xpage issue
I have a document with a field called "hours" and it has a value of 8.
Here is my simplified code .
var xHrs = doc.getItemValueDouble('hours');
println (xHrs); // at this point, hours is 8
doc.replaceItemValue('hours', 0);
return xHrs; // returns 0;
Why is xHrs back to 0 when I replace the document value to 0? How do I break the link?
Thanks in advance for the help :)
R.
Chances are that the code is being executed more than once.
Try wrapping the code with ${javascript: rather than #{javascript:
${javascript:
var xHrs = doc.getItemValueDouble('hours');
doc.replaceItemValue('hours', 0);
return xHrs;
}
The code will be executed only once with the preceding $.
I suspect that setting your xHrs variable as you do creates a function expression that returns the value of the 'hours' field. You change the value of the field and the function returns the new value.
I'm not sure about breaking the chain in an efficient manner, but maybe if you create a second variable to hold the xHrs value?
I have what looks like to me to be a simple variable assignment not working.
This code is in jQuery, for the context see here.
I'm calling:
$('#foo').on('someEvent', eventHandlerFn);
And I get this issue within the jQuery on function. Here's the starting point:
As you can see from the console below the code, selector is set the my eventHandlerFn and the fn variable is undefined. This is as expected.
On line 3509, the value of selector is assinged to fn. So, the value of fn should be same as the value of selector, no??
See below - selector is defined, as expected, but fn is still undefined. Why?
The end result is that my event handler is never registered.
The code runs well as shown in the following two screens (the issue is on how chrome sets the context to the console)
It looks like console has access to the variable at definition time (in this case the passed parameters) and not the live values as you run the code
Before the swap
After the swap
I'm not seeing any problem with this jsFiddle. Feel free to edit the jsFiddle to get it to look more like your code.
Can you try putting in console.log(fn); after line 3510 and rerunning? Maybe it's just a problem with the debugger?
This seems to be an issue with the debugger in Chrome - either a material problem or just a nuance of the debugger that I don't understand. fn does have a value toward the end of the call, but not where the breakpoint is.
I'm updating an existing website running on Expression Engine. So far, I've stayed away from any code I didn't write or couldn't understand. I recently must have altered some bit of code someplace (helpful, I know) and now a block of JS I didn't write is causing an error that seems to bypass the document.ready() event. The window.load() event however is still taking place.
In the Chrome DevTools Console, the error "Uncought TypeError: Cannot call method 'replace' of UNDEFINED" points to the definition of a function "fixedEncodeURIComponent" pasted below.
$("#MessageContainer.Counted").counter({
type: 'char',
goal: 250,
count: 'down'
}).change(function(){
var TEMP = fixedEncodeURIComponent($(this).val());
$("#Message").val(TEMP);
});
var TEMP = fixedEncodeURIComponent($("#MessageContainer.Test").val());
$("#Message").val(TEMP);
function fixedEncodeURIComponent (str) {
str=str.replace(/"/g, '');
return encodeURIComponent(str).replace(/[!'()*]/g, escape);
}
As I interpret the error, this function is being passed a variable that is not a string. I added an alert(str) to the function definition and the result was UNDEFINED as I expected. The first of several unknowns for me is which call to the function 'fixedEncodeURIComponent' is being passed a bad variable. I assume that it's the first call, but that's just a guess. It so happens that this first call contains a syntax I have never encountered before. I don't know how to interpret what happens when $(this) is passed as a function argument.
Any insights would be greatly appreciated. Also, if there's more information you need please let me know. The client's site is password protected but I can include any code you request.
Thank you.
I'm taking a guess that the }); on line 3 is exiting a document.ready context. If that's the case then your second call to fixedEncodeURIComponent may be getting called before the DOM is even loaded.
Start by wrapping
var TEMP = fixedEncodeURIComponent($("#MessageContainer.Test").val());
$("#Message").val(TEMP);
in a
$(function() {
// code
});
block. If that doesn't work, check that #MessageContainer.Test actually matches an element. Since this is code you inherited, the class name "Test" clues me in that the block in question might be a remnant of someone trying to debug an issue and maybe it should have been removed.
I suspect $("#MessageContainer.Test") since it looks like its supposed to be an ID selector instead of what it actually is when jQUery parses it(which is an ID selector combined with a class selector). $("MessageContainer\\.Test") allows you to select an element with ID MessageContainer.Test
This function is declared inline in many tags: <i onClick="noteFc('someNoteText1')"> ... <i onClick="noteFc('someNoteText2')"> ... calling with different note texts, dependent on a call place.
function noteFc(str){
var bubble = window.event.srcElement.getElementsByTagName('i')[0];
diff=...
bubble.style.left = diff + "px";
}
My code works in Chrome, but FF says "undefined" window.event which I read in the function,
FF event object is not visible in function body, thus event should be passed via an explicit parameter of the function, which means for me that all inline declaration(calling?) of function should be rewrited in all source codes to onClick=noteFc(event, "someNoteTextN"); but what with this coding in Chrome, where the event object is conceived differently?
An element's target is sufficient, thus calling can be onClick=noteFc(this, "someNoteTextN"); probably works everywhere, but adding is much work too, led to a bulkier, intricate source code.
Sniffing variable this in the function noteFc(str) led to a blind street, because FF puts [object Window] into this global variable, contrary to Chrome value [mouseEvent] – is there any way to distinguish between such values? In addition, I not see how can I to mine a clicked element from an entire [object Window] in FF.
I have searched the net for many hours, but I found only statements that FF itself passed an event as hidden argument (without parameter declaration in inline, only alone parameter in a definition). Unfortunately all found examples have no real argument passed into.
What in case of passing the explicit real parameter into function? Where is a event implicitly secretly passed into function in such case?
arguments[0] contains str (explicit text parameter), and arguments[1] is undefined.
Is there any direct way (reading of global variable in function body would be best for simplicity) for determining an event(target is sufficient) which triggered an inline function with a real parameter in the Firefox?
(Way other than jQuery.)
Edited: quotes was added due to a reader mention.
You want:
<i onClick="noteFc(event, 'someNoteText1')">
The "event" argument is passed to the event handler compiled from the attribute, but it's then the handler's decision whether to pass it on to the noteFc function it calls or not.
I hit the dead end with this one, scratching my head for half a day already. The problem is quite strange, if you'd care to look at my source code: www.modwebsolutions.com/test2 (warning: not optimized yet, might lock browser for a few seconds). Problem is, that sequence of setTimeouts works ok in Opera, but in other browsers only first one is executed and then execution of the script stops. Exerpt from my code:
var a1=setTimeout(drawVertical([arguments]),1000);
var b1=setTimeout(drawVertical([arguments]),1000);
var c1=setTimeout(drawVertical([arguments]),1000);
var d1=setTimeout(drawVertical([arguments)],1000);
Tried everything, enclosing function in quotes, looking for other syntax errors, nothing helped. And the strange thing is as I mentioned - everything works ok in Opera.
UPDATE: also works in Chrome, and that makes it even more confusing...
UPDATE2: same example without timeout, works just fine (though slow): www.modwebsolutions.com/test
You should pass the parameters for you function after the function and timeout. Have a look at the documentation
var a1 = setTimeout(drawVertical, 1000, [arguments]);
What you have done here is called a function before setting the timeout. Immediately when the code sees drawVertical([arguments]), it is going to call that function. The return value of that function is what you are passing as your first argument into setTimeout, so unless drawVertical returns a function, this won't work.
So you could get this to work by giving setTimeout a function to call:
var args = arguments;
var a1=setTimeout(function() { drawVertical([args]); },1000);
...
EDIT: I set the outer arguments to an args variable, since the arguments would be different in the inner function.