无法在js文件中调用函数

时间:2014-02-20 22:28:48

标签: javascript jquery html popup call

我的index.php有以下几行:

<!doctype html><html class=""><head><title>a title</title>
<link href="_css/boilerplate.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="_script/popup.js"></script>

....然后我们也得到了:

<?php
if(isset ($_GET['page']))
  {
    if ($_GET['page'] == "mail")
      {
         echo "<script>window.alert('Found the reply!');var formPosted = true;</script>";
   }
   }
?>
<script>
if(formPosted) {
  window.alert("popupclick!");
  popupClick();
}
</script>

警告弹出窗口!显示在屏幕上,但popupClick()函数似乎不起作用。

popup.js(其中包含函数popupClick():

jQuery(function($) {

$("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 popupClick() {
    window.alert("insidePopupClick!!!!");
    loading(); // loading
    setTimeout(function(){ // then show popup, deley in .5 second
        loadPopup(); // function show popup 
    }, 500); // .5 second
}

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. **************/
}); // jQuery End

感谢您的帮助!克里斯

1 个答案:

答案 0 :(得分:0)

使用以下语法:

jQuery(function($) {
    ...
});

大括号之间的代码仅在整个页面加载后才执行,因此在调用它时函数popupClick尚未定义。 您应该从闭包中提取函数定义:

jQuery(function($) {
    ...
}); // jQuery End

/************** start: functions. **************/
...
/************** end: functions. **************/