12 Replies Latest reply: Dec 21, 2011 1:42 PM by 875687 RSS

    Assigning JavaScript variable to Application Item

    875687
      Can someone please suggest how I can assign the value of a javascript variable declared in the page "Function and Global Variable Declaration" area of a Page Property to a Page Item or Application Item (global item)?

      I added a javascript function - get_myvar - that gets the browser timezone in a JS function declared on the page HTML Header area.

      Then, declared a JavaScript variable declared on the "Javascript" tab of the page property on the main login page 101

      var myvar = jstz.get_myvar();

      Now, I want to assign the value of 'myvar' to a page item P101_MYVAR value needs to be set. Ideally I want to assign this in the main page 101 Login Page Process.

      Please advice and thanks in advance.

      SK
        • 1. Re: Assigning JavaScript variable to Application Item
          jariola
          Hi,

          Create unprotected hidden item to your login page and use Apex JavaScript API function $s to set item value.

          Or use Ajax/dynamic action to set item session before page submit.

          Regards,
          Jari
          • 2. Re: Assigning JavaScript variable to Application Item
            875687
            Thanks for your reply...I may be asking some basic question. Appreciate if you could give an example as to how to set using $s(pNd,pValue) - where should it be invoked?

            My main javascript to get time zone is on page101 header section. I'm having an other function like this...
            <script type="text/javascript">
            function setTimeZone()
            {
            var tz = jstz.determine_timezone();
            document.getElementById('P101_TIMEZONE').value = jstz.determine_timezone(); //The jstz is the main java script that gets the TZ.
            // $s(document.getElementById('P101_TIMEZONE'), tz)
            }
            </script>

            I'm trying using ajax/dynamic action to invoke the setTimZone() java script. Didn't have good luck. May be I'm missing something fundamental here...Please advice.

            SK
            • 3. Re: Assigning JavaScript variable to Application Item
              jariola
              Hi,

              Example of $s function
              <script type="text/javascript">
              function setTimeZone(){
               var tz = jstz.determine_timezone();
               $s("P101_TIMEZONE", tz)
              }
              </script>
              When you set value to item before user have login, value is stored to session state on session before login.
              I think you need set/get timezone after login.

              Do you have sample about problem on apex.oracle.com?

              Regards,
              Jari

              http://dbswh.webhop.net/dbswh/f?p=BLOG:HOME:0
              • 4. Re: Assigning JavaScript variable to Application Item
                875687
                Hi Jarola - Per your suggestion, I was able to capture the browser timezone to a page hidden item. The item value is being set correctly using the $s() JavaScript Call - But I'm having difficulty while attempting to use the page item in Setting the session timezone..Somehow the item seems to be cleard upon page load.

                This is what I did - I set the following JavaScript code on the "Page Property (on page 101 login page) > JavaScript (tab) > Execute When Page Loads" setting as follows...

                var now = new Date();
                $s('P101_TIMEZONE', now.getTimezoneOffset());

                I displayed the P101_TIMEZONE on the login page, when the login page is being presented, which shows what I'm expecting - 480 minutes (8 hours Pacific Time) from (less than) UTC time...

                However, This step doesn't seem to be setting the sesssion timezone correctly...

                I defined a PageLoad-AfterHeader Process and have this code..
                -----
                DECLARE
                lv_tz_offset VARCHAR2(20) DEFAULT '-08:00'; --Pacific Time
                BEGIN
                wwv_flow.debug('*** p101_timezone: '||v('P101_TIMEZONE'));
                lv_tz_offset := -1*ROUND((v('P101_TIMEZONE')/60),2);
                --
                wwv_flow.debug('*** Setting offset: '||lv_tz_offset);
                APEX_UTIL.SET_SESSION_TIME_ZONE( P_TIME_ZONE => lv_tz_offset);
                EXCEPTION WHEN OTHERS THEN
                wwv_flow.debug('*** Setting exception offset: '||lv_tz_offset);
                APEX_UTIL.SET_SESSION_TIME_ZONE( P_TIME_ZONE => '-08:00');
                END;
                ----
                When I saw the debug messages, the P101_TIMEZONE shows NULL value, but during the Initial Page Load process, the value showed 480, that i'm computing to set the apex_util.set_session_time_zone => -8 (as you can see in the above code).
                --

                I tried moving this code to a Dynamic Action to run the PLSQL code above to run at 'PageLoad' time, and also during Login-Page-Submit Process. In both cases, the P101_TIMEZONE value is NULL.

                Please advice, if I'm missing something here or any other tips...Thanks for your time.

                SK
                • 5. Re: Assigning JavaScript variable to Application Item
                  jariola
                  Hi,

                  Please create example of problem to apex.oracle.com

                  Regards,
                  Jari

                  http://dbswh.webhop.net/dbswh/f?p=BLOG:HOME
                  • 6. Re: Assigning JavaScript variable to Application Item
                    875687
                    Hi Jarola - I uploaded my app to apex.oracle.com, but when I run it all the images are not displayed. I'm able to run it as a developer.

                    Tried to create a public access login to it..Here's the
                    URL: http://apex.oracle.com/pls/apex/f?p=59975:1

                    Also, I uploaded my sample apex app. to my online drive - If you could please take a look I'd appreciate it..
                    https://skydrive.live.com/redir.aspx?cid=5b25eaccb9705dd6&resid=5B25EACCB9705DD6!1043&parid=root

                    I did the following in this app...

                    1. Defined P101_TZ on page 101

                    2. Javascript tab on 'Page Property for 101'

                    Execute When Page Loads
                    var now = new Date();
                    $s('P101_TZ', now.getTimezoneOffset());

                    3. Login Process

                    Trying to access P101_TZ (which shows value of 480) and use it to set the session_time_zone

                    4. Page 1 - upon login

                    on load page header - apex_util.get_session_time_zone;

                    5. print it but not showing correctly

                    Problem - Why is it that the page item P101_TZ not available, upon initial load, so that I can use it for setting the session time zone...To set the session time zone, which is the best place (processs or event) I should choose to execute apex_util.set_session_time_zone( by_passing_the_P101_tz_value); ??

                    Please advice.
                    SK
                    • 7. Re: Assigning JavaScript variable to Application Item
                      jariola
                      Hi,

                      What I can see, you do set item session state before user authenticate.
                      After authentication session will change and value in session state is lost.

                      As I did say, you need get and set timezone after user login.
                      Use e.g. page zero to have needed JavaScript that run conditionally, if TZ item do not have value.

                      Regards,
                      Jari
                      • 8. Re: Assigning JavaScript variable to Application Item
                        Patrick Wolf-Oracle
                        Hi SK,

                        I'm just curiose why you don't use the the "Automatic Time Zone" detection feature of APEX (Edit Application Attributes -> Globalization)? Does it not work for you?

                        Regards
                        Patrick
                        -----------
                        My Blog: http://www.inside-oracle-apex.com
                        APEX Plug-Ins: http://apex.oracle.com/plugins
                        Twitter: http://www.twitter.com/patrickwolf
                        • 9. Re: Assigning JavaScript variable to Application Item
                          jariola
                          Hi Patrick,

                          I did propose that to user in other post
                          Browser Time Zone Setting

                          Regards,
                          Jari
                          • 10. Re: Assigning JavaScript variable to Application Item
                            875687
                            Patrick - Thanks for your tip..The Globalization feature is quite awesome and solves the problem..However, I'm trying to avoid the 'Set Session Time Zone' initial page message that shows up, before P101 is presented, if I'm setting that 'Automatic Time Zone = Yes'. Since this is a commercial product I'm working on using Apex, I'm trying to avoid that message by the Java Script approach.. Thanks for your time and guidance.

                            SK
                            • 11. Re: Assigning JavaScript variable to Application Item
                              875687
                              Thanks Jari for your guidance - I'll look into implementing this on Page 0.
                              • 12. Re: Assigning JavaScript variable to Application Item
                                875687
                                Posting the implementation code here for anyone having similar implementations.

                                Multiple Steps to implement the computatio of TZ offset
                                1. Define JavaScript to capture the browser timezone into an application item
                                2. Have 2-step TrueActions on a Dynamic Action defined
                                3. Compute the captred timezone offset in a 'OnSubmit BeforeComputations' Procedure on P101
                                4. Display on Region 8 the timezone offset value. This is done on Page0 to show TZ offset on all pages of the app
                                ----
                                Step1 - Define JavaScript on P101 - 'JavaScript' tab - "Execute When Page Loads" area
                                ----
                                var now = new Date();
                                $s('P101_TIMEZONE', now.getTimezoneOffset());
                                //set the value of browser variable to an application item
                                $s('APPVAR_TIMEZONE', now.getTimezoneOffset());

                                ----
                                Step2 - Define 2 step DynamicAction with 2 TrueActions steps on P101 main login page
                                ----
                                Step1 - ExecuteJavaScriptCode

                                var now = new Date();
                                $s('P101_TIMEZONE', now.getTimezoneOffset());

                                Step2 - Execute PLSQL Code

                                APEX_UTIL.SET_SESSION_STATE('APPVAR_TIMEZONE',v('P101_TIMEZONE'));

                                -----
                                Step 3 - Compute the TimeZone Offset in a Page Process on P101 - OnSubmit BEFORE ComputationsValidations
                                -----
                                --//This procedure sets the TimeZone Offset and Apex Session TimeZone
                                --//See the Dynamic Action at Page Load and 'JavaScript' Execute on Page load
                                --//on Page Properties to see how P101_TIMEZONE value is begin assigned from browser timezone
                                DECLARE
                                v_tzoffset VARCHAR2(20) DEFAULT NVL(V('P101_TIMEZONE'),V('V_TIMEZONE'));
                                vf VARCHAR2(5);
                                BEGIN
                                --//debugging --wwv_flow.debug('*** p101_timezone: '||v('P101_TIMEZONE'));
                                --//debugging --wwv_flow.debug('*** v_timezone: '||v('V_TIMEZONE'));
                                --
                                IF substr(v_tzoffset,1,1) = '-' then
                                vf := '+';
                                v_tzoffset := substr(v_tzoffset,2);
                                ELSE
                                vf := '-';
                                END IF;

                                --
                                --//Compute the Actual TimeZone Offset with a ':' seperated value divisible by minutes
                                v_tzoffset := vf||lpad(to_char(floor(to_number(v_tzoffset)/60)), 2, '0') || ':' ||
                                lpad(to_char(mod(to_number(v_tzoffset),60)), 2, '0');
                                --
                                --//debugging --wwv_flow.debug('*** Setting Session TimeZone - v_tzoffset AFTER: '||v_tzoffset);
                                --
                                APEX_UTIL.SET_SESSION_TIME_ZONE( P_TIME_ZONE => NVL(v_tzoffset,'-08:00'));
                                --
                                APEX_UTIL.SET_SESSION_STATE('APPVAR_TZOFFSET',v_tzoffset);

                                --//Set database session timezone
                                begin
                                execute immediate 'alter session set time_zone='||v_tzoffset;
                                end;
                                EXCEPTION WHEN OTHERS THEN
                                NULL;
                                APEX_UTIL.SET_SESSION_TIME_ZONE( P_TIME_ZONE => '-08:00'); --default to Pacific Time or some hardcoded offset
                                END;
                                ------
                                Step 4 - Display TimeZone offset on Page0 - Region 8 - NavBar - Computation Type: PLSQL Anonymous Block - printing the TZ offset in the following
                                -----
                                --//Display TZ Offset on NavBar Region
                                --//compute and display/print tz offset on a pag0 process
                                declare
                                v_tz VARCHAR2(20) := APEX_UTIL.GET_SESSION_TIME_ZONE;
                                begin
                                :APPVAR_TIMEZONE := NVL(v('APPVAR_TIMEZONE'),v_tz);
                                htp.p(wwv_flow_lang.system_message('TIMEZONE', :APPVAR_TZOFFSET));
                                end;