Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-3370] iOS: WebView: fires load event multiple times

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2013-09-06T16:51:36.000+0000
Affected Version/sn/a
Fix Version/sRelease 3.1.0, 2013 Sprint 05 API, 2013 Sprint 18, 2013 Sprint 18 API, Release 3.2.0
ComponentsiOS
Labelsapi, event, load, module_webview, parity, qe-testadded, webview
Reportertimothyf
AssigneeSabil Rahim
Created2011-04-15T03:43:23.000+0000
Updated2013-11-25T08:49:14.000+0000

Description

I am using the following code to listen for the load event of a WebView:
   webView.addEventListener('load', function(evt) {
        mapView.evalJS("alert('" + evt.url + "');");
    });</code>
When running this in the iPhone simulator, the load event appears to be getting fired 10 times. This listener is triggered 10 times and thus I get 10 alert's that pop up after the WebView has loaded. *Update: The original PR created a side effect of if the remote webpage loaded uses JavaScript to POST a form to itself (i.e. the same URL), the WebView load event never fires.*

Comments

  1. Dustin Hyde 2012-03-14

    Bug still occurs on both iPhone/iPad. Load event is fired twice. Does not occur on Android (load event fires once correctly). SDK: 1.8.2 githash=59b3a90s, 2.0.0.v20120314120250 Studio: 2.0.0.201203121914 OS: Snow Leopard Devices Tested: iPad 4.3.5, iPhone Simulator 5.0 Steps to Reproduce: 1. Run code, press 'Open Google'.
       var win = Ti.UI.createWindow({
       	backgroundColor:'blue'
       });
       
       var buttonGoogle = Ti.UI.createButton({
       	title:'Open Google',
       	top:'20%',
       	height:'30%',
       	left:'10%',
       	right:'10%'
       });
       
       buttonGoogle.addEventListener('click', function(){
       	var webView = Ti.UI.createWebView({
       			url:'http://www.google.com'
       		});
       	
       	webView.addEventListener('load', function(){
       		alert('load: ' + webView.url);
       		});
       	
       	webView.addEventListener('beforeload', function(){
       		alert('beforeload: ' + webView.url);
       		});
       		
       	webView.addEventListener('error', function(){
       		alert('error: ' + webView.url);
       		});
       		
       	win.add(webView);
       	
       	var button = Ti.UI.createButton({
       		title:'Close',
       		right:0,
       		bottom:0,
       		height:'10%',
       		width:'20%'
       	});
       	
       	button.addEventListener('click', function(){
       		win.remove(webView);
       	});
       	
       	webView.add(button);
       });
       
       win.add(buttonGoogle);
       
       var buttonInvalid = Ti.UI.createButton({
       	title:'Open Invalid URL',
       	bottom:'20%',
       	height:'30%',
       	left:'10%',
       	right:'10%'
       });
       
       buttonInvalid.addEventListener('click', function(){
       	var webView = Ti.UI.createWebView({
       			url:'http://xxx'
       		});
       	
       	webView.addEventListener('load', function(){
       		alert('load: ' + webView.url);
       		});
       	
       	webView.addEventListener('beforeload', function(){
       		alert('beforeload: ' + webView.url);
       		});
       		
       	webView.addEventListener('error', function(){
       		alert('error: ' + webView.url);
       		});
       		
       	win.add(webView);
       	
       	var button = Ti.UI.createButton({
       		title:'Close',
       		right:0,
       		bottom:0,
       		height:'10%',
       		width:'20%'
       	});
       	
       	button.addEventListener('click', function(){
       		win.remove(webView);
       	});
       	
       	webView.add(button);
       });
       
       win.add(buttonInvalid);
       
       win.open();
       
    Expected Result: Only one load event should fire. Actual Result: Two load events fire.
  2. James Thompson 2012-04-20

    I am seeing this same issue on iOS under 1.8.2. It appears to only happen under certain conditions though. Attempting to isolate those conditions, thinking it may be the result of DOM changes within the WebView content.
  3. Vishal Duggal 2013-02-26

    PR https://github.com/appcelerator/titanium_mobile/pull/3909
  4. Anshu Mittal 2013-03-14

    Tested with: SDK: 3.1.0.v20130313215655 Studio: 3.0.2.201302191606 Device: iPad2(v 5.1) OS: OSX 10.7.5 load event is fired only once as expected.
  5. Shannon Hicks 2013-04-18

    This created an equally bad side-effect. If the remote webpage loaded uses JavaScript to POST a form to itself (i.e. the same URL), the WebView load event never fires.
  6. Shannon Hicks 2013-04-18

    Looking at the PR, I'm guessing that any form of navigating back to the same URL probably will have the same issue.
  7. Ingo Muschenetz 2013-05-15

    [~vduggal] Can you please investigate?
  8. Vishal Duggal 2013-05-15

    Probably should check for navigationType in shouldStartLoad delegate method and clear out the history is nav type is anything other than UIWebViewNavigationTypeOther
  9. Shannon Hicks 2013-05-16

    I think that makes sense. The simplest test would be to create a HTML form that posts to itself. The next step up would be a link that uses javascript to reload a page.
  10. Shannon Hicks 2013-06-21

    Marked as resolved fixed, but without any additional code changes or tickets? I think this one slipped past QA.
  11. Ingo Muschenetz 2013-07-18

    It appears we didn't resolve the secondary issue brought up later. Reopening and scheduling for 3.2.0
  12. Anshu Mittal 2013-08-13

    Issue occurs on the following environment: SDK: 3.1.2.v20130809141556 Appcelertaor Studio: 3.1.2.201308091728 OS: OSX 10.8.4 Device: iPhone5(v 6.1.4) Xcode: 4.6.3
  13. Shannon Hicks 2013-09-05

    Wait, how could this possibly have been fixed without any code changes?
  14. Shawn Lipscomb 2013-09-11

    Is there any workaround to the secondary problem that Shannon brought up? Any method of the webview that I can call to "reset" things so that the load event will refire when the javascript reloads the same page? I'm using 3.1.2.GA.
  15. Pragya Rastogi 2013-10-15

    Tested With: OSX: 10.8.5 Xcode:5.0 Appcelerator Studio: 3.2.0.201310112258 SDK:3.2.0.v20131014204636 acs:1.0.7 alloy:1.2.2 npm:1.3.2 titanium:3.2.0 titanium-code-processo:1.0.3 Device: iPod touch2 (viOS7)
  16. Ingo Muschenetz 2013-10-29

    [~bitshftr] Following up here. Did a second ticket get created for the additional problem you were inquiring about?
  17. Shawn Lipscomb 2013-10-29

    Ingo, are you talking about this: "If the remote webpage loaded uses JavaScript to POST a form to itself (i.e. the same URL), the WebView load event never fires." ? If so, then no, I was assuming that this was part of the 3.2.0 fix for this ticket. Otherwise, please remind me what the "additional problem" was.
  18. jithinpv 2013-11-25

    Anvil test case added. PR link: https://github.com/appcelerator/titanium_mobile/pull/5020

JSON Source