简体   繁体   English

为什么此javascript一直说未定义?

[英]Why does this javascript keep saying undefined?

I'm using maxmind's geoip script in order to call out visitors individual city and state name when they arrive on my website. 我使用maxmind的geoip脚本来在访问者到达我的网站时调出他们各自的城市和州名。 This is what I have done. 这就是我所做的。

I have this in my header: 我的头中有这个:

<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>

Then, right under my <body> tag, I have the following: 然后,在我的<body>标记下,我具有以下内容:

<script language="JavaScript">
var country=geoip_country();
var region=geoip_region();
var city=geoip_city();
if(country=="")
    country="US";
if(region=="")
    region="New York";
if(city=="")
    city="New York";
</script>

Then, in order to show the visitor's city for example, I put this: 然后,例如,为了显示访问者所在的城市,请输入以下内容:

<script language="JavaScript">document.write(city);</script>

However, when I put the directly above on my website, it says undefined wherever the city is supposed to show up. 但是,当我将其直接放在我的网站上时,无论该城市应该出现在哪里,它都没有定义 It does this with other javascript I have as well and I'm wondering what I am doing wrong...... 它也与我拥有的其他JavaScript一起执行此操作,我想知道我做错了什么……

Thank you for any insights. 感谢您的任何见解。 :) :)

The problem is this line: 问题是这一行:

var country=geoip_country();

When execution of your first script block hits that line it crashes because there is no such function as geoip_country(); 当您的第一个脚本块的执行到达该行时,它会崩溃,因为没有诸如geoip_country();功能geoip_country(); , which means no later lines in that block are executed so the calls to geoip_city() and geoip_region() don't happen at all. ,这意味着该块中不会再执行任何行,因此对geoip_city()geoip_region()的调用根本不会发生。

(But the city variable itself still exists because of JavaScript's "hoisting" mechanism, so it can be accessed from the second script block but still has a default value of undefined .) (但是由于JavaScript的“提升”机制, city变量本身仍然存在,因此可以从第二个脚本块访问它,但默认值为undefined 。)

You need to change that line to: 您需要将该行更改为:

var country=geoip_country_name();

Or, depending on your need, to: 或者,根据您的需要,执行以下操作:

var country=geoip_country_code();

With that error corrected the rest of the script should run as you expect, including getting the city. 纠正了该错误,脚本的其余部分应按预期运行,包括获取城市。

Demo: http://jsfiddle.net/2kbeg/ 演示: http : //jsfiddle.net/2kbeg/

The statement might be executing before the DOM is ready, document.write(city); DOM准备就绪之前,该语句可能正在执行。 You can put it in function and call on body load. 您可以将其放在函数中并调用身体负荷。 you can put the script tags in Head or just before the closing body tag. 您可以将脚本标签放在Head或紧接在正文标签之前。

Instead of using using document.write() you can assign the city to some span or div or any html control. 无需使用document.write(),您可以将城市分配给某个span或div或任何html控件。

<body onload="myFun();" >........
<div id="city" ></div>


<script language="JavaScript">

function myFun()
{
  // document.write(city);
    document.getElementById('city').innertText = city;
}
</script>

For linked resources, I think you need to specify the type, instead of the language: 对于链接的资源,我认为您需要指定类型,而不是语言:

<script type="text/javascript" src="http://j.maxmind.com/app/geoip.js"></script>

Edit: As per comment, yes it would be better to write into a designated output area: 编辑:根据评论,是的,最好将其写入指定的输出区域:

<div id="OutputDiv">No output yet.</div>

You also might want to put your defaults into a window.onload function, instead of running immediately: 您可能还希望将默认值放入window.onload函数中,而不是立即运行:

window.onload = function() {
    //Your code here
    var country=geoip_country();
    var region=geoip_region();
    var city=geoip_city();
    if(country=="")
        country="US";
    if(region=="")
        region="New York";
    if(city=="")
        city="New York";

    //document.write(city);
    var outputDiv = document.getElementById('OutputDiv');
    outputDiv.innerHTML = "City is " + city;

};

This also lets you be certain that you are running the output after the variable has been set (or is supposed to be). 这也使您可以确定在设置(或应该设置)变量后正在运行输出。

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

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