Solved

How can I utilize AJAX to reload the contents of iFrame every 30 seconds?

Posted on 2010-08-24
16
933 Views
Last Modified: 2012-05-10

The iFram on my page displays different aspx pages, switching between the 4 pages every 30 seconds. As of now I am using javascript and jquery and wondering if i could utilize AJAX.

Each aspx page contains flash
0
Comment
Question by:YZlat
  • 8
  • 7
16 Comments
 
LVL 9

Expert Comment

by:wellhole
Comment Utility
You don't need ajax if you're only refreshing an iframe. Just use javascript on a 30 second timer that changes the src for the iframe.
0
 
LVL 35

Author Comment

by:YZlat
Comment Utility
But then the whole page gets refreshed whenever iFrame gets refreshed
0
 
LVL 9

Expert Comment

by:wellhole
Comment Utility
Oh, i misunderstand your question then. You absolutely should use ajax to change the content of the iframe contents. Just use ajax to talk to your server for content, somehow identify your control in the iframe, and replace it with the result from your server connection. This is an example of javascript  I used to replace content on my page.
$.ajax(
  {
  	type: "POST",
  	dataType: "json",
  	url: "tireSearch/VehicleYears.aspx",
  	data: { VehicleMake: vehicleMake },
  	success: function(result) {
  		var options = "<option>SELECT YEAR</option>";

  		for (var i = 0; i < result.Years.length; i++) {
  			options += "<option>" + result.Years[i] + "</option>"
  		}

  		$("[name='VehicleYear']", searchForm).html(options);

  		if (result.Years.length == 1) {
  			$("[name='VehicleYear']", searchForm).val(result.Years[0]);
  			VehicleYearChange($("[name='VehicleYear']", searchForm));
  		}
  		else {
  			$("[name='VehicleMake']", searchForm).removeAttr("disabled");
  			if (result.Years.length > 0) {
  				$("[name='VehicleYear']", searchForm).removeAttr("disabled");
  				$("[name='VehicleYear']", searchForm).focus();
  			}
  		}
  	}
  });

Open in new window

0
 
LVL 35

Author Comment

by:YZlat
Comment Utility
That's why I posted a question here, because I need help doing what you described
0
 
LVL 35

Author Comment

by:YZlat
Comment Utility
wellhole, could you help me with that? I am a beginner with AJAX
0
 
LVL 9

Expert Comment

by:wellhole
Comment Utility
To do ajax, your javascript must use something to the effect of the code I posted before. My code uses the POST method to access the url member passing it the data members, expects a result of the datatype json, and finally when and if its successful it'll run the function in success with the result parameter.

I'm not going to explain jquery since you should understand that already and if not then you should check out the examples on the jquery homepage because there are very good examples there.

Below is the codebehind for the aspx page my above javascript is trying to access. It should be very self explanatory if you have coding experience. You'll pretty much just have to fill in result.Years to get results for your ajax. You can also return more than 1 member in result and you can change the type of member in the response from a list to anything else.
Imports System.Runtime.Serialization

Imports System.Runtime.Serialization.Json



Partial Class TireSearch_VehicleYears

  Inherits System.Web.UI.Page



  <DataContract> Public Class RequestResponse

    <DataMember()> Public Years As List(Of String) = New List(Of String)

  End Class



  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim result As RequestResponse = New RequestResponse

    Dim vehicleMake As String = Utilities.IsNull(Request.Form("vehicleMake"))

    result.Years = TireSearchVehicles.VehicleYears(vehicleMake)

    Response.Clear()

    Response.ContentType = "application/json"

    Dim jsonSerializer As DataContractJsonSerializer = New DataContractJsonSerializer(GetType(RequestResponse))

    jsonSerializer.WriteObject(Response.OutputStream, result)

    Response.Flush()

    Response.End()

  End Sub

End Class

Open in new window

0
 
LVL 35

Author Comment

by:YZlat
Comment Utility
but what it has to do with switching content of iFrame?
0
 
LVL 9

Expert Comment

by:wellhole
Comment Utility
I gave you code using jquery to utilize ajax and change web content and I even gave you the backend code to return results to the ajax call. If this doesn't get you anywhere then you should be asking for help with javascript in general.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 35

Author Comment

by:YZlat
Comment Utility
wellhole, with all due respect, your code has nothing to do with iframes
0
 
LVL 9

