需要从另一个外部.JS文件中引用外部脚本

时间:2013-06-18 17:29:50

标签: javascript

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

2 个答案:

答案 0 :(得分:0)

查看位置脚本的URL:

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

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

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

答案 1 :(得分: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';
    }
}