I have a web page that includes the following javascript, but the if statement does not work. I have tried changing the = sign in the if statement to ==, but that causes the EMPL_MENU to not execute, and also not show any messages in the apache2 error log. Once I get this running, the arguments to the user variable will be dynamic.
<SCRIPT language=JavaScript src="../scripts/menu_data_EMPL_MAIN.js" type=text/javascript>
var user = "DRL";</SCRIPT>
and in menu_data_EMPL_MAIN.js
if ( user = "DAVID" ){
aI("text=16;url=pyrllist.php?PYRL=16" );
}
I want to pass the value for user to the menu script. But the menu script ignores the if statement and displays the text=16 line, without showing an error in the log. Essentially restrict access to the management payroll.
An issue lies in your use of the assignment operator = instead of the equality operator == or strict equality operator === in the if statement.
When you use =, you are assigning the value "DAVID" to the variable user rather than checking if it is equal to "DAVID". This causes the condition to always evaluate as true.
To fix this issue, try replacing= with === for strict equality comparison.
Additionally, you should consider the following to insure your user variable is assigned before the script is loaded:
<script>
var user = "DRL";
</script>
<script language="JavaScript" src="../scripts/menu_data_EMPL_MAIN.js" type="text/javascript"></script>
Also, language="JavaScript" is (long) obsolete and you don't need it.
<script>
var user = "DRL";
</script>
<script src="../scripts/menu_data_EMPL_MAIN.js" type="text/javascript"></script>
Also, "text/javascript" is also optional (and not necessary) , so further simplify to:
<script>
var user = "DRL";
</script>
<script src="../scripts/menu_data_EMPL_MAIN.js" ></script>
I changed the = to ==, and also moved the initial assignment of user to a separate script as in the last example. And I have it working. The next step is to make the assignment of user dynamic.
The original script was written 20 years ago (not by me) so that probably explains the obsolete/deprecated code.
Thanks.
Jack