CyberShot Posted December 25, 2009 Share Posted December 25, 2009 I made a slider effect for my pages on my site. I know that I can call that function from the js file. For example, If I have a blinking function, I can make that function do it's job by doing blink(); at the end of the js file. How do I call this function into play from my index page instead of the js file. The idea is that I want to have several different options for effects on my index page. So I want to be able to choose what function I want from the index page instead of opening the js file and modifying it. Quote Link to comment https://forums.phpfreaks.com/topic/186307-how-do-i-call-my-jquery-function-from-the-index-page/ Share on other sites More sharing options...
trq Posted December 25, 2009 Share Posted December 25, 2009 Place this within your index file somewhere before the closing </body> tag. <script> blink(); </script> Quote Link to comment https://forums.phpfreaks.com/topic/186307-how-do-i-call-my-jquery-function-from-the-index-page/#findComment-984094 Share on other sites More sharing options...
CyberShot Posted December 26, 2009 Author Share Posted December 26, 2009 I tried that and it doesn't work Quote Link to comment https://forums.phpfreaks.com/topic/186307-how-do-i-call-my-jquery-function-from-the-index-page/#findComment-984132 Share on other sites More sharing options...
trq Posted December 26, 2009 Share Posted December 26, 2009 Actually, being a function that relies on jQuery (I assume thats what you mean by jQuery function) you'll likely need to call the function after the dom is finished loading. <script> $(document).ready(function() { blink(); }); </script> Other than that, is you function definition and jQuery itself being included within index? Quote Link to comment https://forums.phpfreaks.com/topic/186307-how-do-i-call-my-jquery-function-from-the-index-page/#findComment-984137 Share on other sites More sharing options...
CyberShot Posted December 26, 2009 Author Share Posted December 26, 2009 I have several functions in the .js file $(document).ready(function(){ function blink(){ } function slideSidWays(){ } function slideDown(){ } blink(); }); this is how the code is set up. So at the bottom of my js file, I am calling the blink function. So the index page does blink. If I change it to slideSideWays, then the index page does slide side ways. No matter how i call these functions from the index page, they don't work. it only works if I call them from the js file. Quote Link to comment https://forums.phpfreaks.com/topic/186307-how-do-i-call-my-jquery-function-from-the-index-page/#findComment-984138 Share on other sites More sharing options...
trq Posted December 26, 2009 Share Posted December 26, 2009 You have defined your functions within the anonymous function passed to the ready() method. Outside of this function, your functions do not exist. Quote Link to comment https://forums.phpfreaks.com/topic/186307-how-do-i-call-my-jquery-function-from-the-index-page/#findComment-984140 Share on other sites More sharing options...
CyberShot Posted December 26, 2009 Author Share Posted December 26, 2009 I don't know what you just said. Can you explain? Quote Link to comment https://forums.phpfreaks.com/topic/186307-how-do-i-call-my-jquery-function-from-the-index-page/#findComment-984142 Share on other sites More sharing options...
trq Posted December 26, 2009 Share Posted December 26, 2009 I'm not sure I can put it more simply. Your functions only exist within the anonymous function you are passing to the ready() method. Remove them from within.... $(document).ready(function() { }); Quote Link to comment https://forums.phpfreaks.com/topic/186307-how-do-i-call-my-jquery-function-from-the-index-page/#findComment-984143 Share on other sites More sharing options...
CyberShot Posted December 26, 2009 Author Share Posted December 26, 2009 you can put it more simply. Don't forget that not everyone is a code expert. That's why we come here for help. Quote Link to comment https://forums.phpfreaks.com/topic/186307-how-do-i-call-my-jquery-function-from-the-index-page/#findComment-984144 Share on other sites More sharing options...
CyberShot Posted December 26, 2009 Author Share Posted December 26, 2009 That did the trick. It's working now. Thank You! For some reason, I can not mark any topic solved. I get an out of session error Quote Link to comment https://forums.phpfreaks.com/topic/186307-how-do-i-call-my-jquery-function-from-the-index-page/#findComment-984145 Share on other sites More sharing options...
trq Posted December 26, 2009 Share Posted December 26, 2009 Don't forget that not everyone is a code expert. Yes, but most people here know English and anyone programming with jQuery should at least attempt to know a little about Javascript and especially about using anonymous functions as arguments. Has your problem been solved? Can you please mark the thread accordingly? Quote Link to comment https://forums.phpfreaks.com/topic/186307-how-do-i-call-my-jquery-function-from-the-index-page/#findComment-984146 Share on other sites More sharing options...
CyberShot Posted December 26, 2009 Author Share Posted December 26, 2009 I can not mark it solved. It doesn't work for me. As for learning javascript, that part will start for me on January 4th. I think I did understand a little what you meant, but would not have known how to make the functions available. Yes, it seems simple. But that's only because you know how to do it. If I think back to all the things I have been learning over the last two years, I am suprised at how many things were so hard to wrap my head around yet are now so simple. It's easy to forget that. The great thing about jquery is that you don't have to know any javascript to use it. I agree though. One should learn javascript. Quote Link to comment https://forums.phpfreaks.com/topic/186307-how-do-i-call-my-jquery-function-from-the-index-page/#findComment-984147 Share on other sites More sharing options...
trq Posted December 26, 2009 Share Posted December 26, 2009 The great thing about jquery is that you don't have to know any javascript to use it. I think this thread alone proves that theory wrong. Don't you? Quote Link to comment https://forums.phpfreaks.com/topic/186307-how-do-i-call-my-jquery-function-from-the-index-page/#findComment-984150 Share on other sites More sharing options...
CyberShot Posted December 26, 2009 Author Share Posted December 26, 2009 no. I have studied C# a little and have seen some C and PHP and usually you see things like public function or private function If I understand them correclty, they say weather or not to all access in other functions to that particular function. I believe that one of the reasons JQuery is becoming so popular is because of it's ease of use. I don't know how to write a .hide() function or a .show() function. But I do know how to use them in jquery. I know very little about what the ready() function does and I know none of it's rules. My issue with your first explanation was that I am not a pro programmer. It doesn't matter how much english I know. if I talk to a person and describe to them how to build a computer saying things like, how to connect the cpu to the mainboard and to make sure to get a mainboard with a high enough bus speed, Most people people would look at me and not understand any of that. But it's all english. So you explain it at the level they can understand. Different people are at different levels of learning. I am taking a javascript course next term which starts on January 4th. Quote Link to comment https://forums.phpfreaks.com/topic/186307-how-do-i-call-my-jquery-function-from-the-index-page/#findComment-984152 Share on other sites More sharing options...
trq Posted December 26, 2009 Share Posted December 26, 2009 I'll agree that jQuery is easy to use. However, without at least a little knowledge of Javascript (as you have seen) you can easily trip up. I'm aware I'm not always the best at explaining things however, so I'm going to go into a little further detail. This isn't necessarily strictly a Javascript thing either (the same can be done in php 5.3) as other languages allow anonymous functions to be defined within the arguments to another function. What you have with jQuery is a ready() function that expects as an argument another function that will then be called when the document is ready to be manipulated. You can do this in two ways.... the traditional. function foo() { console.log("Hello from foo"); } $(document).ready(foo()); Or by actually defining the function anonymously within the argument.... $(document).ready(function() { console.log("Hello from foo"); }); Both these pieces of code do exactly the same thing. Now you might be able to see what is actually happening within the call to ready(). This logically leads us to why your functions where out of scope. As with most programming languages, variables and functions defined within functions are not available outside of said functions. This is no different in Javascript and is indeed why your code was not working. Hopefully that is a little clearer for you. Quote Link to comment https://forums.phpfreaks.com/topic/186307-how-do-i-call-my-jquery-function-from-the-index-page/#findComment-984153 Share on other sites More sharing options...
CyberShot Posted December 26, 2009 Author Share Posted December 26, 2009 yes, that was awesome. I do understand. Thanks for the explanation. That variable scope thing and public and private functions...These things are still tripping me up a little. I think because there is so much to learn. Javascript, jquery, java, php, sometimes it's just to much. Quote Link to comment https://forums.phpfreaks.com/topic/186307-how-do-i-call-my-jquery-function-from-the-index-page/#findComment-984155 Share on other sites More sharing options...
trq Posted December 26, 2009 Share Posted December 26, 2009 There is a typo in my example above too....sorry. function foo() { console.log("Hello from foo"); } $(document).ready(foo); The public and private (visibility) thing only comes into play within php classes. Javascript works an entirely different way to achieve this as I'm sure you'll find out in your course. But all languages have the concept of scope and it should be one of the first things you master with any given language. Without knowing how scope works things can be terribly confusing. Quote Link to comment https://forums.phpfreaks.com/topic/186307-how-do-i-call-my-jquery-function-from-the-index-page/#findComment-984156 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.