我试图调用 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">×</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/