Your coding skills are fine, that's not the problem here. :-)

The problem is that you can't do what you want in exactly the form you were 
aiming for:

    jQuery('#testhref').RemoteLink(options);

When you call jQuery('#testhref'), the testhref element needs to exist. You 
can't hide the document ready inside this code, because
the jQuery function will try to find that element when you call it.

Instead, make your code a static function:

    (function( $ ) {
        $.remoteLink = function( selector, options ) {
            $(function() {
                $('#testhref').click( function() {
                    $.ajax( $.extend({
                        url: this.href,
                        dataType: "script",
                        beforeSend: function( xhr ) {
                            xhr.setRequestHeader(
                                "Accept", "text/ javascript" );
                        }
                    }, options ) );
                    return false;
                });
            });
        };
    })( jQuery );

and call it with:

    $.remoteLink( '#testhref', options );

If you prefer, you can make this an ordinary function instead of a static $ 
method. Change this line:

        $.remoteLink = function( selector, options ) {

to:

        remoteLink = function( selector, options ) {

and call it with:

    remoteLink( '#testhref', options );

-Mike

> From: Joe
> 
> I'd love some js/jQuery expert help in code minimization / 
> abstraction.
> 
> html:
> 
> <a href="/account/locations/new?_context=proposal" 
> id="testhref">Test Link</a>
> 
> js:
> 
> jQuery(function() {
>   jQuery('#testhref').click(function() {
>     jQuery.ajax({
>       url: this.href,
>       dataType: "script",
>       beforeSend: function(xhr) 
> {xhr.setRequestHeader("Accept", "text/ javascript");}
>     });
>     return false;
>   });
> });
> 
> I'm trying to minimize the code down to this, since that 
> behavior is something that will be desired in many places.  
> So the js id like to see is something like this:
> 
> jQuery('#testhref').RemoteLink(options); //where options 
> could be anything additional to pass into the jQuery.ajax call
> 
> I would plan on handling the document ready state inside 
> there in order to minimize the code needed in setting that 
> up.  Is any of this possible?  I've tried hard to make it 
> work but my js skills are not allowing me to do so.

Reply via email to