Forum Moderators: open

Message Too Old, No Replies

Changing an Iframe src using Javascript

need src to change automatically each month

         

Wolf_man

5:30 pm on Aug 18, 2004 (gmt 0)

10+ Year Member



Hello, Im new to Webmasterworld.

I've run into a problem with a calender I have created.

On my main page, I have an iframe. In this iframe is the calendar (each month is its own page, ex; august.htm, september.htm,...)

Is there a way (using javacript I would think) that I can have the iframe src change automatically each month. So when it is september, the iframe src="september.htm", and then when its october the iframe src="october.htm".

I hope I've explained myself well.

any help, would be greatly appreciated.

Wolf_man

7:30 pm on Aug 18, 2004 (gmt 0)

10+ Year Member



Sorry for making a reply, but i wasent able to edit my first post.

anyways.. this is what I've tried to do, so far its not working.

in the head i have this;

<script language="javascript">

var nmonth= getmonth()
</script>
<script>

function date(calendar_month)
{
if (nmonth==0) {
document.all.calendar.src="january.htm";
}
if (nmonth==1) {
document.all.calendar.src="febuary.htm";
}
if (nmonth==2) {
document.all.calendar.src="march.htm";
}
if (nmonth==3) {
document.all.calendar.src="april.htm";
}
if (nmonth==4) {
document.all.calendar.src="may.htm";
}
if (nmonth==5) {
document.all.calendar.src="june.htm";
}
if (nmonth==6) {
document.all.calendar.src="july.htm";
}
if (nmonth==7) {
document.all.calendar.src="august.htm";
}
if (nmonth==8) {
document.all.calendar.src="september.htm";
{
if (nmonth==9) {
document.all.calendar.src="october.htm";
}
if (nmonth==10) {
document.all.calendar.src="november.htm";
}
if (nmonth==11) {
document.all.calendar.src="december.htm";
}
</script>

and in the body;

<iframe id=calendar src="august.htm" height="300" width="350"></iframe>

any help with what im doing wrong?

Birdman

6:18 pm on Aug 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello,

I suggest using the window.frames array. I added the event handler because I didn't see anything in your example that triggered the function. I also shortened it up a bit.

<script type="text/javascript">

document.body.onload = date; //event handler (calls the function)

function date()
{
var nmonth= getmonth();
var months = new Array("january.htm", "febuary.htm", etc...);
window.frames["calendar"].src = months[nmonth];
}
</script>

Also, I think you may need the name attribute as well for some browsers.

<iframe name="calendar" id="calendar" src="august.htm" height="300" width="350"></iframe>

PS: Welcome WolfMan!

Bernard Marx

6:43 pm on Aug 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Much better with that array.

The only problem I can see is that, if the script is in the head,
then the body element won't exist yet (for: document.body.onload).

I've taken out all the ".htm"'s and stuck a single one on the end.
I do like it sharp y'know.

[blue]window.onload[/blue] = date

function date()
{
var months = new Array(
'january','febuary','march','april','may','june','july',
'august','september','october','november','december')

window.frames['calendar'].src = months[new Date().getMonth()]+".htm"
}

I think you may need the name attribute as well for some browsers.

The Bird's right. Give the IFRAME identical id and name attributes.

Wolf_man

5:37 am on Aug 20, 2004 (gmt 0)

10+ Year Member



Thanx for your help, I figured out what I neede to do.

In case anyone want to know this is what I did;

<script language="javascript">
function LoadPage(){
var objFrame=document.getElementById("month_frame");
var curdate = new Date();
var month = curdate.getMonth();

objFrame.src=month+".htm";

}

</script>

I put this in the head section, and then i made the iframe's name and id "month_frame". I then renamed all my month.htm's according to thier number (ex; january=0.htm"). This way the src automatically chages when the month changes.

Thanx again for the help

Bernard Marx

7:14 am on Aug 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's nice.

Can I suggest a small change?
Call your files "month0.htm", "month1.htm" etc.
This gives the files a slightly more sensible name, but the main reason in that it's not good practice (and often invalid) to begin filenames with digits.
Then..

objFrame.src = "month" + month + ".htm"; 

BillPosters

7:34 am on Aug 20, 2004 (gmt 0)



If using javascript, then the month checker function will fail for those users with javascript disabled, which is averaging around 10% of users.

If your host server supports PHP (which most do), then this can be done much more easily and robustly* by using PHP than by using javascript.

Create a set of calendar pages and name them: January.htm, February.htm, …
(note the use of a capital in the name of the month).

You can then simply use the PHP date() [uk.php.net] or gmdate() [uk.php.net] functions to insert the month of the year into the src filename.

To insert use:


<?php echo(date("F"))?>

e.g.
To automatically insert the markup <iframe src="August.htm"></iframe>


<iframe src="<?php echo(date("F"))?>.htm"></iframe>

As you can see, you can insert the name of the current month just where you really need it - in the src filename itself.

The value/month returned by date("F") will change automatically each month and relates to the date set on your host server.

The only other thing you need to do is give the page containing the iframe tag the suffix .php, instead of .htm
This lets your host server know to check and execute any php functions in that document.

It's by far the simplest, most compact and efficient method to use for your task.

Incidentally, there are a variety of ways you can check and return the current month.
Look through the list of option on the date() page I've linked to and you'll see that months can be called according to their number too.

e.g.
To automatically insert the markup <iframe src="month08.htm"></iframe>


<iframe src="month<?php echo(date("m"));?>.htm"></iframe>

The value of m ranges from 01-12 and changes automatically each month according to the date set on your host server.

-

PHP commands are handled on your host server before they are delivered to the users' browser, so they do not depend on the users' browser settings in order for them to work properly.
PHP can be used in many situations where you might normally use javascript, but can offer a more robust and secure alternative to javascript methods.

It's not actually too complex to learn the basics of PHP if you're familiar with javascript, as the syntaxes of both are almost identical to eachother.
I'm relatively new to PHP and use it only from time to time, but I've found that the functions I use most often are also amongst the easiest to understand and implement - such as the various uses of the date() function.

Take a look at it and you'll quickly see how much it can improve the quality, stability and maintenance of your pages.

Bernard Marx

7:56 am on Aug 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Very good point.

..as the syntaxes of both are almost identical to each other.

- I wish! It looks like abstract algebra to me.

PHP is probably better than ASP classic (open source if for no other reason), but the good thing about ASP is that, if you script it with JScript, then you really do have the same syntax as Javascript.

Birdman

2:41 pm on Aug 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I'm not mistaken, using window.frames[] array will cater to old and new browsers. Using getElementById will limit your script to DOM compliant browsers.

BillPosters is definately correct about using server-side code. If that's possible for you.