Explore the community Forums Lectora Tips and tricks Need Javascript to get Day of week back to Lectora

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #360123 Score: 0
    Profile photo of LBY
    LBY
    Member
    beginner
    intermediate
    curious george
    wise owl
    contributor
    @LBY

    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

    #360163 Score: 1
    Profile photo of Math Notermans
    Math Notermans
    Member
    contributor
    intermediate
    advanced
    friend finder
    junior moderator
    advocate
    LUC16 Attendee
    LUC16
    wise owl
    curious george
    129 pts
    @mnotermans5114

    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  https://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

    This post has received 1 vote up.
    #360167 Score: 1
    Profile photo of Darrel Somoza
    Darrel Somoza
    Member
    contributor
    friend finder
    beginner
    intermediate
    junior moderator
    advanced
    wise owl
    curious george
    profile
    209 pts
    @Klaatu

    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);

    This post has received 1 vote up.
    • This reply was modified 3 years, 1 month ago by Profile photo of Darrel Somoza Darrel Somoza. Reason: typo
    Attachments:
    1. getWeekdayName.zip
    #360170 Score: 0
    Profile photo of Darrel Somoza
    Darrel Somoza
    Member
    contributor
    friend finder
    beginner
    intermediate
    junior moderator
    advanced
    wise owl
    curious george
    profile
    209 pts
    @Klaatu

    By the way, this is a question, not a tip, and would be better suited in the questions and answers forum for the future. 😉

     

    #360174 Score: 0
    Profile photo of LBY
    LBY
    Member
    beginner
    intermediate
    curious george
    wise owl
    contributor
    @LBY

    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.

    #360189 Score: 0
    Profile photo of Math Notermans
    Math Notermans
    Member
    contributor
    intermediate
    advanced
    friend finder
    junior moderator
    advocate
    LUC16 Attendee
    LUC16
    wise owl
    curious george
    129 pts
    @mnotermans5114

    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….

    #360204 Score: 0
    Profile photo of Darrel Somoza
    Darrel Somoza
    Member
    contributor
    friend finder
    beginner
    intermediate
    junior moderator
    advanced
    wise owl
    curious george
    profile
    209 pts
    @Klaatu

    <pre class=”syntaxbox”>The language attribute was deprecated somewhere in 2008, I believe.
    You also might want to learn about parseInt.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.