Tagged: External Link, links, message
-
AuthorPosts
-
March 26, 2021 at 8:10 pm #1290633
On the main menu of our site, I’m trying to add a message to one of the links saying they’re leaving the site and going to an external link. I guess this would be a leaving notice popup for external links. How would I add this?
March 27, 2021 at 5:13 am #1290677I’m trying to do something like this, but on a specific link on the main menu.
March 28, 2021 at 12:31 pm #1290850Hi John,
You can use the same code posted on that thread.
Then go to Appearance > Menus > select Main Menu.
Click Screen Options on the upper right part portion of the page, then check CSS Classes.
Expand the menu item you’d like this to apply, then put external-link to CSS Classes (optional) and save.
Hope this helps.Best regards,
Nikko- This reply was modified 3 years, 8 months ago by Nikko.
March 28, 2021 at 5:13 pm #1290950Where do I type the message, in the PHP?
Would I use this or the other PHP mentioned?
function add_custom_script(){ ?> <script> jQuery(window).load(function(){ jQuery('a.external-link, .external-link a').click(function() { alert("You're going to a better place."); }); }); </script> <?php } add_action('wp_head', 'add_custom_script');
March 29, 2021 at 2:12 am #1290999Hi,
No, actually just change this part of the code:
jQuery('a.external-link, .external-link a').click(function() {
to this:
jQuery('.external-link a').click(function() {
The whole code would be:
function add_custom_script(){ ?> <script> jQuery(window).load(function(){ jQuery('.external-link a').click(function() { alert("You're going to a better place."); }); }); </script> <?php } add_action('wp_head', 'add_custom_script');
Just add this at the bottom of your child theme’s functions.php file.
Best regards,
NikkoMarch 29, 2021 at 3:17 am #1291002And also adding external-link/strong> to CSS Classes (optional)?
March 29, 2021 at 5:15 am #1291013Hi John,
I made a typo, it should only be: external-link
Best regards,
NikkoMarch 29, 2021 at 5:11 pm #1291123I was able to add this message, but how do I add a ‘Cancel’ button to this if someone accidentally clicks it?
March 30, 2021 at 8:00 am #1291237Hi John,
Can you try to replace:
alert("You're going to a better place.");
with:
confirm("You're going to a better place.");
Best regards,
NikkoApril 28, 2021 at 10:50 pm #1297231Sorry for replying so late. That kind of worked. It has the ‘cancel’ and ‘ok’ buttons on the message, but if you click cancel, it still takes you to another page, you don’t stay on the site.
April 29, 2021 at 5:32 am #1297252Hi John,
No worries.
The cancel button in confirm should just return null and won’t do anything.
Here’s some link that might be helpful: https://javascript.info/alert-prompt-confirm
Can you give us steps on how we can replicate this on our end?Best regards,
NikkoMay 6, 2021 at 11:11 pm #1298861Click ‘Shop’ in the main navigation and once that notice comes up, click ‘Cancel’. It just continues to the other page anyway. It doesn’t cancel going to that link.
May 7, 2021 at 9:49 am #1298887first : one thing to mention – on new jQuery 3.5.1 – some of the functions are deprecated.
You had to use.on('load', function() and .on('click', function ()
and use instead return confirm ()so code will be:
i tested it without$(window).on( 'load' , (function(){
if it does not work – try then with it ( do not forget the closing brackets )maybe this will be enough for you to show:
function add_custom_script(){ ?> <script> (function($){ $('.external-link a').on('click', function () { return confirm('Are you sure?'); }); })(jQuery); </script> <?php } add_action('wp_footer', 'add_custom_script');
- This reply was modified 3 years, 7 months ago by Guenni007.
May 7, 2021 at 6:35 pm #1299017So instead of
function add_custom_script(){ ?> <script> jQuery(window).load(function(){ jQuery('.external-link a').click(function() { confirm("You're going to a better place."); }); }); </script> <?php } add_action('wp_head', 'add_custom_script');
it would be
function add_custom_script(){ ?> <script> (function($){ $('.external-link a').on('click', function () { return confirm('Are you sure?'); }); })(jQuery); </script> <?php } add_action('wp_footer', 'add_custom_script');
Is that right or am I adding that in addition?
May 7, 2021 at 8:01 pm #1299027yes – and it costs only a few minutes to test it yourself – no hidden self-destruct button inside the code ;)
May 7, 2021 at 8:07 pm #1299029This worked, thank you!
May 10, 2021 at 6:43 am #1299279Hi John,
Great, I’m glad that you got it working. Please let us know if you should need any further help on the topic or if we can close it.
Best regards,
RikardMay 10, 2021 at 3:46 pm #1299395You can close it. Thanks!
May 10, 2021 at 4:16 pm #1299407Hi John,
We’re glad to hear that :)
Thanks for using Enfold and have a great day!Best regards,
Nikko -
AuthorPosts
- The topic ‘Adding Leaving Notice Message to external link’ is closed to new replies.