Connecting Tech Pros Worldwide Forums | Help | Site Map

iframe problem - null object error when changing url

Jim Marquardson
Guest
 
Posts: n/a
#1: Jul 23 '05
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

Mick White
Guest
 
Posts: n/a
#2: Jul 23 '05

re: iframe problem - null object error when changing url


Jim Marquardson wrote:
[color=blue]
> 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>[/color]

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

[color=blue]
> </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>[/color]

[color=blue]
>
> 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[/color]
Jim Marquardson
Guest
 
Posts: n/a
#3: Jul 23 '05

re: iframe problem - null object error when changing url


> Why not pass the src of the iframe?[color=blue]
> 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[/color]

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
Jim Marquardson
Guest
 
Posts: n/a
#4: Jul 23 '05

re: iframe problem - null object error when changing url


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
CryingClinton
Guest
 
Posts: n/a
#5: Jul 23 '05

re: iframe problem - null object error when changing url


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" <jim@marqorp.com>[color=blue]
> 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[/color]


Richard Cornford
Guest
 
Posts: n/a
#6: Jul 23 '05

re: iframe problem - null object error when changing url


CryingClinton wrote:
<snip>[color=blue]
> 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=...;[/color]

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).
[color=blue]
> This way you will not meet any trouble.[/color]

So long as it never sees any browser but IE.

<snip>[color=blue][color=green]
>> I worked some more at the problem, ...[/color][/color]
<snip>

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

Richard.


Closed Thread