jQuery AJAX Refresh in an iFrame

Tinkering around with a jQuery enabled Drupal module today, I was trying to make an iFrame that continuously updated. I tried the typical meta-tags and window.location.refresh() javascript but I wasn't satisfied with constant progress bar on the page. An AJAX (or AHAH really) request was needed to make this work smoothly.

The trick was combining some standard javascript selectors with the jQuery $ function. Here's my iframe:

And this is what was needed to select the top-level HTML tag within my iframe. Note that the 'script-console' used below is the name attribute of the iframe, not the id.

$('html', window.frames['script-console'].document);

So now to actually add the jQuery request and modify the iFrame:

function refreshConsole() {
$('body', window.frames['script-console'].document).loadIfModified("mytextfile.txt");
setTimeout("refreshConsole()", 1000);
}

$(document).ready(function() {
refreshConsole();
});

This code (placed the HEAD tag of the page), defines a refreshConsole function. Every second (1000 milliseconds), refreshConsole calls itself again. Every call, jQuery check the file "mytextfile.txt" for changes. If it's been updated, it retrieves the text and replaces the contents of the iFrame's BODY tag.

Comments

Hi there, first of all

Permalink Submitted by Marta (not verified) on Nov 26, 2007 | 04:48 pm MST

Hi there,

first of all thanks for sharing your code and help with jquery issues that sometime make us loose time to finishing tasks in a reasonable time.

I have the next problem,

I have a form with input elements, and I want to fill the input elements when the iframe is load. I'm trying to use

- javascript that is sit in the mail document that contains the iframe

$(window.frames['my-iframe'].document).ready(function() {

text = "testing"
updatevalue(text);

}

updatevalue(text) {

$('input#some-text', window.frames['my-iframe'].document).attr({
value: text,
});
}

- html of the document inside the iframe:

Testing jquery

Problem: works in firefox and safari if I call the function from the , but IE send an error message that is:

"Line xx
Char: 1
Error: Object doesn't support this property or method
Code: 0
URL: http://mysite.com/somethig"

Am I losing something?

cheers,

Marta

转载于:https://my.oschina.net/u/1047983/blog/129503

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值