需要从另一个外部. js文件内部引用外部脚本
Need to reference External script from Inside another External .JS file
我有一个电子商务网站,我正在创建一个脚本,将确定我的用户所在的国家,如果它是我们运送到的四个国家之一,那么我们将返回一个声明,说我们运送到那个国家。我能够实现这一点与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';
}
}
相关文章:
- appendChild在函数外部工作,但在函数内部不工作
- 需要原型内部或外部的功能
- js和css文件-在publichtml外部或内部
- Cordova包装应用程序内部链接加载在应用程序中,外部链接加载在浏览器中
- 匿名自执行js函数内部的全局变量在外部仍然可用
- 从内部函数javascript内部分配外部函数的对象
- 将外部函数返回的id传递给内部函数
- 在内部JavaScript函数中包含外部对象
- 保持外部视图不变,而只是在angular ui中重新加载内部视图
- 访问内部功能范围的元素,而不是敲除中的外部
- 内部函数不会为外部函数在 jQuery 中动态创建的元素赋值
- 如何从许多内部非阻塞函数调用之一中提前退出外部函数
- 为什么内部Javascript循环会阻止外部循环中断(即无限循环)
- 如何使用Javascript调用外部库's函数内部的函数
- Javascript和CSS,内部HTML与外部文件
- JavaScript-如何将onload函数内部定义的变量取到外部
- CSS和Javascript可以在内部服务器上工作,但不能在外部工作
- 如何检测用户点击了内部或外部链接
- 强制定向布局从外部/内部数据链接获取特定数据
- 如何在页面上的所有其他脚本之前加载外部/内部javascript