JavaScript question - displaying an image name

Results 1 to 2 of 2

Thread: JavaScript question - displaying an image name

  1. #1
    Join Date
    Dec 1969

    Default JavaScript question - displaying an image name

    Hello all. I&#039m a experienced developer with VBScript but an absolute novice with JavaScript. I spent the last couple hours trying to answer my own question but could really use a little help.<BR><BR>On a web page I am using JavaScript to call in a randomly selected image from another page at timed intervals. Each image actually represents a separate advertiser. I can serve the image fine, but aside from showing the image I need to associate a URL with it to handle a click-through. The simplified script is as follows:<BR>----------------<BR>&#060;html&#062;<BR>&#060;head&#062;<BR>&#060; SCRIPT LANGUAGE=JAVASCRIPT&#062;<BR><BR>/* The next line returns the path to an image that is selected at random. It has to be this way because the collection of images to choose from is dynamic. Otherwise, I&#039d hardcode them */<BR><BR>var adImages = new Array("fetch_an_image.asp");<BR><BR>/* I deleted the code here to save on space. It basically takes the image from the array and makes sure a new one is chosen every x seconds. */<BR><BR>function GetURL() {<BR><BR>/*here is where I want to return a valid URL but am having trouble*/<BR><BR> if (adImages[thisAd]= "../images/image1.gif") <BR> {<BR> return "";<BR> }<BR> if (adImages[thisAd] = "../images/image2.gif") <BR> {<BR> return "";<BR> }<BR>}<BR>&#060;/SCRIPT&#062;<BR>&#060;/head&#062;<BR>&#060;BODY onLoad="cycle()"&#062;<BR>&#060;a href="" onClick="this.href=GetURL()" onMouseOver="self.status=&#039Text Here&#039;return true"&#062;<BR>&#060;img src="starter_image.gif" name="adBanner" width=170 height=45&#062;&#060;/a&#062;<BR>&#060;/body&#062;<BR>&#060;/html&#062;<BR>------------<BR>When I try to display the current image using<BR> document.write (adImages[thisAd])<BR>I literally see "fetch_an_image.asp", regardless of the image that gets served. I want it to show "../images/image1.gif" or something. This way my if...then&#039s will work and I can redirect to the appropriate URL.<BR><BR>Could anyone address this?<BR><BR>Thanks<BR>Tim

  2. #2
    Join Date
    Dec 1969

    Default Wrong way to "mix" ASP and generated JS

    Doing this:<BR>&nbsp;&nbsp;&nbsp;&nbsp;var adImages = new Array("fetch_an_image.asp");<BR><BR>simply put the string you noted into the first element of an array! That is *not* what you want, at all!<BR><BR>You need to *cooperate* between the ASP that generates this page and the JavaScript.<BR><BR>You want your ASP page that generates this page to do something like this:<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;var adImages = new Array( &#039&#060;% = Join( theArrayBuiltUpPreviously,"&#039,&#039" ) %&#062;&#039 );<BR><BR>or something along those lines.<BR><BR>And then what you should *probably* do is create a *parallel* array:<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;var adURLs = new Array( &#039&#060;% = Join( anotherArrayBuiltUpPreviously,"&#039,&#039" ) %&#062;&#039 );<BR><BR>So that the URL in adURLs[N] corresponds to the image in adImages[N] (where the N is the same in each case).<BR><BR>Now, as you pick a new image, you simply *remember* the variable N in a global variable, and instead of having to code the functino getURL, you just do<BR>&nbsp;&nbsp;&nbsp;&nbsp;adURLs[sameNumberAsSelectedTheImage]<BR><BR>Anyway, you seem to be naively assuming that just because you put the *name* of an ASP page into your JS code you will somehow pull in what that page generates. Nope. You need to do it the other way around: Have the ASP page generated the JS. Much easier, actually, once you get the concept in your head.<BR><BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts