call jquery document.ready function inside another jquery or javascript function - javascript

I am facing one problem
I am trying to call document.ready() function inside another JavaScript function but when I am calling it, it says "function is not defined
Here is my code :
this is first function that is document.ready() function
$(document).ready(function ExpandMessageBox() {// this is my document.ready function. I have given the name of it as "ExpandMessageBox"}
this is another regular JavaScript function in which I am trying to call above function
function ShowMessageBox() {
ExpandMessageBox();}
But this is giving me an error as :
"Uncaught ReferenceError: ExpandMessageBox is not defined"
Where I am doing wrong...
Please Help
Tons of thanks in advance

You need to define ExpandMessageBox function in the common scope. Something like this:
function ExpandMessageBox() {
//your staff
}
function ShowMessageBox() {
ExpandMessageBox();
}
$(document).ready(ExpandMessageBox);

Related

sports.util.Utils.openNewWindow is not a function

I have showNewWindow, showNewWindowSuccess functions in one js file
and I have openNewWindow function in another js file,
I am trying tto remove the showNewWindowSuccess function from my
success function
but I am getting the following error, Uncaught TypeError:
sports.util.Utils.openNewWindow is not a function
can you guys tell me how to fix it
providing my code below
showNewWindow: function(menu) {
var me = this,
newWindowId = sports.util.Utils.randomString(12);
To declare a function in javascript you need this sintax:
function openNewWindow(param1, param2, param3){
//Do something here...
};

Overload a JS plugin function with a function from inside

I want to call a function which is in my plugin.
I overloaded a function but I want to call another function from it.
$("mySelector").Myplugin({
My_function : function (){
*do some stuf*
function_from_the_plugin();
}
});
An error appears that says "Unknown function". I think it do not search the function in the plugin but outside.
Do you know how I can do?
I already try with "this", a variable corresponding to my plugin object.
If you have an idea please share it with me.
Thank you for reading this post.
Add this.function_from_the_plugin(); in code where Myplugin() is initialized.

How to call javascript from Jquery function?

I want to call jquery function in side of java script. My code is:
<script type="text/javascript">
function calljs(){
getUserMail(usermMail);
}
$(function() {
function getUserMail(usermMail) {
***some code*****
}
});
</script>
I got error from browser console:
ReferenceError: getUserMail is not defined.
How to solve this problem?
As far as i understand, the method is not defined when the method is being called. So define it before it is getting called
<script type="text/javascript">
function getUserMail(usermMail) {
***some code*****
}
function calljs(){
getUserMail(usermMail);
}
$(function() {
//
});
</script>
hope it helps
If it is really compulsory to put the function with in the jquery's ready callback (which I don't think is compulsory) use the following way
<script type="text/javascript">
var getUserMail = null;
function calljs(){
if ( null !== getUserMail ) {
getUserMail(usermMail);
}
}
$(function() {
getUserMail = function (usermMail) {
***some code*****
}
});
</script>
You can simply do ,
$(document).ready(function(event) {
getUserMail(usermMail);
});
and define it like ,
function getUserMail(usermMail){
. . .
}
or using jquery ,
$(document).on('click', ".selector", function);
trigger a function on an event
getUserMail is not defined in a scope that is accessible to calljs. This is why you get the ReferenceError; in the context in which you tried to invoke getUserMail there was no function with that name available.
// At this point nothing is defined
function calljs(){
getUserMail(usermMail);
}
// now calljs is defined as a global and can be invoked from anywhere
$(function() { // this line is calling a function named $ (an alias for jQuery)
// and passing it an anonymous function as a parameter.
function getUserMail(usermMail) { // This function is being defined inside
// the scope of the anonymous function,
// it can be used anywhere inside the
// anonymous function but not outside it.
// ***some code*****
}
});
// we are now outside the scope of the anonymous function,
// getUserMail is no longer in our scope and can't be called from here.
The easiest and likely best solution for most situations would be to make sure that any functions that call each other are in the same scope.
From what I can tell you don't really need calljs, you were just trying to use it to poke a hole into the scope of the anonymous function where getUserMail is defined.
Instead you should probably get rid of calljs and move any code that is calling getUserMail inside the ready callback. If getUserMail needs to wait for the ready callback to be fired before you call it, any code that invokes it also should be inside the ready callback too. (Things like event handlers that call it should already be inside the ready callback anyway.)
If there is a reason that you can't move it into the ready callback, such as something in another .js file needs to be able to call it etc, your application might be too complicated to be realistically maintained as jQuery soup. It might be worth the effort to port it to a framework such as Ember or Angular.
Also so you know, there is no need to use the type attribute on your script tags. JavaScript is the only language that has wide support in the browser and all browsers default to using JavaScript for script tags.

$.getJSON callback does work

I am experimenting with jQuery and I try to use the $.getJSON function
I red that "A reference to a function declaration could equally be provided as the callback". So instead of an anonymous function, I use a reference to a function declaration like this
$('#letter-b').click(function(e) {
e.preventDefault();
$.getJSON('b.js', outside(data));
});
function outside (data){alert(data);}
I get no alert. Instead I get Uncaught ReferenceError: data is not defined
What am I missing? Is it my syntax?
Thanks in advance
You must give the reference to your function. Like this :
$('#letter-b').click(function(e) {
e.preventDefault();
$.getJSON('b.js', outside);
});
If you write outside(data), you just execute your function.

Why can't I assign a variable inside a function in Javascript?

Very new to JS here. When I write PHP I have no problem assigning variables inside a function, but inside a JavaScript function it doesn't work. Why?
example:
function hello() {
var animal = 'Dog';
document.write(animal);
}
Are you calling the hello() function anywhere? If not, you will not execute the contents of the function and thus, no write will happen.
put this snippet inside your function to check if your function is being called
alert('snippet');
If a message box appears, your code should work, but if it does not then the function is not being executed, post some html coding also.

Categories