Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

javascript - Confirm Leave on External Links in Wordpress

Here's the situation:

  • This is a bank website, so when a user clicks an external link (perhaps to a Facebook page or a partner website), there needs to be a box that says "You are about to leave this page, none of this content is managed by...etc.".

  • The user should then be able to click 'confirm' to open the external link in a new window or click 'cancel' to be taken back to the website and a box saying "you have chosen not to leave the site".

This site is in Wordpress, so I'm looking for an easy way to handle this without having to add custom Javascript to every link.

They actually have it working on their existing site: http://clearmountainbank.com/ and I can make it work on the new site by adding a confirm_entry script to the header and adding custom Javascript to each link but I can't get the links to open in a new window.

This way is also time-consuming as I have to manually edit every external link. Would there be any easy way to recognize external links and add this functionality throughout the site and if not, what can I do to make it open in a new window?

Here's the code I'm using:

    <script type="text/javascript">
        <!--
            function confirm_entry(varible_value)
            {
            input_box=confirm("Link Disclaimer:  Bank Name  provides links to web pages which are not part of the Bank Name website or clearmountainbank.com or online.bankname.com domains. These sites are not under Bank Name control, and Clear Mountain Bank is not responsible for the information or links you may find there. Bank Name is providing these links only as a convenience. The presence of these links on any Bank Name website is not intended to imply Bank Name endorsement of that site, but to provide a convenient link to relevant sites which are managed by other organizations, companies, or individuals.");
            if (input_box==true)

            { 
            window.location.href=varible_value; 
            }

            else
            {
            // Output when Cancel is clicked
            alert ("You have opted not to leave Bank's website.");
            }

            }
        -->
    </script>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's one way you could do it (See JSFiddle):

$("a").on("click", function() {
    if($(this).attr("href").indexOf("http://") == 0) {
        return confirm("Super long message");
    }
});
  • This would only work for http:// and not https://, it also doesn't check whether the absolute link points to your current domain, but it gives you and idea of how to approach it.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...