Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Trying to clear my HTML code, is it correct if in my web page I declare many blocks of <script> .

Like this:

<script type="text/javascript"> 
  ...
  ...
</script>

<script type="text/javascript"> 
  ...
  ...
</script>

<script type="text/javascript"> 
  ...
  ...
</script>

I mean, I do this in order to group jquery functions and allow to declare variables inside each <script> block that will not affect other <script> blocks.

share|improve this question

1 Answer 1

Your script blocks are not likely having the effect you are expecting, since javascript scope rules do not work that way. Scripts in one block can absolutely affect scripts in another block, if the variables and functions are not scoped properly. Javascript variables and functions are globally scoped unless you follow specific procedures to encapsulate them, and script blocks do not accomplish this.

You should read up on scope in javascript, perhaps a good article or two, and consider why it's best to not embed your javascript functionality in script blocks, but instead place them in well-defined files following common javascript patterns such as the javascript module pattern.

share|improve this answer
    
Just to mention he probably needs a module loader system like require.js or browserify. –  inf3rno Sep 18 '14 at 22:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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