Click to See Complete Forum and Search --> : I know this is stupid coming from me...
But I never learned how the "function do(something)" worked. In rollover functions there's code like this...
function rollOver(imgName) {
//rollover function
}
and the image...
<img src='img.jpg' onmouseover="rollOver('img1')">
What I don't understand is how "imgName" is passed through the function inside of the parenthesis. There are other functions like splitUp(str, obj, by). How does that work? I honestly never learned how there is this "something" that is sometimes needed in functions and put inside of the parenthesis on the function. I never learned what that was for or how to use it. If anyone can help, I'd appreciate it! Thanks.
khalidali63
03-02-2003, 04:47 PM
Here is an ok tutorial on functions
http://www.htmlgoodies.com/primers/jsp/hgjsp_9.html
Cheers
Khalid
Nedals
03-02-2003, 05:09 PM
khalidali63...
I took a quick look at that tutorial and it didn't seem to explain passing variables which is what jona needed.
jona...
It is possible to pass information from HTML to a javascript function...
function rollOver(imgName) {
// imgName is a variable and it's value, 'img1.jpg', is set in the <img> tag below
document.images[0].src = imgName;
//which is atually... document.images[0].src = 'img1.jpg';
}
....
<img src='img.jpg' onmouseover="rollOver('img1.jpg')">
If you want to pass more than 1 variable you can use something like this.. splitUp(str, obj, by) ...to pass 3 variables; str, obj, and by.
Hope that helps. :)
khaki
03-02-2003, 06:11 PM
Hi Jona -
This was one of the most confusing concepts that I ever confronted.
Therefore, I don't think that it can be explained enough.
So here's another approach to add to the others:
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE="JavaScript">
function rollOver2(a)
{
document.write('<img src=' + a + '><br>Isnt that precious? (wink)')
}
</SCRIPT>
</HEAD>
<BODY>
<img src='http://forums.webdeveloper.com/img/webdevlogoz.gif'
onmouseover="rollOver2('http://forums.webdeveloper.com/avatar.php?userid=2767&dateline=1046023092')">
</BODY>
</HTML>
The function will take the value that was passed from
the parenthesis in the onmouseover and it will assign it
it's own vaiable name (in this case "a").
And every place in the function that uses "a" as a variable,
will be the same value that was passed from the onmouseover.
"a" will simply equal whatever was passed to the function.
And in this case, "a" is a lot easier for the function to
manage than the very long url string that was passed to the
function by the onmouseover.
...And to pass 2 variables
(in this example, the very long url string is spilt and seperated by
a comma to turn it into 2 variables)
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE="JavaScript">
function rollOver2(a,b)
{
document.write('<img src=' + a + b +'><br>Well I think that i'ts precious? (wink)')
}
</SCRIPT>
</HEAD>
<BODY>
<img src='http://forums.webdeveloper.com/img/webdevlogoz.gif'
onmouseover="rollOver2('http://forums.webdeveloper.com/avatar.php?',
'userid=2767&dateline=1046023092')">
</BODY>
</HTML>
Both of these scripts work y'know, so try them out (and see what makes them so great!).
wink
k
khalidali63
03-02-2003, 11:35 PM
Originally posted by nedals
........
I took a quick look at that tutorial and it didn't seem to explain passing variables which is what jona needed.........
Thanks for pointing it out,Though my soul purpose of putting that link here was to point the member in a direction from where he/she could go further and search on the topic.
Passing paremeters( by reference or by value) to functions is not a very simple concept that can be explained in a forum like this( I could be totally wrong),To comprehend all the ins and outs of this one has to have a book/a tutor and allots of time to practice.
I think ( as dave mentioned in some thread ) that this topic is for guiding in right direction,to reach at the destinantion is a members own task.
Cheers
Khalid
Thanks guys! I think I've got it now. Basically the rollOver(imgName) function is a fast way of saving time, rather than each image having this: <img src="img.jpg" onmouseover="rollOverImg1()"> and <img src="img2.jpg" onmouseover="rollOverImg2()"> and so on, I just have one function that dynamically changes depending on what it passed to the function: <img src="img.jpg" onmouseover="rollOver('img1')">
I can't believe that didn't make sense to me, but now it does! Thank you so much, guys! I never realized that I was so dumb! Haha!
Khalid, I've looked at htmlgoodies.com for stuff like tutorials on passing variables through a function like that and it is never explained well there. I never understood it. Man, I love it when I get these revelations! LOL! Haha. I feel better now...
Code One
03-05-2003, 11:16 PM
but I have to say khaki is probably the worlds prettiest programmer . . . she makes my heart pitter patter, alright Im done before I go to court for sexal harrasment.
Code One
Yes, it is off-topic... *looks at picture* Although.... well, what's wrong with me! It's off topic I said!
....LOL :p