简体   繁体   English

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

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

I wants to show a javascript message box when a user clicks on a "FORUM" link on WordPress menu. 当用户点击WordPress菜单上的“论坛”链接时,我想显示一个javascript消息框。

The message box should show a message like "You are leaving this website..." and once the user confirms the message box, it should open an external link in a new tab. 消息框应显示“您要离开此网站...” 之类的消息一旦用户确认消息框,它应该在新选项卡中打开外部链接。

I had tried this , but it will fire the message box, when ever I leave the website or reload the page. 我试过这个 ,但是当我离开网站或重新加载页面时,它会触发消息框。 I just wants this message box on a single WordPress menu item. 我只想在单个WordPress菜单项上显示此消息框。

I saw some tutorials to do this, but not able to do it on WordPress menu. 我看到了一些教程来做到这一点,但无法在WordPress菜单上进行。

WordPress does not recognize javascript:void(0) or any other javascript function on URL part. WordPress无法识别javascript:void(0)或URL部分上的任何其他javascript函数。

Any ideas?? 有任何想法吗??

Do you have jQuery loaded? 你有jQuery加载? Where is the forum link? 论坛链接在哪里? Where's the code? 代码在哪里?

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

Set the link location to something like this: 将链接位置设置为以下内容:

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

which should generate html like: 应该生成html像:

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

I tested this in JS fiddle, seems to work, so you should be able to put this as the link location in your database. 我在JS小提琴中进行了测试,似乎工作正常,因此您应该能够将其作为数据库中的链接位置。

Edit: 编辑:

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

If you can add any javascript to your page, anywhere, you can do the following (Needs jQuery, but you said you have that loaded): 如果你可以在任何地方添加任何javascript到你的页面,你可以执行以下操作(需要jQuery,但你说你已经加载了):

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

Finally I got it working. 最后,我得到了它的工作。 I'm explaining it step by step, so someone with similar issue can solve this: 我正在逐步解释它,所以有类似问题的人可以解决这个问题:

  1. Put # in your wordpress menu url in which you want the JavaScript Function. #放入您想要JavaScript函数的wordpress菜单URL中。
  2. Track this menu url (li ID) link from source code. 从源代码中跟踪此菜单URL(li ID)链接。 in my case li id is menu-item-88 . 在我的情况下,li id是menu-item-88 (this id is auto generated by wordpress and always unique) (此id由wordpress自动生成,始终是唯一的)
  3. Place the following code before the end of head tag (open header.php find </head> ) of current theme. 将以下代码放在当前主题的head标记结尾(open header.php find </head> )之前。
  4. Don't forget to change the menu-id menu-item-88 to yours. 不要忘记将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>

This requires jQuery. 这需要jQuery。 So if jQuery is not loaded, add the following line also to your head. 因此,如果未加载jQuery,请将以下行添加到您的头部。

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

This will prompt you that, you are leaving the website. 这将提示您,您将离开该网站。 If you clicks OK, it will open the given link in a new tab. 如果单击“确定”,它将在新选项卡中打开给定链接。

Ok. 好。 Thats all.. This will do the magic. 多数民众赞成......这将是神奇的。

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

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