0

我对 jQuery 不是很好,最近碰壁了。我有一个 jQuery UI 对话框,当用户在 index.html 上触发它时会打开它。它隐藏在 index.html 上,直到用户触发它打开。在另一个单独的 html 页面上,我有一个将您链接回 index.html 的按钮,但我需要做的是,当用户单击此链接时,它会将他们带回 index.html 并触发对话框模态窗口自动打开。

我让它在某种程度上工作,但我想找到一个替代解决方案,我不使用 URL 使其工作。

“返回”链接的 HTML

<a href="index.html#dialog">Go back</a>

我的 jQuery:

if (location.href.indexOf("#dialog") != -1) {

$('.dialog-popup').dialog("open"); 

}

UI的jQuery弹出:

$( ".dialog-popup" ).dialog({
  autoOpen: false,
  modal: true,
  width: 860,
  show: {
    effect: "fade",
    duration: 500
  },
  hide: {
    effect: "fade",
    duration: 500
  }
});

在这里的任何帮助将不胜感激。我希望它无需在 URL 中添加 #dialog 即可工作。

4

1 回答 1

0

You could use document.referrer to determine where the user came from. If you parse document.referrer for information regarding the page the user came from and match it against something, you could trigger the dialog that way.

For instance, if you want to trigger the dialog whenever a user arrives at index.html from a page that's on your domain, but not from index.html itself, you could run this code:

jQuery(function($){

    var referrerDomain = document.referrer.replace(/(http|https):\/\//,'').split("/")[0];
    var myDomain = "mydomain.net"; //Your domain name goes here
    if( referrerDomain == myDomain && !document.referrer.match("index.html") )
        alert('You arrived at this page from your domain, but not from index.html!')

});

as seen in this fiddle (full screen version here)

Note - the fiddles will show the referrer as the same page you're currently on. This won't happen on a normal page; it's just happening there because it's using iFrames.

于 2013-05-21T00:19:02.857 回答