Skip to main content
Known Participant
July 26, 2016
Question

stagewebview changing preventDefault

  • July 26, 2016
  • 4 replies
  • 1003 views

I have a stagewebview using the native browser. I have an event listener on LOCATION_CHANGING.  In the location changing event I trace the new location and also call event.preventDefault();.

The function is getting called as I navigate the page because I am getting my new location url trace but the event.preventDefault is not stopping the page from changing. Shouldn't that prevent the page from actually navigating to the new url?

This topic has been closed for replies.

4 replies

Known Participant
July 27, 2016

looks like even if I don't call preventDefault() when using an invalid url like e://e.com?type=save&name=test, the page doesn't change. I am not sure how other systems will handle this.

Participating Frequently
November 30, 2017

Your workaround seems to work, but it's not without faults.

The page I'm hosting in a webview does some XHR network requests, when one of them completes I want to notify my AS code, using LOCATION_CHANGING event listener (and an invalid url like in your example, or `#data=....` url which also works). However, preventDefault() doesn't work correctly, and the other ongoing active XHR network requests are canceled (they return status code 0).

This breaks the functionality of the JS library I'm using.

I'm thinking about making a primitive WebSocket server in AS (using SocketServer) on 127.0.0.1, then connecting to it from JS and using that as JS-to-AS communications channel.

Known Participant
July 27, 2016

So I found a strange work around. If I have a valid web url like http://etherdcp.com?type=save&name=test then I cannot prevent the page from changing (event.preventDefault). If I change the url to e://e.com?type=save&name=test , then I am able to prevent the page from changing and still trigger the location_changing event. This work around will work in my case because I am simply trying to grab the parameters. I hope this helps someone else.

Known Participant
July 26, 2016

Below is my code. I am on a mac, I tried air 21 and 22 with the same result. I get my trace but the webpage continues to navigate.

webView = new StageWebView(true);

webView.stage = this.stage;

webView.viewPort = new Rectangle(0,0, _stageWidth, _stageHeight);

webView.addEventListener(LocationChangeEvent.LOCATION_CHANGING, getWebViewData);

webView.loadURL(fileURL);

private function getWebViewData(event:LocationChangeEvent):void

  {

  event.preventDefault();

  trace(event.location);

}

javascript in the html calling the page change

window.location.href = 'http://etherdcp.com?type=save&name=test';

Colin Holgate
Inspiring
July 26, 2016

Yes it should stop it. Can you show your code?