PDA

View Full Version : ASP Variable used for JavaScript image path?


drej
10-08-2002, 04:43 PM
I am trying to pass an ASP variable into an external JavaScript and use the ASP variable as the image path for a rollover image.

The script works when I use the image path (without the variable). The version below returns a broken image link when I rollover the first image. The variable value is the correct path to the image, but it's not working in the JavaScript.

If I take the quotes out I get all kind of object errors, etc.

I'm trying to do this so that I can manage the button image changes (active link, current section, etc.) from one ASP include file.

Thanks for any ideas.

<!--

//main navigation

if (document.images) { // Active Images
img1on = new Image();
img1on.src="<%=img1varon%>";
//Services
img2on = new Image();
img2on.src="http://www.liqmedia.com/futuretech/site/global_images/services_over.jpg";
//Clients
img3on = new Image();
img3on.src="http://www.liqmedia.com/futuretech/site/global_images/clients_over.jpg";
//News & Events
img4on = new Image();
img4on.src="http://www.liqmedia.com/futuretech/site/global_images/newsevents_over.jpg";
//Resources
img5on = new Image();
img5on.src="http://www.liqmedia.com/futuretech/site/global_images/resources_over.jpg";
//Careers
img6on = new Image();
img6on.src="http://www.liqmedia.com/futuretech/site/global_images/careers_over.jpg";
//Site Map
img7on = new Image();
img7on.src="http://www.liqmedia.com/futuretech/site/global_images/sitemap_over.jpg";
//Contact Us
img8on = new Image();
img8on.src="http://www.liqmedia.com/futuretech/site/global_images/contactus_over.jpg";
//Employee Communication
img9on = new Image();
img9on.src="http://www.liqmedia.com/futuretech/site/global_images/empcomm_over.jpg";
//Custom Training & Instructional Design
img10on = new Image();
img10on.src="http://www.liqmedia.com/futuretech/site/global_images/custrain_over.jpg";
//Communication Staffing & Recruiting
img11on = new Image();
img11on.src="http://www.liqmedia.com/futuretech/site/global_images/commstaff_over.jpg";
//Communication Workshops
img12on = new Image();
img12on.src="http://www.liqmedia.com/futuretech/site/global_images/commwork_over.jpg";

//About Us
img1off = new Image();
img1off.src="http://www.liqmedia.com/futuretech/site/global_images/aboutus_reg.jpg";
//Services
img2off = new Image();
img2off.src="http://www.liqmedia.com/futuretech/site/global_images/services_reg.jpg";
//Clients
img3off = new Image();
img3off.src="http://www.liqmedia.com/futuretech/site/global_images/clients_reg.jpg";
//News & Events
img4off = new Image();
img4off.src="http://www.liqmedia.com/futuretech/site/global_images/newsevents_reg.jpg";
//Resources
img5off = new Image();
img5off.src="http://www.liqmedia.com/futuretech/site/global_images/resources_reg.jpg";
//Careers
img6off = new Image();
img6off.src="http://www.liqmedia.com/futuretech/site/global_images/careers_reg.jpg";
//Site Map
img7off = new Image();
img7off.src="http://www.liqmedia.com/futuretech/site/global_images/sitemap_reg.jpg";
//Contact Us
img8off = new Image();
img8off.src="http://www.liqmedia.com/futuretech/site/global_images/contactus_reg.jpg";
//Employee Communication
img9off = new Image();
img9off.src="http://www.liqmedia.com/futuretech/site/global_images/empcomm_reg.jpg";
//Custom Training & Instructional Design
img10off = new Image();
img10off.src="http://www.liqmedia.com/futuretech/site/global_images/custrain_reg.jpg";
//Communication Staffing & Recruiting
img11off = new Image();
img11off.src="http://www.liqmedia.com/futuretech/site/global_images/commstaff_reg.jpg";
//Communication Workshops
img12off = new Image();
img12off.src="http://www.liqmedia.com/futuretech/site/global_images/commwork_reg.jpg";

}

// Function to 'activate' images.
function imgOn(imgName) {
if (document.images) {
document[imgName].src = eval(imgName + "on.src");
}
}

// Function to 'deactivate' images.
function imgOff(imgName) {
if (document.images) {
document[imgName].src = eval(imgName + "off.src");
}
}

//-->

Roy Sinclair
10-08-2002, 07:08 PM
If it's broken when viewed in a browser try doing a "View Source" at the browser to see exactly what the generated html looks like and comapre that to your "hard coded" version. That should help you find the problem.

drej
10-08-2002, 07:28 PM
When I view the source, I cannot see the JavaScript because it is external.

I'm using a variable (in an ASP include file) to display the initial image and that is displaying fine. The HTML source indicates the correct path for that image.

Any ideas? Thanks.

Roy Sinclair
10-08-2002, 07:52 PM
Putting the code into an external javascript file is the problem then, unless that external file has a .asp extension and the appropriate code to set up the variables you're trying to create all you're getting at the browser is the unprocessed ASP code instead of the actual file names you're expecting. Remember that the ASP processing is done on files named ASP.

Fortunately there's a simple solution available, instead of:


<script type="text/javascript src="myExternalScript.js"></script>


use


<script type="text/jajascript">
<!--#include file="myExternalScript.js"-->
</script>


The latter style declaration uses a SSI (Server Side Include) call to include the external script directly while the ASP page is being processed and the ASP engine will also process the .JS file for you replacing the ASP variables properly.

drej
10-08-2002, 08:45 PM
That produced object errors on the page. But, the external JS file works fine (accept for the variable). When I bring the external JS into the page the variable doesn't work either.

I figured it might be the processing order of the files in the page:

Old:

<script language="JavaScript" src="lib/roll.js"></script>
<!--#include file="includes/header.asp" --> - (where the variables are declared)

New:

<!--#include file="includes/rollvars.asp" --> - (where the variables are declared)
<script language="JavaScript" src="lib/roll.js"></script>
<!--#include file="includes/header.asp" -->

But, that doesn't work either.

Roy Sinclair
10-08-2002, 10:18 PM
The include which brings in the javascript needs to go between the <script> and the </script> tags. What you posted shows it being included outside those tags.

boywonder
10-09-2002, 02:55 AM
drej you would need to do what Roy said originally and use asp to include your javascript, between script tags.

This would print the script in your page (source code) to the browser, but the ASP would be inserted first.

When you have an external javascript in your page, the server will just pass that <script... code to the browser which can't read ASP. so your <%=stuff%> would just be interpreted literally by the browser as the string "<%=stuff%>".

Make sense? Post back if not.

glenngv
10-09-2002, 03:35 AM
another solution is, if you want to keep the external js.

in your external js:

img1on = new Image();
img1on.src=gImage;


in your asp page:

<script language="javascript">
var gImage = "<%=img1varon%>"; //global
</script>
<script language="javascript" src="external.js"></script>

declare the global var before the external js

whammy
10-09-2002, 04:02 AM
I see no reason why glenngv's solution wouldn't work perfectly... :D

Roy Sinclair
10-09-2002, 05:03 PM
Glenn's solution is also workable but I think mine would be better for a simple reason.

When a user requests a web page the source for that page is processed (in the case of server side scripting like ASP) and then sent to the user's browser. The user's browser then looks to see what additional files are called for by the initial page and then sends requests for those files to the server. By using the SSI trick I mentioned above at least one file request isn't needed because the file would have been added to the inital page and sent in the first place. Even with slow clients I doubt the users would see a difference in performance but when you serve the same page thousands of times your server might notice the difference.

glenngv
10-10-2002, 02:02 AM
"For those people who wish they had SSI available from their host, using an external javascript could be a very good alternative for them as it creates the same effect.
" -- http://www.htmlite.com/JS021.shtml

...and loading external js for the 2nd or more time will load faster since the http header will return 304 code (not modified)