Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem referencing iframe 1

Status
Not open for further replies.

dace

Programmer
Jul 21, 2001
263
US
Hi everyone-

I have an asp.net page which uses response.write to add some javascript to a page when it loads.

Basically, I have a page that contains an iframe, and I want to set the source of this iframe to a different page.

I have tried the following:

<script language="javascript"> frames['PageContentFrame'].location.href='newpage.html') </script>

and also:

<script language="javascript"> document.getElementById('PageContentFrame').src='newpage.html'; </script>

But I receive the error "frames.PageContentFrame.location is null or not an object" with either method.

My iframe has id="PageContentFrame" and name="PageContentFrame", and I'm sure they're unique.

Any suggestions?
 
Using the second method you describe should work perfectly:

Code:
<script language="javascript">document.getElementById('PageContentFrame').src = 'newpage.html';</script>

And while I can understand that you'd get the error you gave with the first method, the second method, if it were to error, should give a different message entirely.

Are you sure that when you had your second method in place, that you weren't viewing a cached version of the page? I'd try putting it back in place, closing down your browser, clearing your cache, and trying again.

Hope this helps,
Dan
 
Hi- thanks for your reply. I tried:

<script language="javascript"> alert(document.getElementById('PageContentFrame').location.href); </script>

gives an error saying the property or method is not supported, and if I try:

<script language="javascript"> alert(document.getElementById('PageContentFrame').src); </script>

I get an error saying "Object required".

And finally when I try:
<script language="javascript">document.getElementById('PageContentFrame').src = 'newpage.html';</script>

I get "document.getElementById(..) is null or not an object"


I'm sure it's not caching, as I'm compiling the pages directly from a .net development environment. (I've also tried loading the compiled pages in a browser, so I know this isn't the problem either).
 

Aaah - make sure your page has fully loaded before running the script.

Are you trying to run the script inline, or after the page has loaded?

Dan
 
Hm, that's a good question. That may very well be the problem.

I'm using response.write() to add the javascript line to an http response for a postback (basically page is refreshed and this is added onto the http response). I don't think I have any way of knowing when/where it's being executed. Would this cause the problem? Is there a way to prerender the iframe to make sure I can set a target for it?
 

I dont think you can pre-render an iframe. You'd have to add an onload event to your body tag:

Code:
<body onload="myFunction();">

And then move your JS code from inline to a function:

Code:
<script language="javascript">
function myFunction() {
   document.getElementById('PageContentFrame').src = 'newpage.html';
}
</script>

That way, you can be sure the the iframe will exist by the time you want to access it.

Hope this helps,
Dan
 
The above won't work because I have to dynamically set the source from server-side asp, but at least now I know what my problem is!

Thanks!
 

>> The above won't work because I have to dynamically set the source from server-side asp

Maybe I'm missing something here... Surely if you have the URL within ASP, you can just have something like this delivered to the page?:

Code:
<script language="javascript">
function myFunction() {
   document.getElementById('PageContentFrame').src = '<%=yourURLvariableNameHere%>';
}
</script>

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top