Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

jQuery overload

Name: Anonymous 2010-05-20 15:19

It seems like jQuery is the industry standard for integrating ajax into websites.  I like to be able to look at code and understand how it does what it does though.  What is in jQuery? Isn't it basically javascript? How can I learn how to write my own javascript code to do what jQuery does in the instances that I want to use it for such as uploading images without refreshing the page? When I search for ajax tutorials they all just tell me to use jQuery.

Name: Anonymous 2010-05-20 15:21

Why wouldn't you use what's already done for you?

In any case, the jQuery source code is fairly simple to follow once you orient yourself. http://docs.jquery.com/Source_Code

Name: Anonymous 2010-05-20 15:25

>>1
1. Have /prog/ laugh at you
2. Get jQuery library (http://jquery.com/)
3. Read source code
4. Plagiarize
5. ...
6. PROFIT
7. Have /prog/ laugh at you

Name: Anonymous 2010-05-20 15:26

I don't want to use what someone else has already done because I want to know how to do everything myself.  jQuery is where the magic happens and I want to learn how to be a magician.

Name: >>2 2010-05-20 15:27

Also to answer your questions, yes, it is basically Javascript. The special thing about jQuery is that you don't have to worry about browser compatibility etc.

They are right to tell you to use jQuery, to keep you from the horrors of writing your own XmlHttpRequest nonsense (since each browser implements it differently)!

This is why JS development is exploding recently: we've never had a framework like jQuery/dojo/mootools/yahoo/protoype to standardize things--and make it necessary to write much, much less code to do simple things across all common browsers.

Name: Anonymous 2010-05-20 15:31

I want to know how to do everything myself

Besides Javascript being the wrong place to start, this attitude will get you absolutely nothing

Name: Anonymous 2010-05-20 15:33

It seems like only a small part of the jQuery file applies to what I am actually trying to do with a particular part of the website functionality.  So why do I need to reference the whole file if I could just use part of the code?  What part of the ajax performance does jQuery specifically facilitate?

Name: Anonymous 2010-05-20 15:34





ajax: function( origSettings ) {
        var s = jQuery.extend(true, {}, jQuery.ajaxSettings, origSettings);
       
        var jsonp, status, data,
            callbackContext = origSettings && origSettings.context || s,
            type = s.type.toUpperCase();

        // convert data if not already a string
        if ( s.data && s.processData && typeof s.data !== "string" ) {
            s.data = jQuery.param( s.data, s.traditional );
        }

        // Handle JSONP Parameter Callbacks
        if ( s.dataType === "jsonp" ) {
            if ( type === "GET" ) {
                if ( !jsre.test( s.url ) ) {
                    s.url += (rquery.test( s.url ) ? "&" : "?") + (s.jsonp || "callback") + "=?";
                }
            } else if ( !s.data || !jsre.test(s.data) ) {
                s.data = (s.data ? s.data + "&" : "") + (s.jsonp || "callback") + "=?";
            }
            s.dataType = "json";
        }

        // Build temporary JSONP function
        if ( s.dataType === "json" && (s.data && jsre.test(s.data) || jsre.test(s.url)) ) {
            jsonp = s.jsonpCallback || ("jsonp" + jsc++);

            // Replace the =? sequence both in the query string and the data
            if ( s.data ) {
                s.data = (s.data + "").replace(jsre, "=" + jsonp + "$1");
            }

            s.url = s.url.replace(jsre, "=" + jsonp + "$1");

            // We need to make sure
            // that a JSONP style response is executed properly
            s.dataType = "script";

            // Handle JSONP-style loading
            window[ jsonp ] = window[ jsonp ] || function( tmp ) {
                data = tmp;
                success();
                complete();
                // Garbage collect
                window[ jsonp ] = undefined;

                try {
                    delete window[ jsonp ];
                } catch(e) {}

                if ( head ) {
                    head.removeChild( script );
                }
            };
        }

        if ( s.dataType === "script" && s.cache === null ) {
            s.cache = false;
        }

        if ( s.cache === false && type === "GET" ) {
            var ts = now();

            // try replacing _= if it is there
            var ret = s.url.replace(rts, "$1_=" + ts + "$2");

            // if nothing was replaced, add timestamp to the end
            s.url = ret + ((ret === s.url) ? (rquery.test(s.url) ? "&" : "?") + "_=" + ts : "");
        }

        // If data is available, append data to url for get requests
        if ( s.data && type === "GET" ) {
            s.url += (rquery.test(s.url) ? "&" : "?") + s.data;
        }

        // Watch for a new set of requests
        if ( s.global && ! jQuery.active++ ) {
            jQuery.event.trigger( "ajaxStart" );
        }

        // Matches an absolute URL, and saves the domain
        var parts = rurl.exec( s.url ),
            remote = parts && (parts[1] && parts[1] !== location.protocol || parts[2] !== location.host);

        // If we're requesting a remote document
        // and trying to load JSON or Script with a GET
        if ( s.dataType === "script" && type === "GET" && remote ) {
            var head = document.getElementsByTagName("head")[0] || document.documentElement;
            var script = document.createElement("script");
            script.src = s.url;
            if ( s.scriptCharset ) {
                script.charset = s.scriptCharset;
            }

            // Handle Script loading
            if ( !jsonp ) {
                var done = false;

                // Attach handlers for all browsers
                script.onload = script.onreadystatechange = function() {
                    if ( !done && (!this.readyState ||
                            this.readyState === "loaded" || this.readyState === "complete") ) {
                        done = true;
                        success();
                        complete();

                        // Handle memory leak in IE
                        script.onload = script.onreadystatechange = null;
                        if ( head && script.parentNode ) {
                            head.removeChild( script );
                        }
                    }
                };
            }

            // Use insertBefore instead of appendChild  to circumvent an IE6 bug.
            // This arises when a base node is used (#2709 and #4378).
            head.insertBefore( script, head.firstChild );

            // We handle everything using the script element injection
            return undefined;
        }

        var requestDone = false;

        // Create the request object
        var xhr = s.xhr();

        if ( !xhr ) {
            return;
        }

        // Open the socket
        // Passing null username, generates a login popup on Opera (#2865)
        if ( s.username ) {
            xhr.open(type, s.url, s.async, s.username, s.password);
        } else {
            xhr.open(type, s.url, s.async);
        }

        // Need an extra try/catch for cross domain requests in Firefox 3
        try {
            // Set the correct header, if data is being sent
            if ( s.data || origSettings && origSettings.contentType ) {
                xhr.setRequestHeader("Content-Type", s.contentType);
            }

            // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
            if ( s.ifModified ) {
                if ( jQuery.lastModified[s.url] ) {
                    xhr.setRequestHeader("If-Modified-Since", jQuery.lastModified[s.url]);
                }

                if ( jQuery.etag[s.url] ) {
                    xhr.setRequestHeader("If-None-Match", jQuery.etag[s.url]);
                }
            }

            // Set header so the called script knows that it's an XMLHttpRequest
            // Only send the header if it's not a remote XHR
            if ( !remote ) {
                xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
            }

            // Set the Accepts header for the server, depending on the dataType
            xhr.setRequestHeader("Accept", s.dataType && s.accepts[ s.dataType ] ?
                s.accepts[ s.dataType ] + ", */*" :
                s.accepts._default );
        } catch(e) {}

        // Allow custom headers/mimetypes and early abort
        if ( s.beforeSend && s.beforeSend.call(callbackContext, xhr, s) === false ) {
            // Handle the global AJAX counter
            if ( s.global && ! --jQuery.active ) {
                jQuery.event.trigger( "ajaxStop" );
            }

            // close opended socket
            xhr.abort();
            return false;
        }

        if ( s.global ) {
            trigger("ajaxSend", [xhr, s]);
        }

        // Wait for a response to come back
        var onreadystatechange = xhr.onreadystatechange = function( isTimeout ) {
            // The request was aborted
            if ( !xhr || xhr.readyState === 0 || isTimeout === "abort" ) {
                // Opera doesn't call onreadystatechange before this point
                // so we simulate the call
                if ( !requestDone ) {
                    complete();
                }

                requestDone = true;
                if ( xhr ) {
                    xhr.onreadystatechange = jQuery.noop;
                }

            // The transfer is complete and the data is available, or the request timed out
            } else if ( !requestDone && xhr && (xhr.readyState === 4 || isTimeout === "timeout") ) {
                requestDone = true;
                xhr.onreadystatechange = jQuery.noop;

                status = isTimeout === "timeout" ?
                    "timeout" :
                    !jQuery.httpSuccess( xhr ) ?
                        "error" :
                        s.ifModified && jQuery.httpNotModified( xhr, s.url ) ?
                            "notmodified" :
                            "success";

                var errMsg;

                if ( status === "success" ) {
                    // Watch for, and catch, XML document parse errors
                    try {
                        // process the data (runs the xml through httpData regardless of callback)
                        data = jQuery.httpData( xhr, s.dataType, s );
                    } catch(err) {
                        status = "parsererror";
                        errMsg = err;
                    }
                }

                // Make sure that the request was successful or notmodified
                if ( status === "success" || status === "notmodified" ) {
                    // JSONP handles its own success callback
                    if ( !jsonp ) {
                        success();
                    }
                } else {
                    jQuery.handleError(s, xhr, status, errMsg);
                }

                // Fire the complete handlers
                complete();

                if ( isTimeout === "timeout" ) {
                    xhr.abort();
                }

                // Stop memory leaks
                if ( s.async ) {
                    xhr = null;
                }
            }
        };

        // Override the abort handler, if we can (IE doesn't allow it, but that's OK)
        // Opera doesn't fire onreadystatechange at all on abort
        try {
            var oldAbort = xhr.abort;
            xhr.abort = function() {
                if ( xhr ) {
                    oldAbort.call( xhr );
                }

                onreadystatechange( "abort" );
            };
        } catch(e) { }

        // Timeout checker
        if ( s.async && s.timeout > 0 ) {
            setTimeout(function() {
                // Check to see if the request is still happening
                if ( xhr && !requestDone ) {
                    onreadystatechange( "timeout" );
                }
            }, s.timeout);
        }

        // Send the data
        try {
            xhr.send( type === "POST" || type === "PUT" || type === "DELETE" ? s.data : null );
        } catch(e) {
            jQuery.handleError(s, xhr, null, e);
            // Fire the complete handlers
            complete();
        }

        // firefox 1.5 doesn't fire statechange for sync requests
        if ( !s.async ) {
            onreadystatechange();
        }

        function success() {
            // If a local callback was specified, fire it and pass it the data
            if ( s.success ) {
                s.success.call( callbackContext, data, status, xhr );
            }

            // Fire the global callback
            if ( s.global ) {
                trigger( "ajaxSuccess", [xhr, s] );
            }
        }

        function complete() {
            // Process result
            if ( s.complete ) {
                s.complete.call( callbackContext, xhr, status);
            }

            // The request was completed
            if ( s.global ) {
                trigger( "ajaxComplete", [xhr, s] );
            }

            // Handle the global AJAX counter
            if ( s.global && ! --jQuery.active ) {
                jQuery.event.trigger( "ajaxStop" );
            }
        }
       
        function trigger(type, args) {
            (s.context ? jQuery(s.context) : jQuery.event).trigger(type, args);
        }

        // return XMLHttpRequest to allow aborting the request etc.
        return xhr;
    }

Name: Anonymous 2010-05-20 15:34

>>6
Nothing wrong with that attitude, but one must learn wether knowing implementation details for a specific thing is worth their time or not, for we are but human and don't have the time to learn every system ever concieved.

Name: Anonymous 2010-05-20 16:00

>>9
A satori knows all systems

Name: Anonymous 2010-05-20 16:13

>>10
A person who has achieved Satori is not known as ``a satori''.

Name: Anonymous 2010-05-20 16:20

>>9
There definitely is something wrong with that attitude within an employee. My employee's time is expensive, and code is cheap or even free

Name: Anonymous 2010-05-20 16:35

>>12
Good thing I'm a student and not an employee.

Name: Anonymous 2010-05-20 16:40

>>13
Good thing it's likely to stay that way.

Name: Anonymous 2010-05-20 16:52

>>9
for we are but human and don't have the time to learn every system ever concieved.

i disagree with you in both points.

Name: Anonymous 2010-05-20 16:55

>>12
>>14
said the maintenance programmer

Name: Anonymous 2010-05-20 17:02

>>12
Spoken like a true [u][o]ENTERPRISE[o][u] project manager.

Name: Anonymous 2010-05-20 17:03

>>12
I had an employer like that once. That attitude eventually caused every competent programmer there to quit and form their own programming shop, leaving him with the two guys who loved Java and the ``graphic designer''. They went under a month later.
The world's not all bad, kids.

Name: Anonymous 2010-05-20 17:09

>>17
BBCODE failure.

Name: Anonymous 2010-05-20 22:24

>>14
Are you posting messages on this board from work?  A guy can take a little time to ask some questions can't he?

So jQuery covers the essentials for getting my code to work right in different browsers but doesn't really have much to do with the javascript that I write to get my ajax functionality to work properly?

Name: Anonymous 2010-05-21 8:53

Rewrite a jQuery-ish library in Lisp and make it compile to native javascript. You kill both your problems.

Otherwise use dojo instead of jQuery, since you can just import the modules you want.

Name: Anonymous 2011-02-04 18:27

Name: Anonymous 2013-09-01 10:27



   /i
  .| |
  | |
 .| |
 | |    __ノ ̄i. __,,,.........,,,__
 | |    ) ヽ、_ゝ-        `ヽ、
 | |    `y'"        `ヽ、 ヽ,
 .| |.    イ / / ⌒/ i  i⌒', ',   ',
 .| |    Lハ__L/ i__ハ__ハ___i .l   i
  | |.     ノ iT!'j   <  ト-┘  |
  .i i     ´7〈""  r─,  ""|   ノ  ..:::::::...
  l l__   く__,iゝ、_ ヽノ  ,.イ_ノ_i^i  ::   ::::::
 ⊂ ̄_ゝ、    `´_'ヽ'ハヘT´_/ヽ-ハ,.ト-、 ::  ::::::
  `! (、 i、  ,γ´ ヘ (ハ)´〒 /(つ   〉:  :::::
   iミiヽ、   ̄)  /  Y 。  i _`T ´|  :::::
   .lミl  ` ̄ ^ーイ  ハ ゚  .ハ"7/  i  :::
   

Don't change these.
Name: Email:
Entire Thread Thread List