/    Sign up×
Community /Pin to ProfileBookmark

changing iframe or object content with JS

I’m making a fairly simple site with a rant/blogging facility done in javascript.
I’m trying to get it to navigate through rants using previous and next buttons.

the basic structure of it looks like this (code has been trimmed)…

[CODE]function change_rant(dir)
{
switch (dir)
{
case “prev”: {rant_num–;} break;
case “next”: {rant_num++;} break;
}

document.rant.data = “rants/”+rant_num+”.txt”;

}

<a href=”javascript: change_rant(‘prev’)”>Previous Rant</a>
<a href=”javascript: change_rant(‘next’)”>Next Rant</a>

<script>
document.write(‘<iframe width=”750″ height=”300″ data=”rants/’+latest_rant_num+’.txt” name=”rant”></object>’);
</script>[/CODE]

Obviously, I’m trying to display a new text file (name with a number.txt eg 1.txt, 2.txt) containing the next/previous rant when the user clicks the next or previous link.

Problem is: It doesn’t work. The data attribute seems to be changed ( alert(document.rant.data) tells me that the rant number has been changed), but the new text file is not displayed.

I’ve also tried using an iframe instead of an object (and replacing ‘data’ with ‘src’).

I’m pretty much stumped… This method works fine for things like changing the src of an img tag…

I’m after 1) a way to get it working with iframes or objects, or 2) ideas about some other html/js based way of doing it.

Any help would be very much appreciated.?
Kefka

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Mr_JJun 29.2004 — To change the contents of an Iframe give it an ID and use


document.getElementById('iframe_id').src = "yourpage"
Copy linkTweet thisAlerts:
@KefkaauthorJun 29.2004 — it's all sorted thanks?

That was one of the problems...

The other, after hours of pulling my hair out, was the fact that I'd carelessly id'ed the div enclosing the iframe 'rant' as well.

-.-;

The little things...
×

Success!

Help @Kefka spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...