Simplified Example:
// path/to/js/panel.js
$(function(){
var hidePanel = function(){
//hides the panel div
};
});
// path/to/js/another-script.js
$(function(){
hidePanel(); //out of scope MEGA-FAIL
});
As we speak, i have some functions/variables copy-pasted in 2 different files.
I was curious whether RequireJS would solve this problem...
Your function itself just needs to be declared after jQuery is loaded if it needs jQuery, but it need not be declared on document.ready, just executed then. The simplest way to do what you're after is:
// path/to/js/panel.js
function hidePanel() {
//hides the panel div
}
// path/to/js/another-script.js
$(hidePanel);
This just passes your function to $(), which schedules it run on document.ready, or immediately if the document is already ready.
// path/to/js/panel.js
$(function(){
var hidePanel = function(){
//hides the panel div
};
});
With this code you create an an anonymous function. Anonymous functions don't pollute the global namespace. All variables (in this case hidePanel) declared in an anonymous function are not visible outside of the anonymous function.
Because of that the function is not available.
So you need to do a global function.
You can make that in different ways:
var hidePanel = function() {
});
function hidePanel() {
}
P.S: I recommend that you learn the OO-Pattern for javascript :-)
Javascript Object-Oriented Programming | Part 1
Related
I am studying a JavaScript file and saw in it that some of the methods are wrapped inside a jQuery function. Can Anyone help me how to invoke the following method? And may I know what is the advantage or why the method is wrapped in a function? Below is my sample JavaScript code.
JQuery/JavaScript
$(document).ready(function () {
//How to invoke "testMethod" method?
$(function () {
function testMethod() {
alert("this is a test method");
}
});
});
As you've declared it, testMethod() is a local function and is only available inside the function scope in which it is declared. If you want it to be callable outside that scope, you will need to define it differently so that it is available at a broader scope.
One way of doing that is to make it a global function:
$(document).ready(function () {
//How to invoke "testMethod" method?
$(function () {
window.testMethod = function() {
alert("this is a test method");
}
});
});
testMethod(); // available globally now
It could also be attached to a global namespace or it could be defined at a higher scope where it would also solve your problem. Without specifics on your situation, we can't suggest which one would be best, but the main thing you need to do is to change how the function is declared so it is available in the scope in which you want to call it from.
P.S. Why do you have one document ready function nested inside another? That provides no extra functionality and adds unnecessary complexity. Also, there's really no reason to define testMethod() inside your document ready handlers if you want it available globally.
Before anything else:
$(document).ready(function(){...});
//is the same as
$(function(){...}}
As for your question, here's are potential ways to do it:
If that function is some utility function that everyone uses, then have it available to all in some namespace, like in this one called Utility:
//Utility module
(function(ns){
//declaring someFunction in the Utility namespace
//it's available outside the ready handler, but lives in a namespace
ns.someFunction = function(){...}
}(this.Utility = this.Utility || {}));
$(function(){
//here in the ready handler, we use it
Utility.someFunction();
});
If they all live in the ready handler, and want it to be used by all code in the handler, have it declared in the outermost in the handler so all nested scopes see it.
$(function(){
//declare it in the outermost in the ready handler
function someFunction(){...}
//so we can use it even in the deepest nesting
function nestedSomeFunction(){
someFunction();
}
someElement.on('click',function(){
$.get('example.com',function(){
someFunction();
});
});
nestedSomeFunction();
someFunction();
});
Your call needs to be within the $(function.
It's all about scope and you need to break the testMethod out of the $(function.
Can you perhaps further explain your requirement so that we can maybe help a little better?
Into ready event:
$(document).ready(function () {
//How to invoke "testMethod" method?
var testMethod = function () {
alert("this is a test method");
}
// V0.1
testMethod();
// V0.2
$('#some_id').click(testMethod);
});
In other part:
myObj = {testMethod: null};
$(document).ready(function () {
//How to invoke "testMethod" method?
myObj.testMethod = function () {
alert("this is a test method");
}
});
// Something else
if( myObj.testMethod ) myObj.testMethod();
In the rails.js that came with my rails (3.0.x, still with prototype), I see the following structure:
(function() {
// ...
document.on("click", ...
})();
What exactly is accomplished with the wrapping of the whole code in the anonymous function? Is this a valid way to delay the code until the dom has loaded or only the document object?
In my project, I currently have a lot of setup code inside a Event.observe(document, 'dom:loaded', function() { ... } block. I was wondering, if I should adopt the pattern above when I refactor my code.
You have stumbled across the module pattern. It is useful because variables inside the immediately invoked function are local and don't pollute the global namespace.
(function(){
var something = 17;
//can use something inside here
}());
//but not here anymore
Not ethat there is no difference in timeing since the function is immediately invoked (in the final () bit)
The self-invoking anonymous function will trigger what is inside immediately, which has nothing to do with delaying the code.
To make the code block inside be executed after the DOM is ready, you have to have DOMready listener. I guess the code you mentioned Event.observe(document, 'dom:loaded', function() { ... } is the one.
I have made a lot of functions. I put these functions in a script.js file. But all these functions are loading on every page.
I have made the functions like this:
$(function () {
}):
But how can I ensure that the functions are not loading on every page?
That I can call only the function that I need?
Paul Irish has a nice way of dealing with that problem. See his article:
Automate firing of onload events
You can basically create an structure for each of your pages like this:
var siteNameSpace = {
homepage : {
onload : function(){
// do this stuff.
}
},
contact : {
onload : function(){
// onload stuff for contact us page
}
}
};
And respective page's code will fire only with:
$(document).ready( siteNameSpace[ document.body.id ].onload );
For that, you need to assign an id to body tag of each of your pages:
<body id="home">
<body id="contactUs">
and so on
One way would be to split the .js files into modules, and only include the .js files that are relevant to the page.
In jQuery, it is perhaps more common to use an approach such as this:
$(function () {
if (someCheck) {
// do something
}
if (someCheck) {
// do something else
}
});
The someChecks are usually checking that a specific element exists (if ($(someSelector).length)).
However, to specifically answer your question, you'd give the functions a name and then call them, so instead of having;
$(function () {
// do foo
})
You'd have
function someName() {
// do foo
}
and then you can call the function like someName(), but obviously you'd need to put the function call in a place which you could control on per-page basis (such as in the <head> of the page, or by including different .js files as explained earlier in the answer.
You can create few sets of external javascript files, place them in page wherever they are required.
Alternatively you can load script using jQuery's getScript method;
$.getScript('url of ext js', function(data, textStatus) {
});
I can't really understand what you're exactly asking for, but maybe I can clear some things out for you.
It doesn't matter if variables (functions) are declared in different files, what does matter is in what scope they are declared.
In other words, even if the functions are in different files, if they're declared global (not inside a closure/function/object), they are visible and can be called from anywhere.
Ex of global functions:
function foo() {/*...*/}
var bar = function(){/*...*/}
window.func = function(){/*...*/}
If you want to "hide" the functions, making them inaccessible from a different scope, you should use a closure.
Ex of functions defined in closure:
function initialise {
var foo = function(){}
function bar(){}
alert(typeof foo); // function
alert(typeof bar); // function
// foo & bar are visiblehere
}
alert(typeof foo); // undefined
alert(typeof bar); // undefined
// foo & bar are undefined here
More over, if you want to execute them only once and completely make them inaccessible, you can wrap them in a self executing function :
(function(){
function bar(){}
alert(typeof bar);// function
// bar is visible here
})();
// bar is undefined here
I don't know if I answered your question, but I hope info this helps.
I have two external .js files. The first contains a function. The second calls the function.
file1.js
$(document).ready(function() {
function menuHoverStart(element, topshift, thumbchange) {
... function here ...
}
});
file2.js
$(document).ready(function() {
setTimeout(function() { menuHoverStart("#myDiv", "63px", "myIMG"); },2000);
});
The trouble is that this is not running the function. I need the two separate files because file2.js is inserted dynamically depending on certain conditions. This function works if I include the setTimeout... line at the end of file1.js
Any ideas?
The problem is, that menuHoverStart is not accessible outside of its scope (which is defined by the .ready() callback function in file #1). You need to make this function available in the global scope (or through any object that is available in the global scope):
function menuHoverStart(element, topshift, thumbchange) {
// ...
}
$(document).ready(function() {
// ...
});
If you want menuHoverStart to stay in the .ready() callback, you need to add the function to the global object manually (using a function expression):
$(document).ready(function() {
window.menuHoverStart = function (element, topshift, thumbchange) {
// ...
};
// ...
});
You have declared menuHoverStart inside a function (the anonymous one you pass to the ready ready). That limits its scope to that function and you cannot call it from outside that function.
It doesn't do anything there, so there is no need to hold off on defining it until the ready event fires, so you could just move it outside the anonymous function.
That said, globals are worth avoiding, so you might prefer to define a namespace (to reduce the risk of name collisions) and hang the function off that.
var MYNAMESPACE = {}; // In the global scope, not in a function
// The rest can go anywhere though
MYNAMESPACE.menuHoverStart = function (element, topshift, thumbchange) {
I have a 2000 line jquery file, I just broke up the file into smaller ones, If I have a function in the first file, that file # 2 is referring to, it's coming up undefined.
Every file is is wrapped in a jquery ready function, What's the best way to do this?
If the function in question is declared within the scope of the ready handler, it won't be accessible to any other code, including other ready handlers.
What you need to do is define the function in the global scope:
function foo()
{
alert('foo');
}
$(document).ready(function()
{
foo();
});
P.S. A more concise way of adding a ready handler is this:
$(function()
{
foo();
});
Edit: If the contents of each of your divided ready handlers rely on the previous sections, then you can't split them up, for the reasons outlines above. What would be more sensible would be to factor out the bulk of the logic into independent functions, put these in their own files outside the ready event handler, and then call them from within the handler.
Edit: To further clarify, consider this handler:
$(function()
{
var foo = 'foo';
var bar = 'bar';
alert(foo);
alert(bar);
});
I might then split this up:
$(function()
{
var foo = 'foo';
var bar = 'bar';
});
$(function()
{
alert(foo);
alert(bar);
});
The problem with this is that foo and bar are defined in the first handler, and when they are used in the second handler, they have gone out of scope.
Any continuous flow of logic like this needs to be in the same scope (in this case, the event handler).
Function definition should not be wrapped in another function. Not unless you really want that function definition to be private. And if I understand correctly that's not your intention.
Only wrap function invocation in the jQuery ready function.
If you're worried about your functions clashing with third party function names then namespace them:
var myFunctions = {}
myFunctions.doThis = function () {}
myFunctions.doThat = function () {}
But really, you only need to worry about this if you're creating a mashup or library for others to use. On your own site YOU have control of what gets included in javascript.
Actually, for performance reasons, it may be better to keep it in one file; multiple requests actually can take up more bandwidth... but as separate files, you would need to order them in a particular order so that there is a logical sequence. Instead of having everything in a document.ready, have each script define a method, that the page will execute within its own document.ready handler, so that you can maintain that order.
Most likely the reason it's coming up undefined is because when you have separate ready calls, the scope of the code inside those calls is different.
I would reorganize my code. Any shared functions can be attached to the jQuery object directly, using $.extend. This is what we do for our application and it works well.
See this question. Hope it helps.
Everyfile shouldnt have a ready function. Only one file should have the ready function and that should be the last file.
"wrapped in a jquery ready function" is nothing else than binding stuff to the ready event that is fired when jQuery thinks the DOM is ready.
You should only bind methods that is depending on the DOM to the ready event. It doesnt matter how many binds you make, all of the methods will be executed in the binding order in the end.
Functions provide scope in JavaScript. Your code in the jquery.ready is an anonymous function, so it is unaware of the other scopes. remove the wrappings for those JavaScript functions and declare them as regular functions, a la
$(document).ready(function ()
{
functionFromFile1();
functionFromFile2();
};