简体   繁体   English

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

[英]Need to reference External script from Inside another External .JS file

I have an ecommerce site and I am creating a script that will determine the country that my user is in , and if it is one of the four countries we ship to then we will return a statement saying we ship to that country. 我有一个电子商务网站,正在创建一个脚本,该脚本将确定用户所在的国家/地区,如果它是我们要运送到的四个国家之一,那么我们将返回一条声明,说我们要运送到该国家/地区。 I was able to achieve this with a ip location service script (" http://smart-ip.net/geoip-json callback=GetUserInfo") and my script combined in an html document, but now that I have attempted to move the script to sn external .js document I can not figure out how to get my script to initiate the ip location service script. 我可以使用ip位置服务脚本(“ http://smart-ip.net/geoip-json callback = GetUserInfo”)和将我的脚本合并到html文档中来实现此目的,但是现在我尝试移动用于外部.js文档的脚本我无法弄清楚如何获取我的脚本来启动ip位置服务脚本。

Original HTML Document(Working) 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>

New HTML File Calling Script(index.html) 新的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>

My Script(countrylocate.js) 我的脚本(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';
}

Looking at the URL for the location script: 查看位置脚本的URL:

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

I see callback=GetUserInfo . 我看到callback=GetUserInfo This implies that the script requires that this function exists prior to it being called. 这意味着脚本要求此函数在被调用之前存在。 Since you have now moved this function after the script, it cannot call it. 既然你现在已经搬到了脚本该功能,就不能调用它。 You will need 2 separate external scripts. 您将需要2个单独的外部脚本。 One called before to set up the appropriate functions and one after to use the result. 一个调用之前设置适当的功能,另一个调用之后使用结果。

As an alternative, you might consider checking with this service to see if it can be called in another manner. 或者,您可以考虑使用此服务,以查看是否可以其他方式调用它。 Perhapse a jsonp call through jquery's ajax . 也许通过jQuery的ajax进行jsonp调用。

Your scripts are out of order. 您的脚本混乱。 Here is what you need. 这就是您所需要的。

Html: 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>

Modify countrylocate.js to this: 将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';
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM