Page 1 of 1

How to capture a whole page through Ajax

Posted: Fri Jun 26, 2009 7:41 am
by vasilis
I have a list box in a site with which I capture a selected value with the onChange event using the capture_value() function (code listed below). This function passes 2 arguments, i.e., 'str' which is the selected list box value and 'passed_url' which is a passed url for
running a php script (which contains some url query parameters, e.g. 'somescript.php?var1=value1&var2=value2&var3=value3').
The capture_value() function actually sends a Ajax request with the GET method and a URL which is formed by appending the captured list box value to the 'passed_url' url value (i.e. the 'passed_url' is changed to 'somescript.php?var1=value1&var2=value2&var3=value3&str=value4'.
Now, what I want to do is to capture the whole generated page from running the final 'passed_url' in the same window (as if I entered 'somescript.php?var1=value1&var2=value2&var3=value3&str=value4' in the location field of the browser and pressed <Enter>).

In other words, I want to output in the same window the generated page from running the 'somescript.php?var1=value1&var2=value2&var3=value3&str=value4' url address.

Below is the code listing of the Javascript file I use.
----------------------------------------------------------------------------------

Code: Select all

var xmlhttp;
 
function capture_value(passed_url,str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request ");
  return;
  }
//var url="http://www.test.local/ach_handcrfts/product.php";
passed_url=passed_url+"&drawing_style="+str;
passed_url=passed_url+"&sid="+Math.random();
//alert ("url: " + url);
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",passed_url,true);
xmlhttp.send(null);
 
}
 
function stateChanged()
{
if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
           window.open(xmlhttp.responseXML,"_self");
       
        } else {
            alert("Response Error:n" + xmlhttp.statusText);
        }
    }
 
}
 
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari  
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}
Now, when I select a value in the list box, the desired page is not generated (nothing happens). When I check with Firebug, I can see that I have no errors, and I get the expected GET response in the Firebug console, and when I click on it's console link with the right button and then click on "Open in New Tab", a new browser window opens with the expected page, but this page is not generated by selecting a value in the list box, i.e. somewhere in the end of the code I need to add some code to output the whole page in the same window.

I am sure it's something simple missing or needing modification.
Could you please somebody help?

Re: How to capture a whole page through Ajax

Posted: Sat Jun 27, 2009 9:14 am
by kaszu
Is this what you were looking for?

Code: Select all

function capture_value(passed_url,str)
{
    passed_url=passed_url+"&drawing_style="+str;
    passed_url=passed_url+"&sid="+Math.random();
    document.location.href = passed_url;
}

Re: How to capture a whole page through Ajax

Posted: Sat Jun 27, 2009 12:35 pm
by califdon
The Ajax function returns either text or JSON. If you want to return an entire page, you don't need or want to use Ajax, just redirect to the desired page. The whole point of the Ajax request is to avoid loading an entirely new page.

Re: How to capture a whole page through Ajax

Posted: Sun Jun 28, 2009 2:33 pm
by vasilis
yes, it seems that that's what I was looking for.,,,thanks for the help.