Need Javascript to get Day of week back to Lectora

lby
lby Community Member Posts: 21
I need to get the day of the week and I believe that I need to use an action to run JS like this:
var d = new Date();
var n = d.getDay();
which would yield the day of the week for n, e.g. returns a 3 for Wednesday.

Question is how can I get the variable n back into Lectora and assign it a Lectora variable?
If anyone has this tip, I would appreciate it.
Thanks,
Louie

Comments

  • mnotermans5114
    mnotermans5114 Community Member Posts: 916
    if your variable in Lectora is called 'myLectoraVariable'... you can set that variable from Javascript by
    VarmyLectoraVariable.set("some value or string you want to pass");

    When you download eg. my setup  http://community.trivantis.com/shared-content/moving-with-variables/

    In the Javascript file you find that syntax too... ...eg: Varpos4X.set(posCross4.offsetLeft);

     

    Main thing to remember is Lectora passes all values as strings.

    ~Math
  • klaatu
    klaatu Community Member Posts: 988
    You'll have to use a little JavaScript to convert the returned getDay index to the actual name of the weekday. In this example I have a Lectora variable called "whatDay". This can be written more elegantly but it works.

    var daysOfTheWeek = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];

    today = new Date();

    thisDay=today.getDay();

    thisDay=daysOfTheWeek[thisDay];

    VarwhatDay.set(thisDay);
  • klaatu
    klaatu Community Member Posts: 988
    By the way, this is a question, not a tip, and would be better suited in the questions and answers forum for the future. ;-)

     
  • lby
    lby Community Member Posts: 21
    Thanks Math.
    I tried to set up an action
    OnShow
    Modify Variable "LectoraDay"
    Value: Javascript:WeekDay()

    and then wrote the External HTML Object:

    <script language = "JavaScript">
    function WeekDay(){
    var d = new Date();
    var n = d.getDay();
    varLectoraDay.set"(n)";
    }
    </script>


    also tried:
    varLectoraDay.set"(var n)";
    varLectoraDay.set"(d.getDay())";

    but I'm not successful...Am I doing the right thing?
    "n" is supposed to return the numerical day of the week.
    Any suggestions on how I can fix this is really appreciated.
    Thanks Math.
  • mnotermans5114
    mnotermans5114 Community Member Posts: 916
    Quick tip: Add alerts on specific points in your script... either by showing a Lectora message or using console.log("blablaba") or alert("blablabla"). This way you often find errors in your logic....
  • klaatu
    klaatu Community Member Posts: 988
    <pre class="syntaxbox">The language attribute was deprecated somewhere in 2008, I believe.
    You also might want to learn about parseInt.