Expert Comment

by:wellhole
Comment Utility
YZlat, can you rephrase your question in a less obscure way? Be more precise to the exact problem. Apparently, I'm giving you answers to a question you're asking which you then say is not the question you're asking.
0
 
LVL 35

Author Comment

by:YZlat
Comment Utility
I though I was clear:  have an iFram and I want to display different aspx pages in my iFrame so that every 30 seconds new aspx page is displayed in the iFrame. I have 4 aspx pages I want to display and each of them displays a flash file. I would like to do that with AJAX.
0
 
LVL 9

Expert Comment

by:wellhole
Comment Utility
Then, you shouldn't have objected to my very first post.
var frames = Array('http://www.google.com/', 15,
    'http://www.yahoo.com/', 37,
    'http://www.ask.com/', 12,
    'http://www.dogpile.com/', 14);
var i = 0, len = frames.length;
function ChangeSrc()
{
  if (i >= len) { i = 0; } // start over
  document.getElementById('frame').src = frames[i++];
  setTimeout('ChangeSrc()', (frames[i++]*1000));
}
window.onload = ChangeSrc;

Open in new window

0
 
LVL 35

Author Comment

by:YZlat
Comment Utility
I already have javascript code similar to this and iFrame gets refreshed when the whole page gets refreshed. I'd like to utilize AJAX to do that.

To clarify: I can refresh the page every 30 seconds and change the aspx page displayed in iFrame, that's what I've been doing for over a year. My question is how to do that utilizing capabilities of AJAX
0
 
LVL 9

Assisted Solution

by:wellhole
wellhole earned 100 total points
Comment Utility
You do so by combining the ajax code with the timer code that refreshes the iframe.

You'll want to create a somepage.aspx file similar to the one I posted in post id 33541630 to return the result of the new url for the iframe.
function ChangeSrc()
{
  $.ajax({
     type: "POST",
     url: "somepage.aspx",
     data: ({paramname1 : datavalue1, paramname2 : datavalue2}),
     dataType: "json",
     success: function(msg){
       document.getElementById('frame').src = msg;
       setTimeout('ChangeSrc()', 30000);
     },
     error: function(XMLHttpRequest, textStatus, errorThrown) {
       setTimeout('ChangeSrc()', 30000);
     }
   });
}

window.onload = ChangeSrc;

Open in new window

0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 400 total points
Comment Utility
You may have a problem using AJAX for this because of the Flash.  I also noticed this question may be ending or closing but, I noticed it, and want to clarify something.

When you use AJAX you would not also use a frame/iframe.  The point of AJAX is to make replace the frame/iframe.  I am not sure why your refresh of the content in an iframe would refresh the whole page but, if it is happening, then that can be changed.  However that would not need AJAX.  It would just be javascript that would reload the frame with the next URL.

For AJAX you would replace the iframe section with a div or other "container" html element.  The AJAX code would dynamically reload the contents of that div or container.  A problem though is if you are trying to show a complete web page.  An iframe/frame can do this because it is like another window.  However with AJAX the "window" or reloading part is part of a page.  You can't fill it with the html for a complete page.  Basically, when using AJAX, you are getting the code for just part of a page.

For example, if you were to use AJAX to dynamically validate a person's typing in an input box the AJAX code is just filling in some part of that web page.  In the case where AJAX is used to validate a member name (i.e. it is unique) then the AJAX code (javascript that runs some other server code using an "xmlhttp" object) would just show a message in a "container" right next to the input box.

If you have to show a complete web page then you should stick to an iframe.  There are some elaborate ways to do it without an iframe or frame but they are not easy (and involve more than just "ajax" code).

Let me know how this helps or if you have a question.

bol
0
 
LVL 35

Author Comment

by:YZlat
Comment Utility
b0lsc0tt, thanks a lot! that's exactly what i was looking for and exactly what I did but I ran into some issues displaying flash. Could you please take a look at my other question from 09/08/10:

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_26459170.html
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Hello World !, Thanks : My Sincere thanks to @Michael Male who encouraged me to write an article on this in my  blog (http://scribblejava.wordpress.com). Introduction:       There are several technical issues a developer may face in his…
There is basically two types of AJAX request in mootools. Request  and Request.HTML Request: Request is the basic XHR request class in MooTools. While not extremely useful on its own, it provides the basic functionality for both Request.HTM…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now