I have this JS code:
var show = elm.hasClassName('level0') ? false : true;
if(show) {
doSomething()
}
I am using FireBug to check the value of show and it clearly states false. While debugging, I noticed that the doSomething function is called anyway. What am I missing?
Using if(false) does not run the doSomething function.
Thanks!
From whatever code you have shown (!!!), I believe that your debugging is wrong. May be you are seeing the value of
elm.hasClassName('level0')
as
false
But, var show = elm.hasClassName('level0') ? false : true; means show will be set to inverse of elm.hasClassName('level0')
Just add an alert(show) above the if condition and see what is printed. See this fiddle http://jsfiddle.net/g4Zqp/1/ It works perfectly fine.
If this is not the case, you need to put your complete code
Try this
if( !elm.hasClassName('level0')) {
doSomething()
}
Related
$('#reset').click(function(){
var confirm = confirm("This will reset everything, do you really want to continue?!");
if (confirm == true) {
alert();
}
});
Any idea why above code doesn't work? I got an error of undefined is not a function.
It is because of the local variable confirm.
Since you have declared a local variable with name confirm, when you use confirm() in your function it will have the value undefined as it is no longer referring to the global confirm function.
Just rename the variable and it should be fine.
$('#reset').click(function () {
var value = confirm("This will reset everything, do you really want to continue?!");
if (value == true) {
alert();
}
});
That's because after running this code once, confirm is not a function, but a boolean, in handler's function scope.
Change the local variable name to something else, like 'value' or 'confirmed'.
Confirm is a function, please change the variable confirm in to something else.
var result = confirm("This will reset everything, do you really want to continue?!");
You should try by changing the name of your variable "confirm" (var confirm). It is the keyword of javascript language.
I have the below code in the second of two js files from a web-app.
It works fine, until I combine the two js files into one. Then the js breaks.
function oBlink()
{
return window.setInterval
(
function()
{
$("#sOr").css("background-color", function (){ this.switch = !this.switch; return this.switch ? "#F90" : "" });
}
, 500
);
}
I've isolated the problem to the code
this.switch = !this.switch; return this.switch ? "#F90" : ""
If I take that out, the rest of my js works fine.
I understand there are a lot of external variables that could be coming into play here, but I just wanted to check with you guys that the above function code doesn't have any errors in it.
Thanks for taking a look.
it's working fine in the browser, but failing when checking it on certain devices in the Android emulator.
That's probably because you are using switch in your code which is a reserved word in JavaScript. Only ECMAScript5-based browsers allow using reserved words as object's properties.
Instead of using a flag you can declare a CSS class and use the jQuery's toggleClass method.
Make sure you define somewhere
switch = false
Then Try
$("#sOr").css("background-color", function (){ this.switch =
!this.switch; return (this.switch ? "#F90" : "#FFF" ) });
I'm currently learning OOP in JS.
First off, here's the code:
this.device = {
init: function(){
this.isiOS7 = (this._isiOS7()) ? true : false;
},
_isiOS7 : function(){
return (navigator.userAgent.match(/(iPad|iPhone);.*CPU.*OS 7_\d/i)) ? true : false;
},
isiOS7 : null
}
As you can see, I defined a property named isiOS7 and a function _isiOS7. The main idea behind this is: whenever I needed to know whether the OS is iOS7, I'd access a property (isiOS7) which is true or false. If I'd use the function (_isiOS7) over and over again it would take longer for the JS engine to compute these statements(if/regex/match) than a simple property/var. So my question is: Is this true?
Thanks!
In this case you are just trying to get the window navigator. So it's better to run init once and use an accessor for the property.
Although this will hardly matter unless this is supposed to be called often.
I have some inherited JS code that uses this format:
function main(param) {
var myVar;
function doSomething() {
...
}
....
doSomething();
....
}
It works, but now I have to control some click events. Something like this:
function main(param) {
var myVar;
function manageEvent(item) {
...
myVar = item.value;
...
}
....
item.onclick = function() { manageEvent(this) }
....
}
The problem is that manageEvent() has no access to myVar and I don't know how to solve the problem without rewriting all the code (really hard work). How can I manage the event in order to give "manageEvent" access to myVar?
It works: http://jsfiddle.net/kgmYM/
Your problem is somewhere else, it certainly is not in this code; it's perfectly fine. Try and see if what you're clicking actually has the same value; try and play with its value and see the result. But anyway, your posted code works, and without any further information, we can't find what's really wrong in your situation.
I have an object defined using literal notation as follows (example code used). This is in an external script file.
if (RF == null) var RF = {};
RF.Example= {
onDoSomething: function () { alert('Original Definition');} ,
method1 : function(){ RF.Example.onDoSomething(); }
}
In my .aspx page I have the following ..
$(document).ready(function () {
RF.Example.onDoSomething = function(){ alert('New Definition'); };
RF.Example.method1();
});
When the page loads the document.ready is called but the alert('Original Definition'); is only ever shown. Can someone point me in the right direction. I basically want to redefine the onDoSomething function. Thanks, Ben.
Edit
Thanks for the comments, I can see that is working. Would it matter that method1 is actually calling another method that takes the onDoSomething() function as a callback parameter? e.g.
method1 : function(){
RF.Example2.callbackFunction(function() {RF.Example.onDoSomething();});
}
Your code as quoted should work (and does: http://jsbin.com/uguva4), so something other than what's in your question is causing this behavior. For instance, if you're using any kind of JavaScript compiler (like Closure) or minifier or something, the names may be being changed, which case you're adding a new onDoSomething when the old one has been renamed. Alternately, perhaps the alert is being triggered by something else, not what you think is triggering it. Or something else may have grabbed a reference to the old onDoSomething (elsewhere in the external script, perhaps) and be using it directly, like this: http://jsbin.com/uguva4/2.
Thanks for the response .. in the end the answer was unrelated to the code posted. Cheers for verifying I wasn't going bonkers.