Showwing an image from a variable?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ross M. Greenberg

    Showwing an image from a variable?

    If I have a variable named 'fred', the contents looking like this:
    '/gif/picture1.gif', how can I display that image?

    Thanks!

    Ross

  • Lasse Reichstein Nielsen

    #2
    Re: Showwing an image from a variable?

    "Ross M. Greenberg" <greenber@catsk ill.net> writes:
    [color=blue]
    > If I have a variable named 'fred', the contents looking like this:
    > '/gif/picture1.gif', how can I display that image?[/color]

    Where?

    You can do this:
    ---
    var img = new Image();
    img.src=fred;
    document.body.a ppendChild(img) ;
    ---
    but that displays the image at the end of the document.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Richard Cornford

      #3
      Re: Showwing an image from a variable?

      "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
      news:3cci5uf3.f sf@hotpop.com.. .
      <snip>[color=blue]
      > You can do this:
      > ---
      > var img = new Image();
      > img.src=fred;
      > document.body.a ppendChild(img) ;
      > ---
      > but that displays the image at the end of the document.[/color]

      On some of the more recent browsers new Image() is very like
      document.create Element('img') and does return an object with all of the
      characteristics of an IMG element. But they are not required to be
      equivalent, and I don't think I would recommend new Image() over
      createElement (if available, so probably whenever appendChild is
      available (except for possible problems with IE 4 and late Opera 6
      versions [1])). On IceBrowser, for example, creating and appending an
      IMG element will work but the global Image constructor is a functionless
      dummy.

      Richard.

      [1] For reasons that have never become clear Opera 6 (at least the later
      versions) has a document.create Element function, but as it is not
      possible to dynamically alter the DOM in the browser on Opera 6 there
      doesn't seem much point in having the function (and I don't think that
      it is functional). IE 4 has both document.create Element and appendChild
      methods on its elements but they are pre-W3C DOM methods and cannot be
      used in the same way as the W3C versions.


      Comment

      • Ross M. Greenberg

        #4
        Re: Showwing an image from a variable?

        The variable "fred" is defined in the HEAD, and as such is static for the life
        of the rendering.

        I want a tag such as <IMG src=fred...>, but I simply don't know the proper
        syntax for such.

        Newbie question I know, but then I are one...

        Thanks!

        Ross
        "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
        news:3cci5uf3.f sf@hotpop.com.. .[color=blue]
        > "Ross M. Greenberg" <greenber@catsk ill.net> writes:
        >[color=green]
        > > If I have a variable named 'fred', the contents looking like this:
        > > '/gif/picture1.gif', how can I display that image?[/color]
        >
        > Where?
        >
        > You can do this:
        > ---
        > var img = new Image();
        > img.src=fred;
        > document.body.a ppendChild(img) ;
        > ---
        > but that displays the image at the end of the document.
        >
        > /L
        > --
        > Lasse Reichstein Nielsen - lrn@hotpop.com
        > DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        > 'Faith without judgement merely degrades the spirit divine.'[/color]

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: Showwing an image from a variable?

          "Ross M. Greenberg" <greenber@catsk ill.net> writes:

          Please don't top-post.
          [color=blue]
          > The variable "fred" is defined in the HEAD, and as such is static
          > for the life of the rendering.
          >
          > I want a tag such as <IMG src=fred...>, but I simply don't know the proper
          > syntax for such.[/color]

          Where do you want it?

          What you can do is to add
          <script type="text/javascript">
          document.write( "<img src=\""+fred+"\ " ... >");
          </script>

          That will add an image tag where the script tag is.

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Grant Wagner

            #6
            Re: Showwing an image from a variable?

            <head>
            <script type="javascrip t">
            var fred = 'path/to/image.jpg';
            </script>
            </head>
            <body>
            <script type="text/javascript">
            document.write( '<img src="' + fred + '" />');
            </script>
            </body>

            Of course if the user doesn't have client-side JavaScript enabled, they get
            nothing. I better alternative might be:

            <head>
            <script type="javascrip t">
            var fred = 'path/to/image.jpg';
            </script>
            </head>
            <body onload="
            if (document.image s && document.images['myImage']) {
            document.images['myImage'].src = fred;
            }
            ">
            <img name="myImage" src="/path/to/dummyImage.jpg" />
            </body>


            "Ross M. Greenberg" wrote:
            [color=blue]
            > The variable "fred" is defined in the HEAD, and as such is static for the life
            > of the rendering.
            >
            > I want a tag such as <IMG src=fred...>, but I simply don't know the proper
            > syntax for such.
            >
            > Newbie question I know, but then I are one...
            >
            > Thanks!
            >
            > Ross
            > "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
            > news:3cci5uf3.f sf@hotpop.com.. .[color=green]
            > > "Ross M. Greenberg" <greenber@catsk ill.net> writes:
            > >[color=darkred]
            > > > If I have a variable named 'fred', the contents looking like this:
            > > > '/gif/picture1.gif', how can I display that image?[/color]
            > >
            > > Where?
            > >
            > > You can do this:
            > > ---
            > > var img = new Image();
            > > img.src=fred;
            > > document.body.a ppendChild(img) ;
            > > ---
            > > but that displays the image at the end of the document.
            > >
            > > /L
            > > --
            > > Lasse Reichstein Nielsen - lrn@hotpop.com
            > > DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
            > > 'Faith without judgement merely degrades the spirit divine.'[/color][/color]

            --
            | Grant Wagner <gwagner@agrico reunited.com>

            * Client-side Javascript and Netscape 4 DOM Reference available at:
            *


            * Internet Explorer DOM Reference available at:
            *
            Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


            * Netscape 6/7 DOM Reference available at:
            * http://www.mozilla.org/docs/dom/domref/
            * Tips for upgrading JavaScript for Netscape 7 / Mozilla
            * http://www.mozilla.org/docs/web-deve...upgrade_2.html


            Comment

            • Fabian

              #7
              Re: Showwing an image from a variable?

              Lasse Reichstein Nielsen hu kiteb:
              [color=blue]
              > "Ross M. Greenberg" <greenber@catsk ill.net> writes:
              >
              > Please don't top-post.
              >[color=green]
              >> The variable "fred" is defined in the HEAD, and as such is static
              >> for the life of the rendering.
              >>
              >> I want a tag such as <IMG src=fred...>, but I simply don't know the
              >> proper syntax for such.[/color]
              >
              > Where do you want it?
              >
              > What you can do is to add
              > <script type="text/javascript">
              > document.write( "<img src=\""+fred+"\ " ... >");
              > </script>
              >
              > That will add an image tag where the script tag is.[/color]

              Yet another methodf is:

              document.getEle mentById(id).sr c = fred;

              Where you have an ID attribute on the image.

              --
              --
              Fabian
              Visit my website often and for long periods!
              AGAM69 menghadirkan inspirasi desain kreatif, solusi digital, pengembangan teknologi, serta inovasi modern untuk kebutuhan bisnis dan profesional.



              Comment

              Working...