1

我正在尝试调用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

索引.php

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

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

小部件.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>
4

1 回答 1

0

有几个方面的问题,例如将.php扩展链接到script src属性,这会导致您的错误消息之一index.php出现。

但是,为了解决getWidget未定义的问题,我会将<script>块从widget.php一个单独的.js文件(即, )中删除,然后像这样在您的文件yourfile.js中访问它,index.php

<script type='text/javascript' src='yourfile.js'></script>
<a href="#" onclick="getWidget();">Launch Widget</a>

此外,widget.php该文件中是否有任何 PHP 代码?如果不是,那么为了清楚起见,您应该将其重命名为 a.html.htm扩展名。

于 2013-01-29T03:12:42.690 回答