0

I am having problems setting the educationflag variable to 1. The problem I am solving is not to call enableEdit.php file if flag is 1.

I want to set the flag to 1 when the control comes in the if condition. Right now the control comes in the if condition but does not set the variable to 1. My code is printed below. I

var educationFlag=0;

function editEducation(class){

   //I send education in class variable. So class ='education';
   var condition=eval(class+'Flag');
   if ( condition == 0 ){
      $.ajax({
        url: "enableEdit.php",
        data: "class="+class,
        success: function(msg) {    
        }
    })
      eval(class+'Flag'=1);
   }
}

Thanks

10
  • 2
    I see, all I have to do is call the editEducation function with my malicious code in the argument. Ok, got it. Thanks :) (Not that I wouldn't be able to figure this out by viewing your source code directly on your page ... ) Commented Jun 16, 2011 at 11:33
  • It's possible I'm missing something but as far as I know "class" is used for defining a class. It looks like you want class to be your own personal variable for storing "education." If that's the case, you may be getting problems from misusing keywords as variables. Commented Jun 16, 2011 at 11:35
  • Also you hardcode the name of the function so no need to pass a variable unless you have a generic function editClass(...) Commented Jun 16, 2011 at 11:49
  • @MikeC there is no class in javascript. it's not a keyword Commented Jun 16, 2011 at 11:51
  • Any time you ever want to use eval(), you are almost certainly making a big mistake. eval() is a security risk, it causes performance issues, and it makes your code difficult to debug. But most of all, it is almost never necessary to actually use it: there is always a better way. It is occasionally useful, but never in situations like this. Commented Jun 16, 2011 at 11:52

3 Answers 3

3

Without trying to fix your underlying issues and stop the pollution of the global scope, this should work. Please note I have renamed the reserved word class to varPrefix

var educationFlag=0;

function editClass(varPrefix) {
   if (window[varPrefix+'Flag'] === 0 ){
      $.ajax({
        url: "enableEdit.php",
        data: "class="+varPrefix,
        success: function(msg) {    
          window[varPrefix+'Flag']=1;
        }
    })
  }
}

I send education in varPrefix variable. So varPrefix ='education';

editClass("education") ...

Sign up to request clarification or add additional context in comments.

3 Comments

You don't need a typeof check on the global variable because your accessing it as a property of window
You say that is why I do not get to the catch phrase here? jsfiddle.net/mplungjan/hj9LX
yes. If you try fooFlag it will throw an exception. If you try window.fooFlag it will just return undefined. Only accessing undeclared variables throws errors. Accessing properties that don't exist just returns undefined
1

What is the value of the class variable that is passed into the function? I'll make an educated guess that it's going to be something like education? Is that right?

In that case, the first eval() will effectively be this:

var condition=eval('educationFlag');

and the second will be this:

eval('educationFlag'=1);

The second is failing because the =1 part is not in the string. That's the short answer, which directly solves your problem.

However, the better answer is that eval() is the wrong thing to use. You should never use eval() for this kind of thing. Since educationFlag is a global variable, you can access it via the window object as an array element: this means you can do exactly the same thing, without having to use risky eval() at all.

You can reference it like this: window['educationFlag']

Therefore, your eval() lines can be replaced like this:

var condition=window[class+'Flag'];

and

window[class+'Flag']=1;

Hope that helps.

3 Comments

Yes it does help. See my identical answer (which does not use the reserved word class)
@mplungjan -- yes, I spotted it, and gave you +1. I've left my answer in place because I've given more explanation of the issue than your answer.
Right, I missed the Flag'=1) instead of Flag=1') from your answer
0

I don't understand what you're trying to do in your code... You say you want to "not call enableEdit.php if flag is 1". If that's the case, why can't you just do:

if(flag == 1) { ... }

And get rid of your eval statements?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.