0

我有一个电子商务网站,我正在创建一个脚本来确定我的用户所在的国家/地区,如果它是我们运送到的四个国家之一,那么我们将返回一个声明,说我们运送到那个国家。我能够使用 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';
}
4

2 回答 2

0

查看位置脚本的 URL:

http://smart-ip.net/geoip-json?callback=GetUserInfo

我明白了callback=GetUserInfo。这意味着脚本要求该函数在被调用之前存在。由于您现在已将此函数移到脚本之后,因此无法调用它。您将需要 2 个单独的外部脚本。一个在之前调用以设置适当的功能,一个在之后调用以使用结果。

作为替代方案,您可以考虑检查此服务以查看是否可以以其他方式调用它。也许jsonp通过 jquery 的ajax.

于 2013-06-18T17:42:19.733 回答
0

你的脚本有问题。这是你需要的。

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>
</head>

<body>

<label id="lblCountry"></label>
<a id="weship"/>
<script type="text/javascript" src="scripts/countrylocate.js"></script>
<script type="text/javascript" src="http://smart-ip.net/geoip-json?callback=GetUserInfo"></script>
</body>
</html>

将 countrylocate.js 修改为:

function GetUserInfo(data) {
    strip = data.host;
    strcountry = data.countryName;

    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';
    }
}
于 2013-06-18T17:42:35.670 回答