繁体   English   中英

如何在离开网站之前在“WordPress菜单”上显示一个javascript确认框

[英]How to show a javascript confirmation box on “WordPress menu” before leaving the website

当用户点击WordPress菜单上的“论坛”链接时,我想显示一个javascript消息框。

消息框应显示“您要离开此网站...” 之类的消息一旦用户确认消息框,它应该在新选项卡中打开外部链接。

我试过这个 ,但是当我离开网站或重新加载页面时,它会触发消息框。 我只想在单个WordPress菜单项上显示此消息框。

我看到了一些教程来做到这一点,但无法在WordPress菜单上进行。

WordPress无法识别javascript:void(0)或URL部分上的任何其他javascript函数。

有任何想法吗??

你有jQuery加载? 论坛链接在哪里? 代码在哪里?

<a href="http://www.yourforum.com" onclick="return confirm('Are you sure you want to leave?');" />

将链接位置设置为以下内容:

javascript: if (confirm('Are you sure you want to leave?')) {window.open('http://www.yourforum.com','_blank');}

应该生成html像:

<a href="javascript: if (confirm('Are you sure you want to leave?')) {window.open('http://www.yourforum.com','_blank');}">Link</a>

我在JS小提琴中进行了测试,似乎工作正常,因此您应该能够将其作为数据库中的链接位置。

编辑:

// for a new tab
window.open(url,'_blank');
// for a redirect in same window
window.location=url;

如果你可以在任何地方添加任何javascript到你的页面,你可以执行以下操作(需要jQuery,但你说你已经加载了):

$("a[href='http://example.com/forum']").click(function() {
    alert("Good bye!");
    return true;
});

最后,我得到了它的工作。 我正在逐步解释它,所以有类似问题的人可以解决这个问题:

  1. #放入您想要JavaScript函数的wordpress菜单URL中。
  2. 从源代码中跟踪此菜单URL(li ID)链接。 在我的情况下,li id是menu-item-88 (此id由wordpress自动生成,始终是唯一的)
  3. 将以下代码放在当前主题的head标记结尾(open header.php find </head> )之前。
  4. 不要忘记将menu-id menu-item-88更改为你的。

<script type="text/javascript">

jQuery.noConflict();
jQuery(document).ready(function(){
var menuID = jQuery('#menu-item-82');

findA = menuID.find('a');

findA.click(function(event){
if(confirm("YOU ARE LEAVING THE WEBSITE" + '\n' + "" + '\n' + "You are about to leaving the website to go to the external forum" + '\n' + "" + '\n' + "The FORUM will open in a new tab")) 
{
window.open('http://www.yourforum.com/','_blank'); //This will open the website in a new tab
}

});
});
</script>

这需要jQuery。 因此,如果未加载jQuery,请将以下行添加到您的头部。

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

这将提示您,您将离开该网站。 如果单击“确定”,它将在新选项卡中打开给定链接。

好。 多数民众赞成......这将是神奇的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM