Loading images for use on client

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

    Loading images for use on client

    Hi all,

    I'm having a problem loading images for later use in javascript on the
    client. In my code-behind, I'm using a stringbuilder object to build and
    populate a few javascript variables like : js.append("var myjsvariable = New
    Image")

    js.append("myjs variable.src = " & "http://localhost/images" & "myimage.jp g")

    In one of my javascript functions, I reference this image and it is null.


    What am I doing wrong here?

    Regards
    John.


  • S. Justin Gengo

    #2
    Re: Loading images for use on client

    John,

    Are you then placing the string you've built in the client side code?

    I don't know which object you're trying to attach the javascript to, but it
    sounds like you are building the string on the server side but never placing
    the built string out on the client.

    For example if I want to add an attribute to a textbox I could build the
    string, but I then have to add the attribute to the client side code:

    Dim CodeBuilt As New System.Text.Str ingBuilder
    CodeBuilt.Appen d("MyStringHere ")

    TextBox1.Attrib utes.Add("onCli ck", "javascript :" & CodeBuilt & ";")

    I'm assuming you will need to add your images to a script block so you might
    want to look into the Page.RegisterCl ientScriptBlock method.

    (Don't see RegisterClientS criptBlock in your intellisense? In .Net advanced
    members are hidden by default. To make them visible: Click Tools-Options,
    hen Select the "Text Editor" folder, then select the "All Languages" folder
    and you will see a grayed out check box "Hide advanced members" Click the
    checkbox to clear it. Alternatively you can select each language's specific
    folder and turn off the Hide advanced members feature per language.)

    I hope this helps.

    Justin




    "John" <a@b.com> wrote in message
    news:eEhcbpsSDH A.2252@TK2MSFTN GP12.phx.gbl...[color=blue]
    > Hi all,
    >
    > I'm having a problem loading images for later use in javascript on the
    > client. In my code-behind, I'm using a stringbuilder object to build and
    > populate a few javascript variables like : js.append("var myjsvariable =[/color]
    New[color=blue]
    > Image")
    >
    > js.append("myjs variable.src = " & "http://localhost/images" &[/color]
    "myimage.jp g")[color=blue]
    >
    > In one of my javascript functions, I reference this image and it is null.
    >
    >
    > What am I doing wrong here?
    >
    > Regards
    > John.
    >
    >[/color]


    Comment

    • Vidar Petursson

      #3
      Re: Loading images for use on client

      Hi

      Basic Preloader:

      <script>
      iPreload(// Add images to preload here
      "myImg1.gif ",
      "myImg2.gif "
      );

      var imgArr = new Array();
      function iPreload(){
      var p = "IMAGEPATH/";
      if( document.images )
      {
      for( i = 0; i < iPreload.argume nts.length; i++ )
      {
      imgArr[ i ] = new Image();
      imgArr[ i ].src = p + iPreload.argume nts[i];
      }
      }
      }
      </script>

      So if you want to set the src of a image to myImg1.gif
      document.images["IMAGENAME"].src = imgArr[0];
      document.images["IMAGENAME"].src = imgArr[1]; // myImg2.gif here

      --
      Best Regards
      Vidar Petursson
      =============== ===============
      Microsoft Internet Client & Controls MVP
      =============== ===============
      "John" <a@b.com> wrote in message
      news:eEhcbpsSDH A.2252@TK2MSFTN GP12.phx.gbl...[color=blue]
      > Hi all,
      >
      > I'm having a problem loading images for later use in javascript on the
      > client. In my code-behind, I'm using a stringbuilder object to build and
      > populate a few javascript variables like : js.append("var myjsvariable =[/color]
      New[color=blue]
      > Image")
      >
      > js.append("myjs variable.src = " & "http://localhost/images" &[/color]
      "myimage.jp g")[color=blue]
      >
      > In one of my javascript functions, I reference this image and it is null.
      >
      >
      > What am I doing wrong here?
      >
      > Regards
      > John.
      >
      >[/color]


      Comment

      Working...