Page 1 of 1

When is LONGRANGE javascript object available

Posted: Fri Mar 07, 2014 9:34 am
by Siyaman
In the Programming LongRange With JavaScript document most of the APIs use the LONGRANGE object to access the custom APIs. I'm developing an app on an iPad 2 with iOS 7 and found that the LONGRANGE object is not immediately available when the webview loads and hence all my resulting code did not execute. I ran a test using the following code and found that LONGRANGE was only available after the 3 second timeout.

Code: Select all

        <script type="text/javascript">
            setTimeout(function(){
                alert("After timeout " + typeof LONGRANGE);
            },3000);
        
            document.addEventListener("DOMContentLoaded", function(event) {
                alert("OnDOMLoad " + typeof LONGRANGE);
            });

             alert("Rightaway " + typeof LONGRANGE);

             // Output
             // Rightaway undefined
             // OnDOMLoad undefined
             // After timeout [object]
        </script>
Is there a LONGRANGE.ready function or something similar I can use to check when the LONGRANGE object is ready? Is there some other setting I can set to make LONGRANGE load faster?

Thank you,

Siyaman

Re: When is LONGRANGE javascript object available

Posted: Fri Mar 07, 2014 4:40 pm
by Pablo
Hi Siyaman,

this feature was undocumented in RV15. Below is what's in the current documentation:

When does LONGRANGE object become available?

When the HTML page is loading, the LONGRANGE object may not be available to your code. It is therefore essential that you reference LONGRANGE object only after it has become available, otherwise JavaScript exception would occur and the execution of your script will terminate prematurely.
When the LONGRANGE object becomes available to you, LongRange will invoke a global callback function onLongRangeReady(). It is a good practice to always define this function in your code.
It’s a good idea to initially have your UI in disabled state (for example, buttons not clickable) to stop your user from inadvertently doing an action that would trigger a script code that references the LONGRANGE object before it becomes available. You would then enable your UI in the onLongRangeReady function.