Forum Moderators: open

Message Too Old, No Replies

how to determine content in iframe on page b

         

tkuhrt

2:38 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



I am on page A. When I click a link, I want to load page b and specify the html file to load in the iframe I have on page b.

Can some show me or point me to a tutorial on how to do this? I asume it is a javascript command.

Thx

Todd

Bernard Marx

3:26 pm on Sep 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just a pointer today...
Topic: Query string in URL

Link looks like this:

<a href="page1.htm?doo.htm">page2: loads doo.htm</a>

On page2.htm:

When page has loaded, or at least after the IFRAME's HTML has loaded,
get the query string..

var query = location.search.substring(1) // removes the "?"
document.frames["_iframe_id_"].src = query

tkuhrt

3:41 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



thank you much for your help. I am not a javascript guru, but I am looking for something like this, that will also go to default if a link without the?doo.htm is added. This script Im am sending is to load a form or something, but I just want to load an html in iframe name=content.

can you change this script and send it back?

thx a ton if you can..

"Page 1"
[google.com...]

"Page 2"
<html>
<head>
<script language="JavaScript" type="text/javascript">
<!--
var urltext = document.location.href;

if (urltext.indexOf('?')!= -1)
targeturl = urltext.substring((urltext.indexOf('?')+1),urltext.length);
else
targeturl = document.location.href;
// defaults to current page if no parameter is passed

function init(){
document.forms[0].p_requested_url.value = targeturl;
}
//-->
</script>
</head>
<body onload="init()">
<form>
<input type="hidden" name="p_requested_url" value="">
</form>
</body>
</html>

AWildman

3:50 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



What you want to do is replace the statement within the init() function to:
location.replace(targeturl);

tkuhrt

4:03 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



Thx so much. I am closer, but not there yet.

Page be is loading, but then the entire page changes to the html file that should load in the iframe.

I changed

function init(){
document.forms[0].p_requested_url.value = targeturl;
}

to

function init(){
location.replace(targeturl);
}

where do I specify the iframe name "content"

thx!

AWildman

5:14 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



Oops. Sorry, noticed two things.
First, let's go over the actual process so we can break down what you want to happen so you can see how to make that happen.

Step 1: Click a link on page a
<a href = "../somedir/pageb.html?iframepage.html">Click here</a>

Step 2: Go to page b which contains the iframe
[browser takes you to pageb.html]

Step 3: Change the iframe's content to the substring
So, now, the browser is loading page b. On page b, you put that code that you have which strips out the substring information (everything after the? in the url).
<script language="JavaScript" type="text/javascript">
<!--
var urltext = document.location.href;

if (urltext.indexOf('?')!= -1)
targeturl = urltext.substring((urltext.indexOf('?')+1),urltext.length); //this gets the substring i.e. - the name of the iframe
else
targeturl = document.location.href;
// defaults to current page if no parameter is passed

function init(){
document.framename.src = targeturl;
}

Bernard Marx

5:18 pm on Sep 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have a look at my previous again :)
This should be all you need on the new page..
[pre]
window.onload = init
//! change ¦¦ to unbroken pipes (ctrl+\)
function init()
{
var query = location.search.substring(1) // removes the "?"
// if query is empty string, default.htm is used
document.frames["_iframe_id_"].src = query ¦¦ default.htm
}
[/pre]

You want the iframe to default to the current page?

tkuhrt

5:48 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



this is what I have:

*page one link:

<a href="infection.html?infect_prevent/occupation_overview.html" target="_self">Occupational
Exposure to Bloodborne Pathogens</a>

*page two with an iframe:

//the javascript
var urltext = document.location.href;

if (urltext.indexOf('?')!= -1)
targeturl = urltext.substring((urltext.indexOf('?')+1),urltext.length);
else
targeturl = document.location.href;
// defaults to current page if no parameter is passed

function init(){
document.content.src = targeturl;
}

* in the body tag
<body onLoad= "init()" >

*iframe name "content"

It is loading page2, but Im just getting the default iframe content.

AWildman

6:38 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



If there are no other frames in the document, try replacing:
document.content.src = targeturl;
with
document.frames[0].src = targeturl;

If that doesn't work, try the following

var urltext = document.location.href;
alert(urltext.indexOf("?"));
if (urltext.indexOf('?')!= -1)
targeturl = urltext.substring((urltext.indexOf('?')+1),urltext.length);
else
targeturl = document.location.href;
// defaults to current page if no parameter is passed

All I'm doing here is adding an alert to see which fork of the if statement the script is taking.

tkuhrt

6:49 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



I have only one iframe.

I get an alert 80 in IE on PC, and a 98 on IE MAC

AWildman

7:02 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



This means that in my alert code, the script is discerning that a question mark exists in the url. Perhaps the problem is a syntax error. So try this. If you'll notice in my code is that there are double-quotes "" around the question mark in my alert. In the if statement, there are not. Put double-quotes around the question marek in the if statement. if(urltext.indexOf("?")!= -1)

If that works, remove the alert and you're good to go.

AWildman

7:04 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



Oh, and also add double-quotes to the question mark in the first fork of the if statement as well.

tkuhrt

7:15 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



Sorry for taking up so much of your time.

Still no go... Im at a loss...

tkuhrt

7:21 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



the closest script to work was this one I got somewhere else:

index.html
<html>
<body>
<a href=infection.html?ifsrc=infect_prevent/occupation_overview.html>go to intro</a>
</body>
</html>

infection.html
<html>
<script>
function getSource()
{
   ifsrc=location.search.substring(1)
   ifsrc=ifsrc.substring(6,ifsrc.length)
   alert(ifsrc)
   document.getElementById('content').src = ifsrc
}
</script>
<body onLoad="getSource()">
<iframe id="body" name="content" width="400" height="430" scrolling="yes" frameborder="0" marginwidth="0" marginheight="0" ></iframe>
</body>
</html>

this also had problems. when I clicked on infection.html link from the index.html(the default iframe) I would get a blank frame.

AWildman

7:51 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



You need the id of the iframe to be "content".

I use nearly the same script that I have been helping you with cause I do the same thing. I haven't had a problem with it. Why is it still not working for you? What's happening?

tkuhrt

8:09 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



IM not sure. Im doing a lot of testing on a Mac also. Not sure. I did get it to work with this finally:

index.html:

<a href="infection.html?iframelink=infect_prevent/occupation_overview.html">

AWildman

8:18 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



Well, so long as you got your script to work, that's all that matters! :)

Sorry I wasn't of more help. Let me know if you're still having issues with the script.

Bernard Marx

10:05 pm on Sep 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This might cause a problem:

document.getElementById('content').src = ifsrc
...
<iframe id="body" name="content"