2010-05-05 49 views
0

當頁面的URL中傳遞了該模式的ID時,我需要能夠在頁面上打開ID'd jQuery UI模式。例如:當通過URL打開jQuery Modal時,停止滾動到ID

http://mysite.com/visit.html#directions 

#directions是位於頁面最底部的ID'd DIV,位於關閉BODY元素之前。它在頁面加載時被實例化爲一個jQuery UI對話框,然後我運行一個函數來檢查當前的模式ID的URL。如果URL包含模式的ID,則打開模式。

這裏是我的自動打開的功能是什麼樣子:

function autoOpenModal() { 
    if (document.location.href.indexOf('#')) { 
     var uriParts = document.location.href.split('#'); 
     var hashID = '#' + uriParts[1]; 
     if ($(hashID) && $(hashID).hasClass('dialog')) { 
      $(hashID).dialog('open'); 
     } 
    } 
} 

我遇到的問題是,瀏覽器在頁面最底部的模式之前,自動向下滾動到ID被打開。所以當用戶關閉模式時,他們在頁面的底部(這對我來說似乎「破碎」)。

誰能告訴我如何停止瀏覽器自動滾動到ID?我看過event.preventDefault()但沒有成功;我不確定在事件鏈中使用它的位置或方式。

回答

0

嘗試返回從autoOpenModal()函數中錯誤:

function autoOpenModal() { 
    if (document.location.href.indexOf('#')) { 
     var uriParts = document.location.href.split('#'); 
     var hashID = '#' + uriParts[1]; 
     if ($(hashID) && $(hashID).hasClass('dialog')) { 
      $(hashID).dialog('open'); 
     } 
    } 
    return false; 
} 
+0

謝謝你的建議,karim。但我已經嘗試過了,它似乎沒有工作。我認爲這是因爲在執行document.ready()事件之前,瀏覽器在標記中向下跳轉。 – markedup 2010-05-07 10:03:56

2

如果這是你的URL「http://mysite.com/visit.html#directions」網絡瀏覽器應該總是跳轉到「方向」因爲這就是它所要求的。而不是使用「#」,也許你可以使用查詢字符串。

http://en.wikipedia.org/wiki/Query_string