0

私は e コマース サイトを持っており、ユーザーがいる国を特定するスクリプトを作成しています。その国が出荷先の 4 か国のうちの 1 つである場合は、その国に出荷することを示すステートメントを返します。IP ロケーション サービス スクリプト (" http://smart-ip.net/geoip-json callback=GetUserInfo") と HTML ドキュメントに結合されたスクリプトでこれを実現できましたが、外部.jsドキュメントをsnするスクリプト スクリプトを取得して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 つの別個の外部スクリプトが必要になります。1 つは適切な関数を設定するために前に呼び出され、もう 1 つは結果を使用するために呼び出されます。

別の方法として、このサービスをチェックして、別の方法で呼び出すことができるかどうかを確認することを検討してください。おそらくjsonpjqueryの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 に答える