Forum Moderators: open

Message Too Old, No Replies

Linking to iFrame on Another Page

How do I override the default iframe source?

         

katana_one

4:55 pm on Nov 16, 2004 (gmt 0)

10+ Year Member



I was trying to figure out if this is possible without the use of JavaScript.

Say I have an index page (index.html) and I have another page featuring an iframe (iframe.html) which of course has a default source page (default.html).

What I want to do is put a link on index.html that takes the user to iframe.html, but instead of displaying default.html in the iframe, we substitute another page to appear (alternate.html).

Can this be done? I tried searching, but came up confused and empty-handed.

AWildman

5:19 pm on Nov 16, 2004 (gmt 0)

10+ Year Member



What needs to happen is a few things:
1. Change location to iframepage.html
2. Change iframe src to newpage.html

So, one easy way that I've done it is to add parameters to the anchor on the index.html page.

<a href = "iframepage.html?newsrc=newpage.html">text</a>

Then, onload of iframepage.html, check to see if there is a param (there is "?newsrc=newpage.html") and if so, change the iframe src to newpage.html.

I'm pretty sure there are actual code snippets on this forum about doing just this sort of thing cause I've posted some before. Search for onload and location.href. If you can't find it, lemme know and I'll find it to post again.

katana_one

6:25 pm on Nov 16, 2004 (gmt 0)

10+ Year Member



Thanks wildman, that sounds like what I need. I will check it out when I have a chance.

katana_one

7:04 pm on Nov 17, 2004 (gmt 0)

10+ Year Member



Wildman, I did a search and was not able to find the thread you were referring to. Care to post the link?

AWildman

7:25 pm on Nov 17, 2004 (gmt 0)

10+ Year Member



I doubt I'll be able to find the link, so I'll just post it here.

Step 1: Anchor href (assumes changeme is iframe name
<a href="somedir/iframepage.html?changeme= ../nutherdir/newiframesrc.html" target = "_top">some anchor text</a>

Step 2: Iframe page onload
<body onload = "iframe_src()";>

Step 3: Onload function

function iframe_src()
{
var locsubstr = location.search.substring(1);//find url parameter

if((locsubstr.indexOf("changeme")!= -1))//if there is a new path for changeme
{
var changemeloc = locsubstr.substring(locsubstr.indexOf("=") + 1, locsubstr.length);//find the newpath for changeme
parent.frames[0].location.replace(changemeloc);//change changeme to new path
}
else
{parent.frames[0].location.replace(defaultpath);}//if there isn't a new path, set changeme to default
}

katana_one

7:43 pm on Nov 17, 2004 (gmt 0)

10+ Year Member



Maybe I'm missing something, but I can't get it to work for me.

I'm thinking about dumping the iframe page in favor of a regular framed page. Will this make it easier?

AWildman

8:02 pm on Nov 17, 2004 (gmt 0)

10+ Year Member



Are you getting an error message? What happens when you click the link?

I don't know that regular frames will make it any easier. I've not worked with them, which is odd, but I digress.

katana_one

9:59 pm on Nov 17, 2004 (gmt 0)

10+ Year Member



I used the code you provided above in a couple of test pages, and when you click the link the page with the iframe loads with the default source. No error message.

AWildman

12:56 pm on Nov 18, 2004 (gmt 0)

10+ Year Member



Very interesting. I don't think its a result of the code, but I couldn't tell you what exactly is causing it.

katana_one

1:01 pm on Nov 19, 2004 (gmt 0)

10+ Year Member



I'm assuming the "onload function" gets put in the head of the document as JavaScript. Is this correct?

AWildman

1:10 pm on Nov 19, 2004 (gmt 0)

10+ Year Member



Uh, you COULD do that, but it would be much simpler to create an external js file so that you can easily reference it on every page that will load in the iframe.

So, in the head of the iframe page, you'd have:
<SCRIPT LANGUAGE="JavaScript" SRC="somedir/external.js" type = "text/javascript"></SCRIPT>

And then the body would have the onload:
<body onload = "change_iframe_src()">

Mind you, I forgot what I called the function that I posted earlier, just make it match that.

katana_one

1:23 pm on Nov 19, 2004 (gmt 0)

10+ Year Member



The JavaScript goes in the iframe page, or the pages to be loaded into the iframe?

AWildman

1:28 pm on Nov 19, 2004 (gmt 0)

10+ Year Member



The javascript goes on the page containing the iframe.

<added>Hmmm...I guess I can see why you'd be confused given my earlier post. Please excuse my EXTRAORDINARILY sleepy brain for posting that! Hubby couldn't sleep last night so, of course, neither could I... :)</added>

katana_one

2:05 pm on Nov 19, 2004 (gmt 0)

10+ Year Member



Thanks for the help. If anyone else has ideas, please feel free to chime in.