Jump to content

ajax to change src of iframe


brown2005

Recommended Posts

Hi, I have an icon, that when I click, refreshes the page and loads a new iframe src each time. What I want to do is elimante the need to refresh the whole page, and just refresh the iframes src. can this be done with ajax and does anyone know any code examples. thank you.

Link to comment
Share on other sites

I'm not sure if it's possible to reload an iframe or not.  However, one thing you could probably do is to add a div tag to every iframe that you have.  You might be able to put the ajax code in the "parent" page and then have it refresh the content inside of the div tag in the iframe.  Of course, the ENTIRE content of the iframe would just need to be contained inside of that div tag.

 

Try putting this javascript into your parent page:

 

<script type="text/javascript">
var xmlhttp;

function reloadIframe(page)
{
  xmlhttp=GetXmlHttpObject();
  if (xmlhttp==null)
  {
    alert ("Sorry but your browser does not support AJAX or Javascript has been disabled");
    return;
  }

  // append an random value to the page being called
  // to prevent browser caching
  var url = page + "?id=" + Math.random();


  // execute the fully formed request
  xmlhttp.onreadystatechange=stateChanged;
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
}


function stateChanged()
{
  if (xmlhttp.readyState==4)
  {
    document.getElementById("iframeContent").innerHTML=xmlhttp.responseText;
  }
}


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;
}
</script>

 

In this example all you have to do is put a div tag with the id "iframeContent" in your iframe page (let's call it foo.php) like this:

 

<?php
  echo "<div id=\"iframeContent\">\n";

  // the page content goes here

  echo "</div>\n";
?>

 

 

Then your button should invoke the javascript function with the name of the target page.  Something like:

 

echo "<a href=\"javascript:reloadIframe('foo.php')\"><img src=...></a>\n";

 

Does that work?  I dunno...I don't mess with iframes usually.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.