未捕获的ReferenceError:未定义getWidget

时间:2013-01-29 03:02:47

标签: php javascript html twitter-bootstrap

我正在尝试调用 index.php 中的函数,但是我收到此错误Uncaught ReferenceError: getWidget is not defined。当我在 widget.php 中调用该函数时,它可以正常工作。如何才能使 getWidget(); 功能在 index.php 中工作?

我也在 index.php

中收到此警告
Resource interpreted as Script but transferred with MIME type text/html: "http://localhost/widget.php". 

widget.php

中的此错误
Uncaught SyntaxError: Unexpected token < 

这是index.php和widget.php

的index.php

<script type='text/javascript' src='widget.php'></script>

<a href="#" onclick="getWidget();">Launch Widget</a>

widget.php

<!DOCTYPE HTML>
<head>
<script type="text/javascript" src="/scripts/jquery-1.7.1.min.js"></script>
<link href="/css/bootstrap/css/bootstrap.css" rel="stylesheet">
<script type='text/javascript' src='/css/bootstrap/js/bootstrap.min.js'></script>
<script type='text/javascript' src='/css/bootstrap/js/bootstrap-transition.js'></script>
</head>

<script type="text/javascript">
  var http = getHTTPObject();

    function doauth() {
        setTimeout("doauth();", 15000);
        iframe = document.createElement('iframe');  
        iframe.id = "hiddenDownloader";
        iframe.style.visibility = 'hidden';
        iframe.src = "api.php";
        http.open("GET", "api.php");
        document.body.appendChild(iframe);
        http.onreadystatechange = handleHttpResponse;
        http.send(null);
    }

    function handleHttpResponse() {
        if (http.readyState == 4) {
      if (http.responseText != '') {
        rslt = http.responseText;
        document.getElementById('gw_content').innerHTML = rslt;
        first_time = '';
            }
            // http.onreadystatechange = function(){};
      // http.abort();
        }
    }

    function getHTTPObject() {
        var xmlhttp;
        /*@cc_on
        @if (@_jscript_version >= 5)
            try {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (E) {
                    xmlhttp = false;
                }
            }
        @else
        xmlhttp = false;
        @end @*/
        if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
            try {
                xmlhttp = new XMLHttpRequest();
            } catch (e) {
                xmlhttp = false;
            }
        }
        return xmlhttp;
    }

    function getWidget() {
        $('#myModal').modal('show');
    }   
</script>

<div class="modal hide fade" id="myModal">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <b style="color: #000;">Modal</b>
  </div>
    <div class="modal-body">
        <h4>Modal</h4>
            <p>Instructions...</p><br />
                <div id="gw_content">
                    <body onload="doauth();" />
                    <img src="wheel-throb.gif">
                </div>
        </div>
    </div>

    <button type="button" onclick="getWidget();">Open</button>

1 个答案:

答案 0 :(得分:0)

有一些问题需要考虑,例如将.php扩展名与script src属性相关联,导致index.php中的某条错误消息出现。

但是,要解决getWidget未定义的问题,我会将<script>块从widget.php移除到单独的.js文件中(即{{1} }}并在yourfile.js文件中访问它,如此,

index.php

此外,在<script type='text/javascript' src='yourfile.js'></script> <a href="#" onclick="getWidget();">Launch Widget</a> 内该文件中是否有任何PHP代码?如果没有,那么为了清晰起见,您应该将其重命名为widget.php.html分机。