I have some JavaScript code in an ExpressionEngine template and because it is in an EE template, you can include PHP inside the JavaScript to be parsed before the JavaScript is served to the client. I am setting a variable like this inside a $(document:).ready() function:
- $(document).ready(function() {
$(document).ajaxError(
function(e, jqxhr, settings, exception) {
final_message('We apologize for the inconvenience, the our website is currenty not able to process new registrations. Please try again later or call 800-555-1234');
}
);
<?php
// this code gets the current user PID from magento
include_once($_SERVER['DOCUMENT_ROOT'] . '/expressionengine/third_party/na_config.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/store/app/Mage.php');
umask(0);
Mage::app();
$pidsess = Mage::getSingleton('core/session', array('name'=>'frontend'));
$piddata = $pidsess->getNA_pid();
$pid = (isset($piddata["PID"])) ? $piddata["PID"]: 0;
echo "var userpid = $pid;\n";
?>
});
So the resulting JavaScript like like this:
- $(document).ready(function() {
$(document).ajaxError(
function(e, jqxhr, settings, exception) {
final_message('We apologize for the inconvenience, the our website is currenty not able to process new registrations. Please try again later or call 800-555-1234');
});
var userpid = 1007945;
});
Now, later in my script, I am testing the value of the userpid value like this:
- function logon_or_signup() {
//Logon before initial question
if(undefined === window.userpid) {
userpid = 0;
}
if(userpid == 0) {
// show login form
} else {
// do something else
}
}
In the if(undefined === window.userpid) line, the value of window.userpid is ALWAYS undefined.
Even if I eval userpid, it comes up undefined.
Why would that be since it is given a value in the $(document).ready() function? This is pretty critical to our entire application and it is making me pull my hair out.