I am working on a plugin to replace window.showmodalDialog. However, whenever I try to call my plugin function I am receiving the typical and dreaded "Object does not support this method".
I've boiled down my plugin to show a simple alert box.
( function ( $ )
{
$.fn.showAlert = function ()
{
alert( "Hello There" );
}
} )( jQuery );
This is placed in a separate JavaScript file, included on the page from which I want to call the function and I call the function as such: $.showAlert();
The execution of that line causes the error.
If I boil the code down even further and make it an immediate function,
( function ( $ )
{
alert( "Hello There" );
} )( jQuery );
the alert box pops up as soon as the page loads.
I am certain I am doing something wrong, but I cannot figure out what it is. I have googled this error and looked through SO and everything I can determine is I am constructing my plugin correctly.
EDIT
In an effort to provide full disclosure. In our application, we have a JavaScript file, optionActions.js, that provides several different ways to open new windows. One of the functions is to open a modal dialog, openModal. This function takes several parameters used to define the modal window. The basic structure of this function is
function openModal(strCategory, objParam, strFeatures, strModule)
{
//Various code and function calls to format the incoming data for the modal window
//Provides a single object containing the necessary parameters and data for the modal window
var objDialogIn = new OBJDIALOGIN( objParam );
return window.showModalDialog(strUrl, objDialogIn, strFeatures);
}
So modifying this function to use what should be my new plugin would look like this.
//Include the JS file containing the definition for "showAlert"
document.write( "<script LANGUAGE=JavaScript src='/js/ModalDialog.js'></script>" );
function openModal(strCategory, objParam, strFeatures, strModule)
{
//Various code and function calls to format the incoming data for the modal window
//Provides a single object containing the necessary parameters and data for the modal window
var objDialogIn = new OBJDIALOGIN( objParam );
$( "body" ).showAlert();
}
Based on the responses thus far, the ModalDialog.js file contains the following.
( function ( $ )
{
$.fn.showAlert = function ()
{
alert( "Hello There" );
}
} )( jQuery );
Now the optionActions.js file is included in every other html page in our application.
So, using the recommendations, I added a selector to attach the showAlert function. The selector is not null or undefined, by my method is.
So basically, I have an html module which includes the optionActions.js file. optionActions.js contains the function openModal. openModal is called from a script block in the html module. optionActions includes the ModalDialog.js module, which contains the showAlert function.
Therefore, as the html module loads the call to openModal should trigger the showAlert, but showAlert is undefined.