HTML <a> anchor issue with IE6 / History

4 views
Skip to first unread message

Jean-Lou Dupont

unread,
Sep 26, 2008, 2:46:27 PM9/26/08
to Google Web Toolkit
It seems that putting a simple <a> HTML anchor does not work as
expected i.e. when the user clicks on the anchor, IE6's location bar
changes BUT the "onHistoryChanged" event is NOT fired. Works fine on
Chrome and FF. I am pretty certain this used to work on GWT 1.4 .

Is there a work-around?

Isaac Truett

unread,
Sep 26, 2008, 4:03:53 PM9/26/08
to Google-We...@googlegroups.com
There are a few things you could do. Here are a couple of options:

1. Find <a> tags in your HTML and replace them with Hyperlink widgets.
2. Use JSNI to expose History.newItem() and call that method from your
<a> tags onClick.

Jean-Lou Dupont

unread,
Sep 26, 2008, 4:07:11 PM9/26/08
to Google Web Toolkit
Found a work-around: fix-up the HTML anchors:

<code>
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NodeList;

/**
* Fixes HTML <a> element for IE's shortcoming
* when it comes to triggering History's 'onHistoryChanged' event.
*
* @author Jean-Lou Dupont
*
*/
public class AnchorsUtil {

public static void updateAnchors() {

Document doc = Document.get();
NodeList<Element> anchors = doc.getElementsByTagName("a");

if (null==anchors) return;

for(int i=0;i<anchors.getLength();i++) {
Element e = anchors.getItem(i);
//make sure we are dealing with a local link
String href = e.getAttribute( "href" );
if (href.indexOf('#') != -1)
fixForHistory(e);
}

}//
protected static native void fixForHistory(Element e) /*-{

e.onclick = function() {
var href = e.href.split('#')[1];
@com.google.gwt.user.client.History::newItem(Ljava/lang/String;)
(href);
}

}-*/;

}//END
</code>

Jean-Lou Dupont

unread,
Sep 26, 2008, 4:10:45 PM9/26/08
to Google Web Toolkit
Thanks Isaac.
on #1) I have played quite enough with trying to dynamically link DOM
elements to GWT widgets and let me tell you it is not a pretty sight
on #2) I must have posted my solution at the ~same time you posted
this suggestion!

On Sep 26, 4:03 pm, "Isaac Truett" <itru...@gmail.com> wrote:
> There are a few things you could do. Here are a couple of options:
>
> 1. Find <a> tags in your HTML and replace them with Hyperlink widgets.
> 2. Use JSNI to expose History.newItem() and call that method from your
> <a> tags onClick.
>
> On Fri, Sep 26, 2008 at 2:46 PM, Jean-Lou Dupont
>

Davsket

unread,
Sep 27, 2008, 10:45:15 AM9/27/08
to Google Web Toolkit
Wow, nice code, it inspires me to do other fixes for IE, thnx.
Reply all
Reply to author
Forward
0 new messages