Loading images for use on client

  • Thread starter Thread starter John
  • Start date Start date
J

John

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("myjsvariable.src = " & "http://localhost/images" & "myimage.jpg")

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


What am I doing wrong here?

Regards
John.
 
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.StringBuilder
CodeBuilt.Append("MyStringHere")

TextBox1.Attributes.Add("onClick", "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.RegisterClientScriptBlock method.

(Don't see RegisterClientScriptBlock 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
 
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.arguments.length; i++ )
{
imgArr[ i ] = new Image();
imgArr[ i ].src = p + iPreload.arguments;
}
}
}
</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
==============================
 
Back
Top