Wednesday, February 15, 2006

Some Javascript today....

Vijay wanted a function which will display an image whose number he will pass, for eg if the name of the image is 1.gif he will pass "1" to it.
At first we had trouble of "unterminated string constant" error, later found out that since we had only one slash ("\") in the images (since the image was inside images folder), and javascript has "\" as escape character. So we need to place two "back slashes" and we got it right.
function change()
{
var img;
img = 1;
var path; path="images\\" + String(img)+ ".gif";
document.Form1.img1.src = path;
alert(path);
}
This is a function prototype which I made, parameters need to be added to it.

After making all this and giving Vijay the code, I thought of the great blunder made by me.For the virtual path of the image I was using "\"--> the network slash where as it should be "/". Randeep had told me the same more than once and I am making the same mistake.
So the function will become something like this
function change()
{
var img; img = 1;
var path; path="images/" + String(img)+ ".gif";
document.Form1.img1.src = path;
alert(path);
}

Nice to see that I am repeating the mistakes :-(

No comments: