PDA

View Full Version : keep getting an undefined error


habib
10-07-2002, 11:35 AM
Hi,

I am trying to change an image depending on the cookie value of a user. The image is of a table which contains cost amounts in various currencies.

Below is my function which is stored in an external.js file (image.js)

function change_image() {
var name='home_location';
var picture="";
var cookieVal=getCookie(name);
if (getCookie('home_location') == 'EU') {
picture="<img src=images/costs-eu.jpg>"; }
else
if (getCookie('home_location') == 'SA') {
picture="<img src=images/costs-sa.jpg>"; }
else
if (getCookie('home_location') == 'UK') {
picture="<img src=images/costs-uk.jpg>"; }
else
if (getCookie('home_location') == 'US') {
picture="<img src=images/costs-us.jpg>";
}
}

I then call this function in my htm file

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

<body onLoad="change_image();">
<script>document.write(picture);</script>
</body>


But I keep getting "picture" undefined errors
Can't understand why, can anyone help?

Thanks
HR

glenngv
10-07-2002, 11:54 AM
<body onLoad="change_image();">
<script>document.write(picture);</script>
</body>

the document.write will execute first than the onload statement and picture variable is not global.

change it to:


<body>
<script>
function change_image() {
var name='home_location';
var picture="";
var cookieVal=getCookie(name);
if (getCookie('home_location') == 'EU') {
picture="<img src=images/costs-eu.jpg>"; }
else
if (getCookie('home_location') == 'SA') {
picture="<img src=images/costs-sa.jpg>"; }
else
if (getCookie('home_location') == 'UK') {
picture="<img src=images/costs-uk.jpg>"; }
else
if (getCookie('home_location') == 'US') {
picture="<img src=images/costs-us.jpg>";
}
return picture;
}

document.write(change_image());
</script>
</body>

habib
10-07-2002, 12:33 PM
Hey, it Worked!

Thanks Glen

:thumbsup: