需要从另一个外部. js文件内部引用外部脚本

Need to reference External script from Inside another External .JS file

本文关键字:外部 内部 引用 脚本 文件 js 另一个      更新时间:2023-09-26

我有一个电子商务网站,我正在创建一个脚本,将确定我的用户所在的国家,如果它是我们运送到的四个国家之一,那么我们将返回一个声明,说我们运送到那个国家。我能够实现这一点与ip位置服务脚本("http://smart-ip.net/geoip-json回调=GetUserInfo")和我的脚本组合在一个html文档,但现在,我已经试图移动到外部的脚本sn .js文档,我无法弄清楚如何让我的脚本启动ip位置服务脚本。

原始HTML文档(Working)

<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';
}

查看位置脚本的URL:

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

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

作为备选方案,您可以考虑检查此服务,看看是否可以以另一种方式调用它。也许是通过jquery的ajax调用jsonp

您的脚本顺序乱了。这是你需要的。

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>

修改countrylocation .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';
    }
}