PDA

View Full Version : Disabling Links and/or Redirecting Links in HTMLLoader


kcreation
10-30-2009, 09:43 PM
Here is some helpful code for disabling and redirecting links in the HTMLLoader:


import flash.html.*;
import flash.net.URLRequest;
import fl.controls.UIScrollBar;
import fl.controls.ScrollBarDirection;
import fl.events.ScrollEvent;

//set the url of the page we want to load
var url:String = "http://www.actionscript.org";

//create a new htmlloader object
var htmlObj:HTMLLoader = new HTMLLoader();
htmlObj.width = stage.stageWidth;
htmlObj.height = stage.stageHeight;
htmlObj.load(new URLRequest(url));
addChild(htmlObj);

//hide loader until the links have been changed
htmlObj.visible = false;

//setup listener to modify html through javascript after loading
htmlObj.addEventListener(Event.COMPLETE, htmlLoaded);
function htmlLoaded(event:Event):void {
var hLdr:HTMLLoader = event.target as HTMLLoader;
//setup redirect function
htmlObj.window.redirectLink = redirectLink;
var numLinks:Number = hLdr.window.document.links.length;
for(var i:int = 0; i < numLinks; i++) {
hLdr.window.document.links[i].setAttribute("onclick","redirectLink(this.href, document.URL);return false");
}
trace("Links Disabled. Num:", numLinks);
//show html loader since the link have been disabled
hLdr.visible = true;
}

function redirectLink(hRef:String="", pageUrl:String=""):void {
trace("hRef:", hRef, " pageUrl:", pageUrl);
//hide loader for new redirect
htmlObj.visible = false;
//new link
var newUrl:String = "http://www.actionscript.org";
//redirect link
htmlObj.load(new URLRequest(newUrl));
}