1

I have an html file that has some inline JavaScript in the end of the body where a global variable data is defined. A JavaScript file is included in the head, and one of the functions inside the JavaScript file uses the value in the variable.

Since the file was included before the definition of the variable, the variable in the JavaScript file is returning undefined. but window.variable returns the variable. Is there any general rule I'm missing or this is just a coincidence in my case.

HTML simplified:

<script src='myFile.js' language='JavaScript' type='text/javascript'></script>
<script language="javascript">
    var inLine = "abcde"; 
</script>

myFile.js that does not work (returns undefine inLine):

var insideFile = inLine.length;

myFile.js that works:

var insideFile = window.inLine.length;

In sum I want to know why one of them works and not the other

5
  • @dandavis that's what i felt. This is why I said that it might be a coincidence. Because i was shocked when it did work lol
    – Anthony N.
    Nov 10, 2015 at 1:49
  • 1
    I think you simplified the example too much. Your observation may also be incorrect. Nov 10, 2015 at 1:49
  • 2
    you might have an element with an ID attrib of "inLine", which becomes a global sooner than the end of the body where the other code lives...
    – dandavis
    Nov 10, 2015 at 1:50
  • @dandavis yeah probably. my problem is I can't manage the file includes because the inline javascript is being added by a CMS. And there's a lot of dependencies in the file. So the aim of the question was to know if there is a rule I didn't know of or it was a coincidence (like you said it's being set elsewhere without me knowing lol)
    – Anthony N.
    Nov 10, 2015 at 1:52
  • if my answered helped you click check mark Nov 10, 2015 at 15:40

2 Answers 2

2

Your js file is included before variable is defined and which means code inside the file runs before this variable is defined, So you get the error.

The reason you dont get error for window.varname is also the reason you wont get error for this:

var obj = {};
console.log(obj.a);

when you look up property on object and it doesnt exists Javascript shows undefined.

3
  • but window.inLine.length throws if inLine is undefined/null. you can only go "one extra dot" before it's really missed.
    – dandavis
    Nov 10, 2015 at 1:52
  • It's good to know. In fact it's true that while debugging, inLine.length returned undefined and raised an error, and window.inLine.length returned undefined but didn't raise the error. I have to also mention that the code inside the JS file was inside a function which was called at the end of the file (inLine was declared) so the only problem I had is the error being raised.
    – Anthony N.
    Nov 10, 2015 at 1:59
  • 1
    end of the file, end of html file or js file? Nov 10, 2015 at 2:24
0

In sum I want to know why one of them works and not the other

One approach that could possibly return defined results , without error, would be if defer attribute was set at "myFile.js"

<script src="data:text/javascript,var insideFile = window.inLine.length;console.log(inLine, insideFile)" 
  type="text/javascript" defer></script>
<script type="text/javascript">
  var inLine = "abcde";
  console.log(window.inLine)
</script>

2
  • He's not asking how to make it work, he asked why it worked in the code he posted. He doesn't have defer in his <script> tags.
    – Barmar
    Nov 10, 2015 at 2:08
  • @Barmar "he asked why it worked in the code he posted. He doesn't have defer in his <script> tags." Yes . Though not certain if process that loaded script tags may have added defer attribute ? See OP at "HTML simplified:" , "myFile.js that works:" , "In sum I want to know why one of them works and not the other" , comment at "my problem is I can't manage the file includes because the inline javascript is being added by a CMS. And there's a lot of dependencies in the file." . If js at Question actually did not throw error , attempted to solve for why , how possible Nov 10, 2015 at 2:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.