php - Uncaught ReferenceError : getWidget is not defined

标签 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>

最佳答案

有几个方面值得关注,例如链接 .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 代码吗?如果没有,那么您应该将其重命名为 .html.htm为了清晰起见,扩展。

关于php - Uncaught ReferenceError : getWidget is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14574968/

相关文章:

php - 如何在 Emacs 中关闭 php-indent 警告

php - 如何使用 cli 启用 php5-redis?

javascript - PostgreSQL 字符串比较

php - 有没有办法在 gmail/wave/gdocs 中粘贴代码片段/ block ?

jquery - 修改 Jquery 以重置浏览器窗口大小

php - APC 是否适用于高度动态的 PHP 网站?

javascript - AJAX/Javascript 在 PHP 中页面加载时选择元素更改

javascript - 设计 Javascript 前端 <-> C++ 后端通信

javascript - AngularJS:带有查询字符串和表单的单页应用程序?

jQuery:在页面加载时隐藏图片