views:

516

answers:

1

For any links with a particular CSS class, I'd like to control whether the links open in the same window, a new window or a pop-up (using onclick), based on the user's selection from a group of radio buttons -- and then save that choice in a cookie (all using jQuery). Anyone know how to accomplish this?

+3  A: 

This is probably how I'd do it... (you will need the jQuery cookie plugin):

<script language="javascript">
$(function() {
    if($.cookie('link_pref')) {
    var link_pref = $.cookie('link_pref');
        $('#link_options_form :radio[value="'+ link_pref+'"]')
        .attr('checked','checked');
    }
    $.cookie('link_pref',$('#link_options_form :radio:checked').val(), {expires: 0});
    $('#link_options_form :radio').unbind('click').bind('click',function() {
         $.cookie('link_pref', $(this).val(), {expires: 0});
    });
    $('a').unbind('click').bind('click',function() {
        var link_pref = $.cookie('link_pref');
        var href = $(this).attr('href');
        var link_txt = $(this).text();
        switch(link_pref) {
            case 'new':
                $(this).attr('target','_blank');
                return true;
            case 'pop':
                window.open(href,link_txt,'width=640,height=480,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes');
                return false;
            case 'greybox':
                // Other options found here: 
                // http://orangoo.com/labs/greybox/advance_usage.html
                GB_show(link_txt, href);
                return false;
            default:
                $(this).attr('target','_self');
                return true;
        }
    });
});
</script>

<form id="link_options_form">
    <label><input type="radio" name="link_options" value="same" /> Open in Same Window</label>
    <label><input type="radio" name="link_options" value="new" /> Open in New Window</label>
    <label><input type="radio" name="link_options" value="pop" /> Open in Pop-Up Window</label>
    <label><input type="radio" name="link_options" value="greybox" /> Open in Greybox</label>
</form>

Edit: Sorry that I didn't test it first. I had a few typos in there and I forgot to set the cookie to begin with (sorry). I've tested it and it now works with your HTML. Use the newly edited code above. ;-)

Edit 2: I added a direct link to the cookie plugin just in case you, for some reason, aren't using the right one.

Edit 3: Personally, I wouldn't set the radio button as checked in javascript... you can access the same cookie in your server-side language I believe. But, I've provided a way that should work in my newly edited code.

Edit 4: The initial setting of the checked radio buttons bug has been fixed. It should really really work this time. For real. o_0

KyleFarris
Thanks for your reply, Kyle, but I couldn't get that to work. Here is the full page code I used: http://textsnip.com/734b04 - thanks for your help.
Thanks for your help! I got it to work, but it trips a lot of errors within the browser. "Too much recursion," among others. (Not sure what that means) Also, once I've selected the "new window" option and opened a link, and then select "same window" again, it's still using a new window for the links. Any ideas? Thanks again.
I'm not sure about the recursion problem as I'm not looping over anything. Are you getting the error in Firefox? How many links do you have on this page?? That could be the problem. Anyways, the 'new window'/'same window' bug is fixed. I updated the code above.
KyleFarris
Thanks again for your help, Kyle. I think the errors I was seeing had to do with FireBug and not the script. One other question -- how can I select the actual radio button from the cookie when the page loads? Right now, if I select "new window" and then close the browser and re-open it, the 'new window' functionality works, but that radio button isn't selected. Also, is it possible to change the 'rel' attribute for the 'pop' option, so that I can trigger a Greybox window? Here is the code I'm working with: http://textsnip.com/a15e91 - Thanks again, I really appreciate it.