472,126 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,126 software developers and data experts.

iframe problem - null object error when changing url

Hi,

I've struggled with this for a while now, so I'm asking for help. I
am trying to click on a link in one page, have that link open up in a
new window, and set that newly opened window's ifram to a specific
url. Here is the code I'm using:

<html>
<head>

<script>
var myWindow;
function run(page1,framepage) {
myWindow=window.open(page1,'window');
myWindow.frames['winPanel'].location.href=framepage;
if (window.focus) {myWindow.focus()}
}
</script>

</head>
<body>
<a href="javascript:"
onClick="run('scroll.html','temp.html');">click4lo cal</a>
<a href="javascript:"
onClick="run('http://www.mysite.com/page_with_iframe.htm','http://www.mysite.com/page_to_go_in_iframe.htm;">click4remote</a>
</body>
</html>

The strange this is that the first link works just fine when I run it
locally, but the second link won't work when I put it on my server.
It opens up a page with the iframe, but I get the error "line: 10
character 2 'myWindow.iframes.winPanel.location is null or not an
object," so it doesn't load the url into the iframe.

I appreciate any help you can give me!

Jim
Jul 23 '05 #1
5 4049
Jim Marquardson wrote:
Hi,

I've struggled with this for a while now, so I'm asking for help. I
am trying to click on a link in one page, have that link open up in a
new window, and set that newly opened window's ifram to a specific
url. Here is the code I'm using:

<html>
<head>

<script>
var myWindow;
function run(page1,framepage) {
myWindow=window.open(page1,'window');
myWindow.frames['winPanel'].location.href=framepage;
if (window.focus) {myWindow.focus()}
}
</script>
Why not pass the src of the iframe?
myWindow=window.open(page1+"?pageWithIFrame.html", 'window')

Then parse the IFrame src from the location.search property of
pageWithIFrame.html

if(location.search && isFile(location.search.substring(1)){
document.frames['winPanel'].src=location.search.substring(1);
}
The "isFile(file") function needs to examine the query to see if it is a
proper candidate for the iframe.

Mick

</head>
<body>
<a href="javascript:"
onClick="run('scroll.html','temp.html');">click4lo cal</a>
<a href="javascript:"
onClick="run('http://www.mysite.com/page_with_iframe.htm','http://www.mysite.com/page_to_go_in_iframe.htm;">click4remote</a>
</body>
</html>

The strange this is that the first link works just fine when I run it
locally, but the second link won't work when I put it on my server.
It opens up a page with the iframe, but I get the error "line: 10
character 2 'myWindow.iframes.winPanel.location is null or not an
object," so it doesn't load the url into the iframe.

I appreciate any help you can give me!

Jim

Jul 23 '05 #2
> Why not pass the src of the iframe?
myWindow=window.open(page1+"?pageWithIFrame.html", 'window')

Then parse the IFrame src from the location.search property of
pageWithIFrame.html

if(location.search && isFile(location.search.substring(1)){
document.frames['winPanel'].src=location.search.substring(1);
}
The "isFile(file") function needs to examine the query to see if it is a
proper candidate for the iframe.

Mick


Thanks for the code. It opened up the new window, but it still didn't
load the right page in the iframe and now I am getting another error:
"Access is denied." I think this problem has to do with iframes
security, and I don't think there's an easy fix.

Jim
Jul 23 '05 #3
I worked some more at the problem, and I solved it with your code. It
took some more coding to make your solution work, but it ended up
being the best solution. I thought I'd post what I ended up doing.

A sample link would be <a
href="PageWithFrame.html?nonDefaultContent.html">L ink</a>

Then, the HTML file:

<html>
<head>
<script>
function writeFrame() {
page = "defaultContent.htm"; //the default page
if(location.search /*&& isFile(location.search.substring(1))*/) {
page = location.search.substring(1);
}
document.write("<iframe src=\"" + page + "\" id=\"myIframe\"
name=\"myIframe\" width=\"800\" scrolling=\"no\"
frameborder=\"0\"></iframe>\"");
}
</script>
</head>

<body>
This is the page. Content goes here.
<script>writeFrame();</script>
</body>
</html>

I had too many problems trying to change the src of the iframe on
loading (usually because the iframe hadn't completed loading), so I
decided to write out the correct src the first time. That way, I
don't have to worry about changing it.

Thanks for you help,

Jim
Jul 23 '05 #4
Instead of document.write statement, put the <iframe id=...
name=...></iframe> code into HTML directly - without a src argument.

after that code, run your script to give a src (better be location.href) for
instants: document.frames('name_of_the_iframe').location.hre f=...;

This way you will not meet any trouble.

ccton

--
www.vicdir.com
"Jim Marquardson" <ji*@marqorp.com>
I worked some more at the problem, and I solved it with your code. It
took some more coding to make your solution work, but it ended up
being the best solution. I thought I'd post what I ended up doing.

A sample link would be <a
href="PageWithFrame.html?nonDefaultContent.html">L ink</a>

Then, the HTML file:

<html>
<head>
<script>
function writeFrame() {
page = "defaultContent.htm"; //the default page
if(location.search /*&& isFile(location.search.substring(1))*/) {
page = location.search.substring(1);
}
document.write("<iframe src=\"" + page + "\" id=\"myIframe\"
name=\"myIframe\" width=\"800\" scrolling=\"no\"
frameborder=\"0\"></iframe>\"");
}
</script>
</head>

<body>
This is the page. Content goes here.
<script>writeFrame();</script>
</body>
</html>

I had too many problems trying to change the src of the iframe on
loading (usually because the iframe hadn't completed loading), so I
decided to write out the correct src the first time. That way, I
don't have to worry about changing it.

Thanks for you help,

Jim

Jul 23 '05 #5
CryingClinton wrote:
<snip>
after that code, run your script to give a src (better be
location.href) for instants:
document.frames('name_of_the_iframe').location.hre f=...;
Global - frames - collections are widely supported on browsers that
understand what a frame is. Referring to a - frames - collection as a
property of the document is not nearly as reliable (and more likely to
encounter cross-domain scripting restrictions).

Treating a - frames - collection as a function and calling it with a
parameter is a Microsoftism that is not widely supported on other
browsers, a bracket notation property accessor works fine on IE browsers
and all other browsers that support the - frames - collection.

Assigning a string value to the - location - property directly (instead
of its - href - property) is slightly more reliable as it also avoids
some cross-domain security restrictions).

The most reliable cross-browser method of navigating a named IFRAME is:-

frames['iframe_name'].location = 'url_string';

(but would be safest done in conjunction with testing to ensure that
the - frames - collection was implemented, that the property accessor
from the named IFRAME returned an object, and that that object
implemented a location object).
This way you will not meet any trouble.


So long as it never sees any browser but IE.

<snip>
I worked some more at the problem, ...

<snip>

Please do not top-post on comp.lang.javascript (see the FAQ).

Richard.
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by HolaGoogle | last post: by
7 posts views Thread by Christopher J. Hahn | last post: by
1 post views Thread by Rhys | last post: by
5 posts views Thread by Grzesiek | last post: by
14 posts views Thread by Aaron Gray | last post: by
8 posts views Thread by Henrik Stidsen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.