我的Javascript / html代码如下所示,效果很好,并在下方的Part 1
中显示国家/地区名称。
当我尝试转换code in .JS file
它不起作用意味着不会在Part 2
中显示国家/地区名称..不确定代码中有什么问题
第1部分
<script src="http://code.jquery.com/jquery-1.8.2.js" type="text/javascript"></script>
<script type="text/javascript">
var strip, strcountry, strcity, strregion, strlatitude, strlongitude, strtimezone
function GetUserInfo(data) {
strip = data.host; strcountry = data.countryName;
}
$(function ()
{
BindUserInfo();
})
function BindUserInfo()
{
document.getElementById('lblCountry').innerHTML = strcountry;
}
</script>
<script type="text/javascript" src="http://smart-ip.net/geoip-json?callback=GetUserInfo"></script>
</head>
<body>
We Ship To <a id="lblCountry"/>
</body>
第2部分
// JavaScript Document
document.write("<script src='http://code.jquery.com/jquery-1.8.2.js' type='text/javascript'></script>");
var strip, strcountry, strcity, strregion, strlatitude, strlongitude, strtimezone
function GetUserInfo(data) {
strip = data.host; strcountry = data.countryName;
}
$(function ()
{
BindUserInfo();
})
function BindUserInfo()
{
document.getElementById('lblCountry').innerHTML = strcountry;
}
document.write("<script type='text/javascript' src='http://smart-ip.net/geoip-json?callback=GetUserInfo'></script>");
这是PArt 2的HTML
<head>
<title>Get User Details IP Address, city, country, state, latitude, longitude </title>
<script src="test.js" type="text/javascript"></script>
</head>
<body>
We Ship To <a id="lblCountry"/>
</table>
答案 0 :(得分:3)
在您的HTML中将jQuery参考作为真实脚本标记包含在内 - 并删除document.write
。
;
列表的末尾也是var
...也许。
您的<head>
标记应为
<head>
<title>Get User Details IP Address, city, country, state, latitude, longitude
</title>
<script src='http://code.jquery.com/jquery-1.8.2.js' type='text/javascript'>
</script>
<script src="test.js" type="text/javascript"></script>
</head>
正如Paul所说,document.write
已被弃用。您应该始终尝试包含<script>
标记,而不是操纵DOM。我认为你做这件事的方式意味着你的文件中的jQuery代码将在jQuery加载之前执行 - 因为你在你的jQuery之前直接将标记写入DOM码。所以没有时间解析它。我认为这段代码实际上会引发错误。