Jump to content

Recommended Posts

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.

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?

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.

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?

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.

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.

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.

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.

 

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.