【问题标题】:Invoke an alert on an href, then continue to a new page在 href 上调用警报,然后继续到新页面
【发布时间】:2009-09-03 02:49:26
【问题描述】:

我有一个页面链接,该页面调用“性感警报框”脚本来弹出一条消息,询问用户是否同意条款和条件,并带有“我同意”和“取消”选项。

我已经完成了这么多,但现在我需要“我同意”选项才能将他们发送到新页面。 (“取消”返回用户当前页面。)

document.location.href有什么关系?

我正在使用 mootools 框架。

Sexy Alert Box 1.2 mootools & jQuery

代码如下:

<a href="#" 
    onclick="Sexy.confirm('Do you agree to the terms and conditions?', {
        textBoxBtnOk: 'I Agree', // goes to www.newpage.com
        textBoxBtnCancel: 'Cancel' // return to current page    
    });">Link</a>

【问题讨论】:

  • 那真的应该是&lt;a href="newpage.com" onclick="..."&gt;,这样非js用户就不会得到无效的链接。

标签: javascript mootools alerts


【解决方案1】:

标准的内置 javascript confirm 函数将返回 true 或 false,具体取决于按下的按钮(这是一个 同步 操作),但这个 Sexy.confirm 插件看起来像将通过回调函数返回其选择(这将是一个异步操作)。

例如,

function doConfirm() {
    Sexy.confirm('Do you agree to the terms and conditions?', {
        textBoxBtnOk: 'I Agree',
        textBoxBtnCancel: 'Cancel',
        onComplete: function(returnvalue) {
            // here is where you act, after user has made a choice...
            if (returnvalue) {
                //alert ("pressed OK");
                window.location.href = '/page1/';
            }
            else {
                //alert("pressed Cancel");
                window.location.href = '/page2/';
            }
        }
    });
    return false; // this is to cancel the native click event
}

然后你可以让你的链接标签更好一点,

<a href="#" onclick="return doConfirm();">Link</a>

祝你好运!

【讨论】:

  • P.S.,您还应该强烈考虑将 href="#" 更改为指向真正的 href,以便没有 javascript 的用户代理仍然可以访问该站点。
【解决方案2】:

使用 Mootools:

用于 HTML

<a class="confirm" href="www.google.fr">Google Fr</a>
<a class="confirm" href="www.google.be">Google Be</a>

和 JavaScript

window.addEvent('domready', function () {
    $$('a.confirm').each(function (el) {
        el.addEvent('click', function (e) {
            e.stop();
            Sexy.confirm('<h1>Etes-vous sur de vouloir suivre ce lien ?</h1>',{ 
                onComplete: function(returnvalue) {
                    if (returnvalue) {
                        window.location.href = el.get('href');
                    }
                },
                textBoxBtnOk: 'Oui',
                textBoxBtnCancel: 'Non'
            });
        });
    });
});

【讨论】:

    猜你喜欢
    • 2011-08-20
    • 1970-01-01
    • 2019-03-31
    • 2012-07-24
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多