How to capture a whole page through Ajax

HTML, CSS and anything else that deals with client side capabilities.

Moderator: General Moderators

Post Reply
vasilis
Forum Commoner
Posts: 40
Joined: Tue Apr 22, 2003 7:37 am

How to capture a whole page through Ajax

Post 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?
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: How to capture a whole page through Ajax

Post 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;
}
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How to capture a whole page through Ajax

Post 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.
vasilis
Forum Commoner
Posts: 40
Joined: Tue Apr 22, 2003 7:37 am

Re: How to capture a whole page through Ajax

Post by vasilis »

yes, it seems that that's what I was looking for.,,,thanks for the help.
Post Reply