Differences between revisions 2 and 3
Revision 2 as of 2012-12-10 22:15:40
Size: 3341
Comment: Added events tests
Revision 3 as of 2012-12-10 22:16:20
Size: 3350
Comment:
Deletions are marked like this. Additions are marked like this.
Line 10: Line 10:
   var iabRef = null;             var iabRef = null;

   1        <script type="text/javascript">
   2             
   3             if (typeof String.prototype.startsWith != 'function') {
   4                 String.prototype.startsWith = function (str){
   5                     return this.slice(0, str.length) == str;
   6                 };
   7             }
   8             
   9             var iabRef = null;
  10                         
  11             function launchUrl() {
  12                 try {
  13                     var url = document.getElementById("windowUrl").value;
  14                     var target = getWindowName();
  15                     var showLocationbar = document.getElementById("windowOptions").checked;
  16                     
  17                     if (!url.startsWith('http://') && !url.startsWith('https://')) {
  18                         url = 'http://' + url;
  19                     }
  20                     
  21                     iabRef = window.open(url, target, "location=" + (showLocationbar? "yes":"no"));
  22                     iabRef.addEventListener('loadstart', onLoadStart);
  23                     iabRef.addEventListener('loadstart', onLoadStop);
  24                     iabRef.addEventListener('exit', onExit);
  25                 
  26                 } catch (e) {
  27                     alert(e);
  28                 }
  29             }
  30             
  31             function getWindowName()
  32             {
  33                 var radioButtons = document.getElementsByName("windowName");
  34                 for (var i = 0; i < radioButtons.length; ++i) {
  35                     if (radioButtons[i].checked) {
  36                         return radioButtons[i].value;
  37                     }
  38                 }
  39             }
  40             
  41             function checkUrlLength() {
  42                 var txt = document.getElementById("windowUrl");
  43                 var btn = document.getElementById("launchButton");
  44                 
  45                 btn.disabled = (txt.value.length == 0);
  46             }
  47                         
  48             function onLoadStart(event) {
  49                 alert(event.type + ' - ' + event.url);
  50             }
  51 
  52             function onLoadStop(event) {
  53                 alert(event.type + ' - ' + event.url);
  54             }
  55 
  56             function onExit(event) {
  57                 alert(event.type);
  58                 if (iabRef) {
  59                     iabRef.removeEventListener('loadstart', onLoadStart);
  60                     iabRef.removeEventListener('loadstart', onLoadStop);
  61                     iabRef.removeEventListener('exit', onExit);
  62                     iabRef = null;
  63                 }
  64             }
  65             
  66         </script>
  67         <div style="margin-left:10px">
  68             <div style="margin-top:20px">
  69                 <input type="radio" name="windowName" value="_self" />_self
  70                 <input type="radio" name="windowName" value="_blank" checked="checked" />_blank
  71                 <input type="radio" name="windowName" value="_system">_system
  72             </div>
  73             <div>
  74                 <input type="checkbox" id="windowOptions"/>Show location bar?
  75             </div>
  76             <div>
  77                 <input id="windowUrl" placeholder="Enter a url" onkeyup="checkUrlLength()" style="width:80%" type="text" id="windowUrl" value="www.google.com"/>
  78             </div>
  79             <button id="launchButton" onclick="launchUrl()">Launch InAppBrowser</button>
  80         </div>

InAppBrowserTest (last edited 2012-12-10 22:16:20 by ShazronAbdullah)