jQuery.load and PHP's Header() redirect.

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
  1. //######
  2. //## Modified jQuery Load to handle PHP header redirects
  3. //## Tyler Boyd, 2010
  4. //######

  5. jQuery.fn.extend({
  6. loadphp: function( url, params, callback ) {
  7. if ( typeof url !== "string" ) {
  8. return _load.call( this, url );

  9. // Don't do a request if no elements are being requested
  10. } else if ( !this.length ) {
  11. return this;
  12. }

  13. var off = url.indexOf(" ");
  14. if ( off >= 0 ) {
  15. var selector = url.slice(off, url.length);
  16. url = url.slice(0, off);
  17. }

  18. // Default to a GET request
  19. var type = "GET";

  20. // If the second parameter was provided
  21. if ( params ) {
  22. // If it's a function
  23. if ( jQuery.isFunction( params ) ) {
  24. // We assume that it's the callback
  25. callback = params;
  26. params = null;

  27. // Otherwise, build a param string
  28. } else if ( typeof params === "object" ) {
  29. params = jQuery.param( params, jQuery.ajaxSettings.traditional );
  30. type = "POST";
  31. }
  32. }

  33. var self = this;
  34. var iframe = $( '<iframe style="display: none; visibility: none;" name="loadframe" id="loadframe" src="about:none" />' );
  35. self.append(iframe);
  36. // Request the remote document
  37. $('iframe#loadframe').attr("src",url);
  38. $('iframe#loadframe').load(function(){
  39. var xmlhttp=new XMLHttpRequest();
  40. var iframeContent = $('iframe#loadframe').contents().get(0)["documentElement"]["outerHTML"];
  41. // reattach any css that could get cut off
  42. // and any javascript that might have been cut off too
  43. // which gets cut off due to the iframe re-organizing links back into the <head>
  44. //alert(iframeContent);
  45. iframeContent += $('iframe#loadframe').contents().get(0)["head"]["innerHTML"];
  46. self.html( selector ?
  47. // Create a dummy div to hold the results
  48. jQuery("<div />")
  49. // inject the contents of the document in, removing the scripts
  50. // to avoid any 'Permission Denied' errors in IE
  51. .append(iframeContent.replace(rscript, ""))
  52. // Locate the specified elements
  53. .find(selector) :
  54. // If not, just inject the full result
  55. iframeContent);
  56. if (iframeContent != ""){ status="success"; }else{ status="failed"; }

  57. if ( callback ) {
  58. self.each( callback, [iframeContent, status, xmlhttp] );
  59. }
  60. });

  61. return this;
  62. }
  63. });