Javascript, Mozzilla, and img.src

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

    Javascript, Mozzilla, and img.src

    Hi, here's my code to basically make an popup window open with an
    image in it and resize to fit it properly. It works in IE, but not in
    Mozilla (no picture shows up and it doesn't resize). It would be
    opened with window.open('pi c.html?'+path,. ..); Any ideas?? Thanks in
    advance!


    <body bgcolor="#00000 0" text="#ffffff" onLoad="loader( )"
    onBlur="self.cl ose()" topmargin=0 leftmargin=0 marginheight=0
    marginwidth=0>

    <script language="JavaS cript"><!--
    function loader() {
    var str = document.locati on.toString();
    var pic = str.substring(s tr.indexOf('?') +1, str.length);

    image.src = pic;
    image.alt = 'Picture: ' + pic;
    }

    function resizer() {
    window.resizeTo (document.all.i mage.width+10,
    document.all.im age.height+30);
    window.focus();
    }
    --></script>

    <img name="image" src="" alt="" onLoad="resizer ()"
    style="display: block">

    </body>
    </html>
  • Vincent van Beveren

    #2
    Re: Javascript, Mozzilla, and img.src

    Hey,
    [color=blue]
    > window.resizeTo (document.all.i mage.width+10,
    > document.all.im age.height+30);
    > window.focus();[/color]

    Don't use the all object, its IE only. Instead, give the picture an ID,
    like ID='image' and use document.getEle mentById('image ') to retrieve the
    object.

    myImage = document.getEle mentById('image ');

    window.resizeTo (myImage.width+ 10, myImage.height+ 30);

    Please note that in IE, if you use ID='somename', then you can not use
    somename as another variable, so

    image = document.getEle mentById('image ');

    might go wrong in IE.

    The rest should pretty much work.

    Good luck

    Comment

    • lion

      #3
      Re: Javascript, Mozzilla, and img.src

      hi, why not use something like this - works for me...

      <script>
      function resizeMe(){
      if (navigator.appN ame == 'Netscape'){
      adjWidth = document.images[0].width + 0;
      adjHeight = document.images[0].height + 30;
      } else {
      adjWidth = document.images[0].width + 0;
      adjHeight = document.images[0].height + 30;
      }

      window.resizeTo (adjWidth, adjHeight);
      window.focus();
      }
      </script>

      Then in the body:
      <body onload="resizeM e();" >

      any good?

      Comment

      • juan zamudio

        #4
        Re: Javascript, Mozzilla, and img.src

        cpwebmaster@wi. rr.com (Robert) wrote in message news:<1ab5bcd8. 0404192225.7fe4 620e@posting.go ogle.com>...[color=blue]
        > Hi, here's my code to basically make an popup window open with an
        > image in it and resize to fit it properly. It works in IE, but not in
        > Mozilla (no picture shows up and it doesn't resize). It would be
        > opened with window.open('pi c.html?'+path,. ..); Any ideas?? Thanks in
        > advance![/color]

        You are using document.all that only works in explorer, you should use
        docuemtn.getEle mentById to access elements.

        Comment

        • Mark Preston

          #5
          Re: Javascript, Mozzilla, and img.src

          Robert wrote:
          [color=blue]
          > Hi, here's my code to basically make an popup window open with an
          > image in it and resize to fit it properly. It works in IE, but not in
          > Mozilla (no picture shows up and it doesn't resize).[snip]
          >[/color]
          You are using "document.a ll" which is (basically) for IE only. Switch it
          to use "document.getEl ementById ("imageid")" and give the image an ID.

          By the way - it still may not work, since Mozilla and Firefox can (and
          usually do) turn pop-up windows off altogether.

          Comment

          • Michael Winter

            #6
            Re: Javascript, Mozzilla, and img.src

            On Tue, 20 Apr 2004 09:45:55 +0200, Vincent van Beveren
            <vincent@provid ent.remove.this .nl> wrote:
            [color=blue]
            > Hey,
            >[color=green]
            > > window.resizeTo (document.all.i mage.width+10,
            > > document.all.im age.height+30);
            > > window.focus();[/color]
            >
            > Don't use the all object, its IE only. Instead, give the picture an ID,
            > like ID='image' and use document.getEle mentById('image ') to retrieve the
            > object.
            >
            > myImage = document.getEle mentById('image ');[/color]

            The simpler approach is to use the document.images collection, which is
            supported by the W3C DOM and Netscape browsers as old as v3.0. In general,
            you would replace the line above with

            myImage = document.images[ 'image' ];

            If you need to support NN4, specify both a name and id attribute. However,
            as there is only one image in this document, document.images[ 0 ] will
            suffice.
            [color=blue]
            > window.resizeTo (myImage.width+ 10, myImage.height+ 30);[/color]

            The OP should be wary of relying on the ability to resize windows. Decent
            browsers will allow the user to disable that functionality to prevent
            author abuse.
            [color=blue]
            > Please note that in IE, if you use ID='somename', then you can not use
            > somename as another variable, so
            >
            > image = document.getEle mentById('image ');
            >
            > might go wrong in IE.[/color]

            I don't think that's correct, but my experiment is based on only one
            browser version so I could be wrong myself. I'll take your example of an
            IMG element with the id, "image".

            Under IE's (rather flawed) global identifier system, using "image" as an
            object reference will allow you to directly modify the properties of that
            IMG element using the identifier in any scope. As the reference represents
            document content, IE won't let you overwrite the value. For example,

            image = new Image();

            will fail with an error. However, if you declare the variable, it is
            created anew and there is no further link between the identifier and the
            object it originally represented in that scope. So, using

            var image;

            in global scope will mean that "image" no longer refers to the IMG element
            in any scope. In addition, consider

            function a() {
            var image;

            alert( image );
            b();
            }
            function b() {
            alert( image );
            }

            When function a() is called, it will display "undefined" in the alert box.
            That is the value of the local variable, image. When a() calls b(),
            "[object]" will be displayed as the variable, image, uses the unaffected
            global value.

            It would be nice if someone who thoroughly understands IE's idiosyncrasies
            could confirm this as behaviour across all IE versions, rather than in
            just my copy of IE 6.

            Mike

            --
            Michael Winter
            M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

            Comment

            • Michael Winter

              #7
              Re: Javascript, Mozzilla, and img.src

              On 20 Apr 2004 02:10:56 -0700, lion <adamslionel@ho tmail.com> wrote:
              [color=blue]
              > hi, why not use something like this - works for me...
              >
              > <script>[/color]

              <script type="text/javascript">

              The type attribute is required.
              [color=blue]
              > function resizeMe(){
              > if (navigator.appN ame == 'Netscape') {[/color]

              Don't use browser detection. It is a flawed technique. Read

              <URL:http://jibbering.com/faq/#FAQ4_26>

              and its links.
              [color=blue]
              > adjWidth = document.images[0].width + 0;
              > adjHeight = document.images[0].height + 30;
              > } else {
              > adjWidth = document.images[0].width + 0;
              > adjHeight = document.images[0].height + 30;
              > }[/color]

              Why are both of those branches exactly the same?

              [snip]

              Mike


              Please format code when posting it. Clear formatting makes code much
              easier to read and understand. Finally, don't use tabs: they won't produce
              a consistent layout in all newsreaders. Some might remove them, others may
              use large spaces that makes code wrap prematurely.

              --
              Michael Winter
              M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

              Comment

              • Robert

                #8
                Re: Javascript, Mozzilla, and img.src

                Thanks for all the quick responeses. A combination of the two
                solutions fixed it. Thanks again!

                Comment

                • Vincent van Beveren

                  #9
                  Re: Javascript, Mozzilla, and img.src

                  [color=blue][color=green]
                  >> myImage = document.getEle mentById('image ');[/color]
                  >
                  > myImage = document.images[ 'image' ];
                  >[/color]

                  Yes, Images is supported, but I wouldn't use it. I guess its a matter of
                  style. Besides who still uses NN3 or 4? and if they do, they should
                  consider downloading a more up-to-date site. I'm sure many sites
                  thesedays would not work anymore with NN4 or 3
                  [color=blue][color=green]
                  >> Please note that in IE, if you use ID='somename', then you can not use
                  >> somename as another variable, so
                  >>
                  >> image = document.getEle mentById('image ');
                  >>
                  >> might go wrong in IE.[/color]
                  >
                  > It would be nice if someone who thoroughly understands IE's
                  > idiosyncrasies could confirm this as behaviour across all IE versions,
                  > rather than in just my copy of IE 6.
                  >[/color]

                  Well, that might be true, but its not like that in Mozilla like
                  browsers, or Safari... So, the only reason to use var is for
                  InternetExplore r... therefor I just try to avoid it all-together.


                  Comment

                  • Richard Cornford

                    #10
                    Re: Javascript, Mozzilla, and img.src

                    Vincent van Beveren wrote:[color=blue][color=green][color=darkred]
                    >>> myImage = document.getEle mentById('image ');[/color]
                    >>
                    >> myImage = document.images[ 'image' ];
                    >>[/color]
                    >
                    > Yes, Images is supported, but I wouldn't use it. I guess its a matter
                    > of style. Besides who still uses NN3 or 4? and if they do, they should
                    > consider downloading a more up-to-date site. I'm sure many sites
                    > thesedays would not work anymore with NN4 or 3[/color]
                    <snip>

                    It isn't just NN4 here, and some of those less capable browsers that
                    don't do W3C DOM but understand - document.images - are embedded
                    browsers that are not to easy to update, and may even be the latest
                    available on their platform. Given two methods that produce the same
                    results on the same browsers when they work, but one is known to also
                    work on many other browsers, cross-browser coding should favour the
                    wider supported.

                    If you need some additional reason for using a W3C HTML DOM specified
                    convenience properties when scripting an HTML document, beyond being a
                    standard method and support for older browsers, how about execution
                    speed. Potentially - getElementById will search the entire DOM looking
                    for a corresponding element (worst case, no optimisation), while the
                    images collection is unlikely to take anything like as long to search
                    for a named property.

                    Richard.


                    Comment

                    • Michael Winter

                      #11
                      Re: Javascript, Mozzilla, and img.src

                      On Wed, 21 Apr 2004 09:09:19 +0200, Vincent van Beveren
                      <vincent@provid ent.remove.this .nl> wrote:
                      [color=blue][color=green][color=darkred]
                      >>> myImage = document.getEle mentById('image ');[/color]
                      >>
                      >> myImage = document.images[ 'image' ];[/color]
                      >
                      > Yes, Images is supported, but I wouldn't use it.[/color]

                      Any particularly good reason?
                      [color=blue]
                      > I guess its a matter of style. Besides who still uses NN3 or 4?[/color]

                      Hopefully, no-one. However, this isn't the case.
                      [color=blue]
                      > and if they do, they should consider downloading a more up-to-date site.[/color]

                      Examples have been cited in this group of libraries, and similar public
                      places, that still use old browser versions. Some don't have the
                      opportunity to change.
                      [color=blue]
                      > I'm sure many sites thesedays would not work anymore with NN4 or 3[/color]

                      Quite true. In any case, this isn't just about old browser support. The
                      collection will be supported in many browsers. As it is included in the
                      DOM HTML specifications, the range of browsers will also include modern
                      DOM-compliant browsers.

                      Furthermore, as Richard suggested, the collection is likely to be faster.
                      The document.getEle mentById() method has no way of knowing where it will
                      find a match. If it has to negotiate the entire document tree (as a tree),
                      it could take a significant amount of time, depending on the location of
                      the node. If the a list of ids is contained more conveniently, in an array
                      perhaps, it will obviously proceed quicker. However, if you add in the
                      fact that IE (wrongly) includes named elements in its getElementById( )
                      search, that array will certainly be larger than one which is limited
                      purely to images.
                      [color=blue][color=green][color=darkred]
                      >>> Please note that in IE, if you use ID='somename', then you can not use
                      >>> somename as another variable, so
                      >>>
                      >>> image = document.getEle mentById('image ');
                      >>>
                      >>> might go wrong in IE.[/color]
                      >>
                      >> It would be nice if someone who thoroughly understands IE's
                      >> idiosyncrasies could confirm this as behaviour across all IE versions,
                      >> rather than in just my copy of IE 6.[/color]
                      >
                      > Well, that might be true, but its not like that in Mozilla like
                      > browsers, or Safari...[/color]

                      Of course not. Mozilla-based browsers (I can't comment on Safari) don't
                      implement IE's global identifier scheme (a good thing in my opinion).
                      [color=blue]
                      > So, the only reason to use var is for InternetExplore r...[/color]

                      No. The reason to use var is to control the scope of variables. As the
                      code is likely to be executed in a function, you should be using var to
                      declare the variable in local scope. You're indulging in bad practice if
                      you don't.
                      [color=blue]
                      > therefor I just try to avoid it all-together.[/color]

                      If using a particular identifier makes your code easier to understand, for
                      yourself and others, use it. You shouldn't allow some ridiculous scheme to
                      dictate how you'd prefer to write your code, especially if using var to
                      declare your variables (which I always use, for globals and locals) avoids
                      the issue.

                      Mike

                      --
                      Michael Winter
                      M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

                      Comment

                      • Jim Ley

                        #12
                        Re: Javascript, Mozzilla, and img.src

                        On Tue, 20 Apr 2004 16:01:03 +0100, Mark Preston
                        <usenet@mpresto n.demon.co.uk> wrote:
                        [color=blue]
                        >Robert wrote:
                        >[color=green]
                        >> Hi, here's my code to basically make an popup window open with an
                        >> image in it and resize to fit it properly. It works in IE, but not in
                        >> Mozilla (no picture shows up and it doesn't resize).[snip]
                        > >[/color]
                        >You are using "document.a ll" which is (basically) for IE only. Switch it
                        >to use "document.getEl ementById ("imageid")" and give the image an ID.[/color]

                        but .all is supported by _more_ types of browsers than gEBI, gEBI's
                        better for many reasons, perhaps most importantly moz's lack of
                        support for it and its slowness, but it's unfair to say that it's
                        bascially IE only.

                        Jim.
                        --
                        comp.lang.javas cript FAQ - http://jibbering.com/faq/

                        Comment

                        • Mark Preston

                          #13
                          Re: Javascript, Mozzilla, and img.src

                          Jim Ley wrote:
                          [color=blue]
                          > On Tue, 20 Apr 2004 16:01:03 +0100, Mark Preston
                          > <usenet@mpresto n.demon.co.uk> wrote:
                          >[color=green]
                          >>Robert wrote:
                          >>[color=darkred]
                          >>>Hi, here's my code to basically make an popup window open with an
                          >>>image in it and resize to fit it properly. It works in IE, but not in
                          >>>Mozilla (no picture shows up and it doesn't resize).[snip]
                          >>>[/color]
                          >>You are using "document.a ll" which is (basically) for IE only. Switch it
                          >>to use "document.getEl ementById ("imageid")" and give the image an ID.[/color]
                          >
                          > but .all is supported by _more_ types of browsers than gEBI, gEBI's
                          > better for many reasons, perhaps most importantly moz's lack of
                          > support for it and its slowness, but it's unfair to say that it's
                          > bascially IE only.
                          >[/color]
                          Jim, I do accept that a lot - indeed most, as you say, - browsers can
                          and do "spoof" so that they either use or recognise MSIE extensions (or,
                          in fact, actually even identify themselves as MSIE whatever they might
                          actually be). But to describe all of that is both awkward and
                          uneccassary for most users - it is far easier to say "the standard
                          method is _this_ but Microsoft chose to use _this_" and to leave the
                          choice of what to use to the writer.

                          Like it or not, the fact is that the MSIE extensions simply will not
                          work on some browsers while the real "standards" will work on pretty
                          much all the browsers _including_ MSIE. It is far easier to "keep it
                          simple" for questions like that.

                          Comment

                          Working...