最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Scope of javascript variable declared inline - Stack Overflow

programmeradmin1浏览0评论

Here are the problem scripts:

This is from the HTML file:

<script type="text/javascript">
    var devices_record = "some string";
</script>
<script type="text/javascript" src="/js/foo.js"></script>

This is from foo.js:

function bar () {
    devices_record = "assign new string";
}

The error report by HttpFox is that devices_ record is not defined. What gives? I thought devices_ record will be a globally scoped variable so it should be accessible from anywhere.

Here are the problem scripts:

This is from the HTML file:

<script type="text/javascript">
    var devices_record = "some string";
</script>
<script type="text/javascript" src="/js/foo.js"></script>

This is from foo.js:

function bar () {
    devices_record = "assign new string";
}

The error report by HttpFox is that devices_ record is not defined. What gives? I thought devices_ record will be a globally scoped variable so it should be accessible from anywhere.

Share Improve this question asked Aug 27, 2009 at 18:01 Salman HaqSalman Haq 3403 silver badges11 bronze badges 1
  • 1 You should definitely be able to access devices_record. I think you are missing something else. Is this the exact code OR you have created a sample to illustrate the problem? – SolutionYogi Commented Aug 27, 2009 at 18:16
Add a ment  | 

3 Answers 3

Reset to default 3

Your test case works for me. Here is my plete test:

foo.js:

function bar () {
    alert(devices_record);
    devices_record = "assign new string";
    alert(devices_record);
}

foo.html:

<html>
<head>
    <script type="text/javascript">
    var devices_record = "some string";
    </script>
    <script type="text/javascript" src="foo.js"></script>
</head>
<body onload="bar()">
</body>

I get two alerts, the first says "some string", the second "assign new string".

Your problem exists elsewhere in your code.

I believe your given code works. At least executing the code you give above will not cause any errors.

Also, it's better to include your dependencies such as foo.js before using any inline js. This ensures your library is loaded before calling functions provided your library.

Also, assignment operators will not cause 'devices_ record is not defined' errors because you ARE defining it with the assignment operator.

Your error is probably caused by something else.

Declare devices_record in foo.js. Then, reference it from the javascript embedded in the HTML.

You may also consider wrapping your foo.js code in a class, with devices_record as a field/property. This can make for much easier error trapping, among other things.

Tidbit: also, if devices_record declaration were placed before the script inclusion, the script may be able to access it. However, be wary of declaring variables outside of the file that uses them. If you decided to include that script in a page that forgets to declare devices_record, you'll run into errors.

发布评论

评论列表(0)

  1. 暂无评论