`

gwt中window.location.hash实现前进后退

 
阅读更多

ie8中支持onhashchange事件,在ie6中不支持,ie6使用一个iframe的open,将token记录在iframe中实现的。

以下是js版本:

<iframe src="javascript:''" id='__gwt_historyFrame' style='position: absolute; width: 0; height: 0; border: 0'></iframe>
<a href="javascript:myhistory.newItem('a')">a</a>
<a href="javascript:myhistory.newItem('b')">b</a>
<script>
var myhistory={
		historyFrame:null,
		callback:[],
		addCallback:function(callback){
			myhistory.callback[myhistory.callback.length] = callback;
		},
		init:function (){
				myhistory.historyFrame = document.getElementById('__gwt_historyFrame');
			
				var token = '';  
		    
		    // Get the initial token from the url's hash component.  
		    var href = window.location.href;
		    var hashLoc = href.lastIndexOf("#");
		    var token= (hashLoc > 0) ? href.substring(hashLoc+1) : "";
		    
		    myhistory.setToken(token);  
		    
		    var tokenElement;
		    if (myhistory.historyFrame.contentWindow) {
		    	var doc = myhistory.historyFrame.contentWindow.document;
		        tokenElement = doc.getElementById('__gwt_historyToken');
		    }
		   
		    if(tokenElement){
		    	myhistory.setToken(tokenElement.innerText);
		    }else{
		    	myhistory.navigateFrame(token);
		    }
		},
		__gwt_onHistoryLoad : function(token) {
			if(token){
				var hash = myhistory.encodeFragment(token);
				window.location.hash=hash;
				myhistory.newItemOnEvent(token);
			}
		},
		newItemOnEvent:function(token){
			for(var i = 0 ; i<myhistory.callback.length;i++){
				var call = myhistory.callback[i];
				call(token);
			}
		},
		decodeFragment:function(fragment){
		  	// decodeURI() does *not* decode the '#' character.  
		    return decodeURI(fragment.replace("%23", "#")); 
		},
		encodeFragment:function(fragment){
		  	// encodeURI() does *not* encode the '#' character.  
		    return encodeURI(fragment).replace("#", "%23");  
		},
		setToken:function(token){
			window.__gwt_historyToken = token;  
		},
		getToken:function(){
			return  window.__gwt_historyToken;
		},
		newItem:function(historyToken){
		    var historyToken = (historyToken == null) ? "" : historyToken;   
		    if (historyToken!=(myhistory.getToken())) {   
		    	myhistory.setToken(historyToken);   
		    	myhistory.nativeUpdate(historyToken);   
		        //if (issueEvent) {   
		        // fireHistoryChangedImpl(historyToken);   
		        //}   
		    }   
		},
		navigateFrame:function(token){
			var div = document.createElement('div');
			div.innerText=token;
		    var escaped = div.innerHTML;
			var doc = myhistory.historyFrame.contentWindow.document;
			doc.open();
		  	doc.write('<html><body onload="if(parent.myhistory.__gwt_onHistoryLoad)parent.myhistory.__gwt_onHistoryLoad(__gwt_historyToken.innerText)"><div id="__gwt_historyToken">' + escaped + '</div></body></html>');
		   	doc.close();
		},
		nativeUpdate:function(token){
			var hash = myhistory.encodeFragment(token);
			window.location.hash=hash;
			myhistory.navigateFrame(token);
		}
}
myhistory.addCallback(function mycall(token){	alert("hahah="+token);});
myhistory.init();
</script>

 

gwt实现history前进后退的代码:

 

http://code.google.com/p/google-web-toolkit/source/browse/trunk/user/src/com/google/gwt/user/client/impl/HistoryImpl.java?r=5606

 

/* 
  * Copyright 2008 Google Inc. 
  *  
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
  * use this file except in compliance with the License. You may obtain a copy of 
  * the License at 
  *  
  * http://www.apache.org/licenses/LICENSE-2.0 
  *  
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
  * License for the specific language governing permissions and limitations under 
  * the License. 
  */ 
 package com.google.gwt.user.client.impl; 
  
 import com.google.gwt.core.client.GWT; 
 import com.google.gwt.core.client.GWT.UncaughtExceptionHandler; 
 import com.google.gwt.event.logical.shared.HasValueChangeHandlers; 
 import com.google.gwt.event.logical.shared.ValueChangeEvent; 
 import com.google.gwt.event.logical.shared.ValueChangeHandler; 
 import com.google.gwt.event.shared.GwtEvent; 
 import com.google.gwt.event.shared.HandlerManager; 
 import com.google.gwt.event.shared.HandlerRegistration; 
 import com.google.gwt.event.shared.HasHandlers; 
  
 /** 
  * Native implementation associated with 
  * {@link com.google.gwt.user.client.History}. 
  * User classes should not use this class directly. 
  *  
  * <p> 
  * This base version uses the HTML5 standard window.onhashchange event to 
  * determine when the URL hash identifier changes. 
  * </p> 
  */ 
 public class HistoryImpl implements HasValueChangeHandlers<String>, 
     HasHandlers { 
  
   static boolean updateHashOnIE6 = true; 
  
   /** 
    * Sets whether the IE6 history implementation will update the URL hash when 
    * creating a new item. This should be used only for applications with large 
    * DOM structures that are suffering from performance problems when creating 
    * a new history item on IE6 and 7. 
    */ 
   public static void setUpdateHashOnIE6(boolean updateHash) { 
     HistoryImpl.updateHashOnIE6 = updateHash; 
   } 
  
   public static native String getToken() /*-{ 
     return $wnd.__gwt_historyToken || ""; 
   }-*/; 
  
   protected static native void setToken(String token) /*-{ 
     $wnd.__gwt_historyToken = token; 
   }-*/; 
  
   private HandlerManager handlers = new HandlerManager(null); 
  
   /** 
    * Adds a {@link ValueChangeEvent} handler to be informed of changes to the 
    * browser's history stack. 
    *  
    * @param handler the handler 
    */ 
   public HandlerRegistration addValueChangeHandler( 
       ValueChangeHandler<String> handler) { 
     return handlers.addHandler(ValueChangeEvent.getType(), handler); 
   } 
  
   public void fireEvent(GwtEvent<?> event) { 
     handlers.fireEvent(event); 
   } 
  
   /** 
    * Fires the {@link ValueChangeEvent} to all handlers with the given tokens. 
    */ 
   public void fireHistoryChangedImpl(String newToken) { 
     ValueChangeEvent.fire(this, newToken); 
   } 
  
   public HandlerManager getHandlers() { 
     return handlers; 
   } 
  
   public native boolean init() /*-{ 
     var token = ''; 
  
     // Get the initial token from the url's hash component. 
     var hash = $wnd.location.hash; 
     if (hash.length > 0) { 
       token = this.@com.google.gwt.user.client.impl.HistoryImpl::decodeFragment(Ljava/lang/String;)(hash.substring(1)); 
     } 
  
     @com.google.gwt.user.client.impl.HistoryImpl::setToken(Ljava/lang/String;)(token); 
  
     var historyImpl = this; 
     $wnd.onhashchange = function() { 
       var token = '', hash = $wnd.location.hash; 
       if (hash.length > 0) { 
         token = historyImpl.@com.google.gwt.user.client.impl.HistoryImpl::decodeFragment(Ljava/lang/String;)(hash.substring(1)); 
       } 
  
       historyImpl.@com.google.gwt.user.client.impl.HistoryImpl::newItemOnEvent(Ljava/lang/String;)(token); 
     }; 
  
     return true; 
   }-*/; 
  
   public final void newItem(String historyToken, boolean issueEvent) { 
     historyToken = (historyToken == null) ? "" : historyToken; 
     if (!historyToken.equals(getToken())) { 
       setToken(historyToken); 
       nativeUpdate(historyToken); 
       if (issueEvent) { 
         fireHistoryChangedImpl(historyToken); 
       } 
     } 
   } 
  
   public final void newItemOnEvent(String historyToken) { 
     historyToken = (historyToken == null) ? "" : historyToken; 
     if (!historyToken.equals(getToken())) { 
       setToken(historyToken); 
       nativeUpdateOnEvent(historyToken); 
       fireHistoryChanged(historyToken); 
     } 
   } 
  
   protected native String decodeFragment(String encodedFragment) /*-{ 
     // decodeURI() does *not* decode the '#' character. 
     return decodeURI(encodedFragment.replace("%23", "#")); 
   }-*/; 
  
   protected native String encodeFragment(String fragment) /*-{ 
     // encodeURI() does *not* encode the '#' character. 
     return encodeURI(fragment).replace("#", "%23"); 
   }-*/; 
  
   /** 
    * The standard updateHash implementation assigns to location.hash() with an 
    * encoded history token. 
    */ 
   protected native void nativeUpdate(String historyToken) /*-{ 
     $wnd.location.hash = this.@com.google.gwt.user.client.impl.HistoryImpl::encodeFragment(Ljava/lang/String;)(historyToken); 
   }-*/; 
  
   protected void nativeUpdateOnEvent(String historyToken) { 
     // Do nothing, the hash is already updated. 
   } 
  
   private void fireHistoryChanged(String newToken) { 
     UncaughtExceptionHandler handler = GWT.getUncaughtExceptionHandler(); 
     if (handler != null) { 
       fireHistoryChangedAndCatch(newToken, handler); 
     } else { 
       fireHistoryChangedImpl(newToken); 
     } 
   } 
  
   private void fireHistoryChangedAndCatch(String newToken, 
       UncaughtExceptionHandler handler) { 
     try { 
       fireHistoryChangedImpl(newToken); 
     } catch (Throwable e) { 
       handler.onUncaughtException(e); 
     } 
   } 
 } 
 
分享到:
评论

相关推荐

    gwt-2.7.0.part2.rar

    gwt-2.7.0.part2.rar 官网下载最新版,提供给大家使用

    GWT中文教程 .

    gwt-ext GWT.in.Action Google.Web.Toolkit.Applications

    org.jresearch.commons.gwt.utils.shared-1.0.45.zip

    通过解压并检查这些文件,可以深入了解`org.jresearch.commons.gwt.utils.shared`库的实现细节,例如: 1. **源代码**:通常位于`src/main/java`目录下,包含了项目的核心逻辑,按照包结构组织。 2. **构建脚本**:...

    gwt-2.7.0.part3.rar

    gwt-2.7.0.part3.rar 第三部分,大家记得下载前两部分啊

    gwt-servlet.jar

    gwt-servlet.jar 最新版,由于文件太大、不可以上传、有需要可以留言、整包分享给你、

    浪曦原创]GXT系列+第1讲+GXT_GWT的安装.

    浪曦原创]GXT系列+第1讲+GXT_GWT的安装.

    gwt-2.8.2.zip

    在GWT 2.8.2中,有几个关键的特性值得关注: 1. **编译优化**:GWT的编译器将Java代码转换为高效的JavaScript,使得浏览器能够运行。在2.8.2版本中,编译器进行了优化,提高了代码的压缩率和运行效率,减少了加载...

    gwt-user.jar

    gwt-user.jar 大小:2.86 MB

    gwt developer plugin.rar

    开发者插件则是用于支持GWT开发环境的重要工具,它允许在浏览器中直接运行和调试GWT应用。 描述 "gwt developer plugin" 明确了我们讨论的是GWT的开发者插件,这是一个用于提升GWT项目开发效率的工具。通常,这个...

    cypal.studio.for.gwt-2.alpha

    2. **in.cypal.studio.gwt.core_2.0.0.200903252317.jar**:这个文件可能是插件的核心组件,包含了对GWT核心特性的支持,如编译、调试、代码生成和性能优化等功能。它是cypal Studio功能实现的基础,为开发者提供了...

    GWT Designer 7.x 破解 注册机

    GWT Designer 7.x 是Google Web Toolkit的最佳可视化开发工具,所见即所得,即拖即用,使你能够非常容易地在可视化环境下开发AJAX应用程序。 GWT Designer 官方下载地址: ...

    gwt-2.0.4.part2.rar

    gwt-2.0.4.part2.rar gwt-2.0.4.part2.rar

    gwt-2.5.1.zip

    接着,创建一个GWT模块(.gwt.xml文件),在其中定义应用的设置,包括使用的GWT版本、主题、本地化等。 5. **编写GWT代码**: 开始编写你的GWT应用。在Java源代码中,创建用户界面元素,如 panels、widgets,并...

    GWT开发教程.pdf

    入口点类是GWT应用的核心,它实现了`com.google.gwt.core.client.EntryPoint`接口,并包含`onModuleLoad()`方法。当GWT应用加载时,会首先执行此方法。在这里,你可以编写初始化应用逻辑,包括创建用户界面组件、...

    配置GWT.ui.xml代码提示

    - 选择`Kind`为`System URI`,然后在`Location`中输入GWT的DTD或XSD文件路径,通常位于`&lt;GWT安装目录&gt;/public.gwt.xml`。 - 确保`System ID`字段填写正确,这通常是GWT的DTD或XSD的URL,例如:`...

    Manning.GWT.in.Practice.Apr.2008

    Manning.GWT.in.Practice.Apr.2008

    gwt-jar.rar

    GWT的核心理念是通过Java编译器将Java代码转换为高性能的JavaScript,从而在浏览器环境中运行。这样,开发者可以利用强大的Java生态系统,同时避免直接与JavaScript的复杂性打交道。 **GWT的主要特点:** 1. **...

Global site tag (gtag.js) - Google Analytics