• Resolved seandyer

    (@seandyer)


    I’m new to this plugin, and I am trying to accomplish a specific task with it as fast as possible. The task is to have a modal window pop up anytime a user clicks any external link to leave the site. There are a few modals already built by other developers on the site that have content in them like “>>Leave Site? >>Return”, and apparently they used to work but now are not. Can you give my any insight as to how content like the example I just gave works, if there are other similar calls like this, and just in general the best way to approach the task?

    Thanks.

    http://wordpress.org/plugins/easy-modal/

Viewing 14 replies - 1 through 14 (of 14 total)
  • Thread Starter seandyer

    (@seandyer)

    I figured out my question about the previous modals content, but I still have not been able to figure out how to complete the original task.

    My question at this point is how would I go about populating the href to a link within a modal window from the link that called the modal window, and how could I allow the user to stay on the current page instead of going to the link? In other words, the user clicks on a link, a modal window pops up saying, “You are leaving this site” and two options are offered, Leave (go to link that was clicked) or Return (stay on current page, or reload current page).

    Plugin Author Daniel Iser

    (@danieliser)

    Thread Starter seandyer

    (@seandyer)

    I had seen that, but I can’t find a js file in the theme, so I wasn’t sure where that should go. I was also wondering if there was a way to have this modal window automatically pop up for all external links without having to by hand add it to each. There are multiple users who are able to post, and if they have an external link within the post, we need this window to still pop up.

    Plugin Author Daniel Iser

    (@danieliser)

    This is something we will be adding in the future but currently it isn’t there.

    The solution in that article can do just that. You can use JS to detect all links that have an external domain and add modal functionality that way.

    If your theme doesnt have one you can create one and add it via wp_enqueue_script function in your themes functions.php file.

    In that JS file add something like this should do the trick.

    // Creating custom :external selector
    jQuery.expr[':'].external = function(obj){
        return !obj.href.match(/^mailto\:/)
                && (obj.hostname != location.hostname);
    };
    
    jQuery(document).ready(function(){
    	// Add 'external' CSS class to all external links
    	jQuery('a:external')
    		.addClass('external')
    		.click(function(e){
    			e.preventDefault();
    			jQuery('#eModal-1 iframe').attr('src', jQuery(this).attr('href'));
    			jQuery('#eModal-1').emodal('open');
    		});
    })

    Thread Starter seandyer

    (@seandyer)

    I’m sorry for being thick-headed, I’m just still very new to webdev. I assume that instead of using iframe, I would put ‘a’ to reference the html tag in the modal?

    For the Return option, where should the code go for closing the modal window?

    Plugin Author Daniel Iser

    (@danieliser)

    With the above script you would need to create a modal with contents of

    <iframe src=""></iframe>

    The iframe is inside the modal and how you show another site inside your own.

    Once you get the modals ID # and replace the scripts # “1” with your modal ID any link on your site pointing to another domain would open that modal and change the iframe to show the link clicked.

    The modals existing functionality and options still work so no need to add close functionality as it will already be there.

    The script above simply over rides the opening function and during opening changes the source of the iframe.

    Thread Starter seandyer

    (@seandyer)

    I don’t want the external link to open in the modal window though, I still want the link to take the user to the actual site.

    Plugin Author Daniel Iser

    (@danieliser)

    Then im not sure what your after. If your talking exit prevention you can do this with a custom modal and instead of changing iframe source change the LEAVE links href attribute.

    Should be simple enough. The Pro has soft exit modals which load when a user attempts to go to the toolbar to change urls or click the x to close the window.

    Thread Starter seandyer

    (@seandyer)

    Thank you so much for all your help and your quick response time. Your script works great, and I was able to easily conform it to my needs, which were pretty much what you described in your last post.

    I think I will be removing the return option and just use the X to close the modal window. The only issue I’m having now is that when I click the X to close the modal window, it closes and then takes me to the external link anyways. Do have any idea about how to fix that?

    Plugin Author Daniel Iser

    (@danieliser)

    Sounds like we arnt just changing the leave links href but also the close buttons.

    On a side note you can make the return button close the modal by either adding the close-modal class. Or by using the JS line jQuery('.modal.active').emodal('close');

    Your best bet to fix your issue though is to either add a class to the leave link in the modals content of something like leave-link and then change the script above to read

    jQuery('#eModal-1 .leave-link').attr('href', jQuery(this).attr('href'));

    Glad you got it working. Please take a moment to rate and review the plugin and or support http://wordpress.org/support/view/plugin-reviews/easy-modal

    Thread Starter seandyer

    (@seandyer)

    I actually got it working by going into your js files for the plugin, and adding
    .attr(“href”, window.location.pathname)
    to the return statement of the
    $.fn.themeClose = function(opts).

    Thanks again for all your assistance and speedy responses. You built a great plugin.

    Plugin Author Daniel Iser

    (@danieliser)

    Be careful modifying plugin files as these changes will be lost if you update in the future.

    The problem is caused because you are most likely telling the JS to replace all a tags href values to the links. This would include the a tag used for the close button, so when clicked it will follow through to the link as well.

    Simply add a class and target that class to fix it permanantly.

    Thread Starter seandyer

    (@seandyer)

    I’ve come across a new issue, and I was wondering if you have seen it before or have any ideas on how to handle it. One of the widgets in the theme we are using uses an iframe. Within the iframe there are external links, but since the iframe is technically loading in a completely different html document, the conditions setup on the rest of the site to flag all external links (so the modal window pops up) don’t apply to the document within the iframe. I have been reading about ways to force certain code into the iframe’s document using jquery, but so far nothing has worked.

    Plugin Author Daniel Iser

    (@danieliser)

    There is no real way to do what your after there. You can modify the iframes contents from outside itself. Also if you did get a modal to open it would open inside that iframe not in the browsers main window.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Modal Alert for leaving site’ is closed to new replies.