我有一个电子商务网站,我正在创建一个脚本来确定我的用户所在的国家/地区,如果它是我们运送到的四个国家之一,那么我们将返回一个声明,说我们运送到那个国家。我能够使用 ip 位置服务脚本(“ http://smart-ip.net/geoip-json callback=GetUserInfo”)和我的脚本组合在一个 html 文档中来实现这一点,但现在我试图移动脚本到 sn 外部 .js 文档我无法弄清楚如何让我的脚本启动 ip 位置服务脚本。
原始 HTML 文档(工作中)
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Get User Country</title>
<script src="http://code.jquery.com/jquery-1.8.2.js" type="text/javascript"></script>
<script type="text/javascript">
var strcountry
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>
<a id="weship"/>
<script type="text/javascript">
if (strcountry == "United States")
{
document.getElementById('weship').innerHTML = 'We ship to The United States';
}
else if (strcountry == "Singapore")
{
document.getElementById('weship').innerHTML = 'We ship to Singapore';
}
else if (strcountry == "Malaysia")
{
document.getElementById('weship').innerHTML = 'We ship to Malaysia';
}
else if (strcountry == "Hong Kong")
{
document.getElementById('weship').innerHTML = 'We ship to Hong Kong';
}
</script>
新建 HTML 文件调用脚本(index.html)
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Get User Ountry </title>
<script src="http://code.jquery.com/jquery-1.8.2.js" type="text/javascript"></script>
<script type="text/javascript" src="http://smart-ip.net/geoip-json?callback=GetUserInfo"></script>
</head>
<body>
<a id="weship"/>
<script type="text/javascript" src="countrylocate.js"></script>
</body>
</html>
我的脚本(countrylocate.js)
var strcountry
function GetUserInfo(data) {
strip = data.host;
strcountry = data.countryName;
}
$(function ()
{
BindUserInfo();
})
function BindUserInfo()
{
document.getElementById('lblCountry').innerHTML = strcountry;
}
if (strcountry == "United States")
{
document.getElementById('weship').innerHTML = 'We ship to The United States';
}
else if (strcountry == "Singapore")
{
document.getElementById('weship').innerHTML = 'We ship to Singapore';
}
else if (strcountry == "Malaysia")
{
document.getElementById('weship').innerHTML = 'We ship to Malaysia';
}
else if (strcountry == "Hong Kong")
{
document.getElementById('weship').innerHTML = 'We ship to Hong Kong';
}