1

I'm trying to call a function in index.php but I get this error Uncaught ReferenceError: getWidget is not defined. When I call the function in the widget.php it works. How can I get the function getWidget(); to work in index.php?

I also get this warning in index.php

Resource interpreted as Script but transferred with MIME type text/html: "http://localhost/widget.php". 

and this error in widget.php

Uncaught SyntaxError: Unexpected token < 

Here is index.php and 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>
4

1 に答える 1

0

.php拡張機能を属性にリンクするなど、いくつかの懸念事項がありscript src、エラー メッセージの 1 つが表示さ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拡張子に変更する必要があります。

于 2013-01-29T03:12:42.690 に答える