如果浏览器检测到它在线,我正在使用函数“loadScript”插入外部 .js。
<script type="text/javascript">
function loadScript()
{
if (navigator.onLine == true)
{
var src = "js/getdata.js";
var script = document.createElement("script");
script.type = "text/javascript";
script.src = src;
document.getElementsByTagName("head")[0].appendChild(script);
}
}
</script>
一切正常,除了最终脚本显示为...
<script type="text/javascript" src="js/getdata.js">
function getData() // get MySQL data from db
{
<?php
$dbopen = 0; // database flag
$con = mysql_connect("website.co.uk","guest","password");
mysql_select_db("DB", $con);
if (!$con)
{die('Could not connect: ' . mysql_error());}
else
{$dbopen = 1;}
?>
ETC...
Firebug 控制台告诉我函数 getData 没有定义,内容
<?php
是语法错误...
我想将脚本放入头部并将其定义为 DOM 中的可用函数。
如果您有任何想法,我将不胜感激。
GitaarLAB 要求提供整个代码,所以就在这里。
<html>
<head>
<script type="text/javascript">
function loadScript()
{
if (navigator.onLine == true)
{
var src = "js/getdata.js";
var script = document.createElement("script");
script.type = "text/javascript";
script.src = src;
document.getElementsByTagName("head")[0].appendChild(script);
}
}
</script>
</head>
<body>
<script type="text/javascript">
loadScript();
</script>
</body>
</html>
输出是...
<script type="text/javascript" src="js/getdata.js">
function getData()
{
<?php
$hhgopen = 0;
$con = mysql_connect("website.co.uk","guest","password");
mysql_select_db("ThisDB", $con);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else
{
$result = mysql_query("SELECT * FROM articles", $con);
$numrows = mysql_num_rows($result);
$hhgopen = 1;
}
?>
}
</script>