iframe problem - null object error when changing url

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Marquardson

    #1

    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,frame page) {
    myWindow=window .open(page1,'wi ndow');
    myWindow.frames['winPanel'].location.href= framepage;
    if (window.focus) {myWindow.focus ()}
    }
    </script>

    </head>
    <body>
    <a href="javascrip t:"
    onClick="run('s croll.html','te mp.html');">cli ck4local</a>
    <a href="javascrip t:"
    onClick="run('h ttp://www.mysite.com/page_with_ifram e.htm','http://www.mysite.com/page_to_go_in_i frame.htm;">cli ck4remote</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.ifram es.winPanel.loc ation 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

    #2
    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,frame page) {
    > myWindow=window .open(page1,'wi ndow');
    > 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+"?p ageWithIFrame.h tml",'window')

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

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

    Mick

    [color=blue]
    > </head>
    > <body>
    > <a href="javascrip t:"
    > onClick="run('s croll.html','te mp.html');">cli ck4local</a>
    > <a href="javascrip t:"
    > onClick="run('h ttp://www.mysite.com/page_with_ifram e.htm','http://www.mysite.com/page_to_go_in_i frame.htm;">cli ck4remote</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.ifram es.winPanel.loc ation 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]

    Comment

    • Jim Marquardson

      #3
      Re: iframe problem - null object error when changing url

      > Why not pass the src of the iframe?[color=blue]
      > myWindow=window .open(page1+"?p ageWithIFrame.h tml",'window')
      >
      > Then parse the IFrame src from the location.search property of
      > pageWithIFrame. html
      >
      > if(location.sea rch && isFile(location .search.substri ng(1)){
      > document.frames['winPanel'].src=location.s earch.substring (1);
      > }
      > The "isFile(fil e") 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

      Comment

      • Jim Marquardson

        #4
        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="PageWithF rame.html?nonDe faultContent.ht ml">Link</a>

        Then, the HTML file:

        <html>
        <head>
        <script>
        function writeFrame() {
        page = "defaultContent .htm"; //the default page
        if(location.sea rch /*&& isFile(location .search.substri ng(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>writeFr ame();</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

        Comment

        • CryingClinton

          #5
          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_i frame').locatio n.href=...;

          This way you will not meet any trouble.

          ccton

          --
          www.vicdir.com
          "Jim Marquardson" <jim@marqorp.co m>[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="PageWithF rame.html?nonDe faultContent.ht ml">Link</a>
          >
          > Then, the HTML file:
          >
          > <html>
          > <head>
          > <script>
          > function writeFrame() {
          > page = "defaultContent .htm"; //the default page
          > if(location.sea rch /*&& isFile(location .search.substri ng(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>writeFr ame();</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]


          Comment

          • Richard Cornford

            #6
            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_i frame').locatio n.href=...;[/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.javas cript (see the FAQ).

            Richard.


            Comment

            Working...