简体   繁体   English

全局外部链接确认警报的Javascript问题

[英]Javascript problem with a global external link confirmation alert

Below is the code from a plugin for Joomla. 以下是Joomla插件的代码。 It works on it's own and it's purpose is to detect external links on the page and force them into new browser windows using _blank. 它可以独立工作,目的是检测页面上的外部链接,并使用_blank将其强制进入新的浏览器窗口。

I've tried for about an hour (I don't know javascript well) but I can't seem to figure out how to get an onclick function working. 我已经尝试了大约一个小时(我不太了解javascript),但似乎无法弄清楚如何使onclick函数正常工作。

End result, I want to add to this script the ability of a confirmation dialog, shown in the 2nd code section. 最终结果,我想向该脚本添加确认对话框的功能,如第二代码部分所示。

An external link, when clicked, will pop up the confirmation dialog, and if it says yes, they will be able to get to the external URL, opening in a new window. 单击外部链接后,将弹出确认对话框,如果显示“是”,则他们将能够访问外部URL,并在新窗口中打开。 Otherwise, it cancels, and does nothing. 否则,它将取消,并且不执行任何操作。

When I create a link with 当我创建一个链接时

onclick="return ExitNotice(this.href);"
within it it works perfectly, but since my website has multiple people submitting input, I'd like the confirmation box global. 在其中可以完美地运行,但是由于我的网站上有多个人提交输入,因此我希望全局确认框。

/* ----------
  OnClick External Link Notice
---------- */
function ExitNotice(link,site,ltext) {
  if(confirm("-----------------------------------------------------------------------\n\n" + 
    "You're leaving the HelpingTeens.org website. HelpingTeens.org\ndoes not " + 
    "control this site and its privacy policies may differ\nfrom our own. " + 
    "Thank you for using our site.\n\nYou will now access the following link:\n"  + 
    "\n" + link + "\n\nPress \'OK\' to proceed, or  press \'Cancel\' to remain here." + 
    "\n\n-----------------------------------------------------------------------")) 
  {
  return true;
} 
history.go(0);
return false;
}

Second Part 第二部分

 /* ---------- OnClick External Link Notice ---------- */ function ExitNotice(link,site,ltext) { if(confirm("-----------------------------------------------------------------------\\n\\n" + "You're leaving the HelpingTeens.org website. HelpingTeens.org\\ndoes not " + "control this site and its privacy policies may differ\\nfrom our own. " + "Thank you for using our site.\\n\\nYou will now access the following link:\\n" + "\\n" + link + "\\n\\nPress \\'OK\\' to proceed, or press \\'Cancel\\' to remain here." + "\\n\\n-----------------------------------------------------------------------")) { return true; } history.go(0); return false; } 

A) Can anyone help me fix this problem? A)谁能帮我解决这个问题? or B) Is there a better solution? 或B)有更好的解决方案吗?

So, I'll summarize my what I think you're asking and then tell you how I think that can be solved. 因此,我将总结我认为您要问的问题,然后告诉您我认为该如何解决。 It's possible I've misunderstood your question. 我可能误解了您的问题。

You've got code that goes through all tags and sets their target attribute to "_blank" and sets their className to "blank", and you'd like to instead rewrite the links to use JavaScript to show a confirmation dialog and only go to the location if the user clicks Yes. 您已经获得了遍历所有标记的代码,并将其目标属性设置为“ _blank”,并将其className设置为“ blank”,并且您想改写链接以使用JavaScript来显示确认对话框,并且仅转到用户单击“是”时的位置。

In this case I believe you want the links to end up as though they were written like so: 在这种情况下,我相信您希望链接像这样写一样结束:

<a href="javascript:void(0)" onclick="if (ExitNotice('http://www.whatever.com')) window.location.assign('http://www.whatever.com')">

The javascript:void(0) causes the normal browser handling of the click to not go to another page, so that your onclick has time to execute. javascript:void(0)导致正常的浏览器对点击的处理不会转到其他页面,因此onclick有时间执行。

To make this happen, you'll want to change the definition of this.set (inside this.blankwin) to something like this: 为此,您需要将this.set的定义(在this.blankwin内部)更改为以下内容:

  this.set = function(obj){
    var href = obj.href;
    obj.href = "javascript:void(0)";
    obj.onclick = function() { (if ExitNotice(href) window.location.assign(href); };
    obj.target = "_blank";
    obj.className = "blank";
  };

See here for info on void(0). 有关void(0)的信息,请参见此处

Also, note that the code that sets obj.className to "blank" isn't great, because it will remove any other className already assigned to the link. 另外,请注意,将obj.className设置为“空白”的代码不是很好,因为它将删除已分配给链接的任何其他className。 You could instead just add the class name to the existing ones, like so: 您可以将类名称添加到现有名称中,如下所示:

obj.className = obj.className + " blank";

Everything works as I want it now. 一切都按我现在的要求进行。 Thank you for your help. 谢谢您的帮助。 As this is my first time using SO, I hope I credited you correctly. 因为这是我第一次使用SO,所以我希望我能正确记账。 Since I can't figure out how to (or even if it's possible) to show code in comment boxes, I'm choosing to answer my own question with the final code for others to benefit. 由于我不知道如何(甚至可能)在注释框中显示代码,因此我选择用最终代码回答我自己的问题,以使其他人受益。

Once again, you showed me the syntax I needed to modify to get it working the way I wanted. 再次,您向我展示了需要修改的语法以使其按我想要的方式工作。

THANK YOU! 谢谢!

Final code 最终代码

    this.set = function(obj){
            var href = obj.href;
            //obj.href = "javascript:void(0)";
            obj.onclick = function() { return ExitNotice(href)};
            obj.target = "_blank";
            obj.className = obj.className + " blank";
    };

I refined the code a little bit more. 我进一步完善了代码。 Please consider changing the mfblank.js into the following content. 请考虑将mfblank.js更改为以下内容。 So now, all the Script is in one file only AND a additional rel=nofollow is added. 因此,现在,所有脚本仅在一个文件中,并且添加了另一个rel = nofollow。 Have fun with this script... 玩这个脚本...

this.blankwin = function(){
  var hostname = window.location.hostname;
  hostname = hostname.replace("www.","").toLowerCase();
  var a = document.getElementsByTagName("a");   
  this.check = function(obj){
    var href = obj.href.toLowerCase();
    return (href.indexOf("http://")!=-1 && href.indexOf(hostname)==-1) ? true : false;              
  };  
  this.set = function(obj){
            var href = obj.href;
            obj.onclick = function(){ if(confirm("Do you wanne leave this page?")) {
              return true;
            } 
              history.go(0);
              return false;
            };
            obj.target = "_blank";
            obj.className = obj.className + " blank";
            obj.rel = "nofollow";
    };
    for (var i=0;i<a.length;i++){
        if(check(a[i])) set(a[i]);
    };      
  }; 
  this.addEvent = function(obj,type,fn){
        if(obj.attachEvent){
            obj['e'+type+fn] = fn;
            obj[type+fn] = function(){obj['e'+type+fn](window.event );}
            obj.attachEvent('on'+type, obj[type+fn]);
        } else {
            obj.addEventListener(type,fn,false);
        };
    };
    addEvent(window,"load",blankwin);

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

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