我正在尝试调用 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>
答案 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
分机。