您可以使用JavaScript和jQuery設置窗口和餅乾的滾動位置存儲滾動到的位置。在頁面與搜索結果的JavaScript,你可以有這樣的事情:
var COOKIE_NAME = "scrollPosition";
$(document).ready(function() {
// Check to see if the user already has the cookie set to scroll
var scrollPosition = getCookie(COOKIE_NAME);
if (scrollPosition.length > 0) {
// Scroll to the position of the last link clicked
window.scrollTo(0, parseInt(scrollPosition, 10));
}
// Attach an overriding click event for each link that has a class named "resultLink" so the
// saveScrollPosition function can be called before the user is redirected.
$("a.resultLink").each(function() {
$(this).click(function() {
saveScrollPosition($(this));
});
});
});
// Get the offset (height from top of page) of the link element
// and save it in a cookie.
function saveScrollPosition(link) {
var linkTop = link.offset().top;
setCookie(COOKIE_NAME, linkTop, 1);
}
// Boring cookie helper function
function getCookie(name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(name + "=");
if (c_start != -1) {
c_start = c_start + name.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end ==- 1) c_end = document.cookie.length;
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
}
// Another boring cookie helper function
function setCookie(name, value, expiredays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie = name + "=" + escape(value) +
((expiredays==null) ? "" : ";expires=" + exdate.toGMTString());
}
這裏假設你的搜索結果鏈接有class="resultLink"
。
所以僞代碼看起來像這樣: 1.用戶單擊結果列表中的鏈接。 2.通過點擊的鏈接調用onClick事件。 3.從事件調用的函數獲取鏈接的ID。 4.該功能將頁面重定向到結果頁面,並將鏈接的ID添加到地址末尾 5.結果頁面重新加載後,該功能會將頁面重定向到頁面,並提供有關結果的更多信息。 這是你的意思嗎?我可以看到它是如何工作的,但是如果找到一個更直接的方法而不必重新加載結果頁面,這將是很好的。 – VinkoCM 2009-08-01 01:42:53
是的,確切地說。是的,這很複雜。 – Anirvan 2009-08-01 17:14:27
@VinkoCM,不需要整頁重新加載。在大多數瀏覽器中,在同一頁面的片段之間移動應該幾乎是瞬間的。 – Anirvan 2010-11-01 20:06:07