Smokey
Smokey

Reputation: 117

Global function unable to call

Can someone please show me what I am doing wrong, I cannot get this to work and I have read about 10+ stack overflow scope questions and I'm so very confused.

My popup.js file:

function popupClickGlobal()
{
window.popupClick = function(){ 
    window.alert("insidePopupClick!!!!");
    loading(); // loading
    setTimeout(function(){ // then show popup, deley in .5 second
    loadPopup(); // function show popup 
    }, 500); // .5 second
}
}

$("a.topopup").click(function() {
        loading(); // loading
        setTimeout(function(){ // then show popup, deley in .5 second
            loadPopup(); // function show popup 
        }, 500); // .5 second
return false;
});

/* event for close the popup */
$("div.close").hover(
                function() {
                    $('span.ecs_tooltip').show();
                },
                function () {
                    $('span.ecs_tooltip').hide();
                }
            );

$("div.close").click(function() {
    disablePopup();  // function close pop up
});

$(this).keyup(function(event) {
    if (event.which == 27) { // 27 is 'Ecs' in the keyboard
        disablePopup();  // function close pop up
    }   
});

$("div#backgroundPopup").click(function() {
    disablePopup();  // function close pop up
});

 /************** start: functions. **************/
function loading() {
    $("div.loader").show();  
}
function closeloading() {
    $("div.loader").fadeOut('normal');  
}

var popupStatus = 0; // set value

function loadPopup() { 
    if(popupStatus == 0) { // if value is 0, show popup
        closeloading(); // fadeout loading
        $("#toPopup").fadeIn(0500); // fadein popup div
        $("#backgroundPopup").css("opacity", "0.7"); // css opacity, supports IE7, IE8
        $("#backgroundPopup").fadeIn(0001); 
        popupStatus = 1; // and set value to 1
    }   
}

function disablePopup() {
    if(popupStatus == 1) { // if value is 1, close popup
        $("#toPopup").fadeOut("normal");  
        $("#backgroundPopup").fadeOut("normal");  
        popupStatus = 0;  // and set value to 0
    }
}
/************** end: functions. **************/

My index.php snippets:

<script type="text/javascript" src="_script/popup.js"></script>
    <script>
        if(formPosted) {
            window.alert("popupclick!");
            $(document).ready(popupClickGlobal);
            popupClickGlobal();
        }
    </script>

The window shows me pupupclick! alert, I'm confused how to call the global variable (nowhere seems to be very clear on it. I have also tried $.popupClickGlobal(); to call it. I'm expecting after the alert for a 2nd alert saying insidePopuoClick!!!!

Please help, thank you. Chris

Upvotes: 0

Views: 123

Answers (2)

Gautam
Gautam

Reputation: 3386

Try putting only below code in popup.js

function popupClickGlobal()
{
    window.popupClick = function(){ 
        window.alert("insidePopupClick!!!!");
        loading(); // loading
        setTimeout(function(){ // then show popup, deley in .5 second
        loadPopup(); // function show popup 
        }, 500); // .5 second
    }
}

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

popupClickGlobal is not a global variable..it is local to the dom ready handler in which it is declared.

Since the function popupClickGlobal is to be used in the global scope, just remove the jQuery(function(){...}) wrapper around it

function popupClickGlobal() {
    window.popupClick = function () {
        window.alert("insidePopupClick!!!!");
        loading(); // loading
        setTimeout(function () { // then show popup, deley in .5 second
            loadPopup(); // function show popup 
        }, 500); // .5 second
    }
}

Upvotes: 1

Related Questions