jQuery.load and PHP's Header() redirect.
Hello All,
I came across the problem of headers when porting an application to jQuery. The header call gives an error and halts the process due to
headers already sent error, and after a while of Google searching I didn't really come up with much but a few people asking the question with no real answers. (at least none that suited me) So, how do you handle a load that your PHP may try to redirect with a header call?? Well, here is what I did...I duplicated the standard load function and created a loadphp one. This one generates a hidden iframe, loads the content, then ports it back into the element that called it in the first place. This works for me (in my environment), so no guarantees that it will work for you, but I found it helpful so figured I would share.
I can now redirect to my hearts content using PHP.
Also, I was able to use it in case my PHP session timed out on me and redirected to my login page. In my login page I can look to see if its TOP, and if not force it out of the IFRAME.
Of course, must be on same domain only.
jQuery.loadphp.js
- //######
- //## Modified jQuery Load to handle PHP header redirects
- //## Tyler Boyd, 2010
- //######
- jQuery.fn.extend({
- loadphp: function( url, params, callback ) {
- if ( typeof url !== "string" ) {
- return _load.call( this, url );
- // Don't do a request if no elements are being requested
- } else if ( !this.length ) {
- return this;
- }
- var off = url.indexOf(" ");
- if ( off >= 0 ) {
- var selector = url.slice(off, url.length);
- url = url.slice(0, off);
- }
- // Default to a GET request
- var type = "GET";
- // If the second parameter was provided
- if ( params ) {
- // If it's a function
- if ( jQuery.isFunction( params ) ) {
- // We assume that it's the callback
- callback = params;
- params = null;
- // Otherwise, build a param string
- } else if ( typeof params === "object" ) {
- params = jQuery.param( params, jQuery.ajaxSettings.traditional );
- type = "POST";
- }
- }
- var self = this;
-
- var iframe = $( '<iframe style="display: none; visibility: none;" name="loadframe" id="loadframe" src="about:none" />' );
- self.append(iframe);
-
- // Request the remote document
- $('iframe#loadframe').attr("src",url);
- $('iframe#loadframe').load(function(){
- var xmlhttp=new XMLHttpRequest();
- var iframeContent = $('iframe#loadframe').contents().get(0)["documentElement"]["outerHTML"];
- // reattach any css that could get cut off
- // and any javascript that might have been cut off too
- // which gets cut off due to the iframe re-organizing links back into the <head>
- //alert(iframeContent);
- iframeContent += $('iframe#loadframe').contents().get(0)["head"]["innerHTML"];
- self.html( selector ?
- // Create a dummy div to hold the results
- jQuery("<div />")
- // inject the contents of the document in, removing the scripts
- // to avoid any 'Permission Denied' errors in IE
- .append(iframeContent.replace(rscript, ""))
- // Locate the specified elements
- .find(selector) :
- // If not, just inject the full result
- iframeContent);
-
- if (iframeContent != ""){ status="success"; }else{ status="failed"; }
- if ( callback ) {
- self.each( callback, [iframeContent, status, xmlhttp] );
- }
- });
- return this;
- }
- });