Dr. Mark Humphrys

School of Computing. Dublin City University.

Online coding site: Ancient Brain

coders   JavaScript worlds

Search:

Online AI coding exercises

Project ideas


JavaScript



Program delivered with web page



Where to put the JS

Client-side JS. Code is sent with the page. Where should the JS code go on the page?
  1. Embedded within HTML:
     
    <script language="javascript" type="text/javascript"> 
    (code) 
    </script> 
    

    In fact this will probably work (and default to JS) on just about every browser:

     
    <script> 
    (code) 
    </script> 
    


  2. In separate JS file containing just Javascript code:
     <script src="file.js" ></script> 

    Q. Why might you use .JS files?


  3. One-liners inside certain HTML attributes. See Event attributes.
    
     <div onmouseover="fn();" > div content </div>     
    
     <button onclick="fn();" > button text </button>      
    
    

    This mixes up JS with HTML, which is a bit messy. See more on this later.




When should the JS be run?




Include remote library

You can include a remote library at run-time:

   <script  src="http://remotesite.com/library/file.js" > </script> 

Q. Why might you prefer to use a local copy?



Document Object Model (DOM)

Treats the page as an object that can be manipulated even after initial download.

How to write to page




Javascript demos

I remove the header and footer to make it easier to View Source.

With these examples, the JS is inside the HTML file.
The exception is the syntax highlighting, which uses an external file.


Demo HTML JS
Javascript page change demo HTML  
Javascript page change demo (Minimal) HTML  
Javascript menu demo HTML  
Javascript environment demo HTML  
Javascript syntax highlighting HTML JS
Javascript demo - doing calculations on client side HTML  
Javascript percent encoder/decoder HTML  
  

JS used on my pages

I have gone JS crazy on my site. Here is some JS I use:
  1. JS to define the Notes for my courses.
  2. JS to include live stats in my header.
  3. JS to highlight Wikipedia links in warning red.
  4. JS graphics for header display and background display.



Is JS safe?

Remote site sending you code to execute is a strange idea.
  1. It does not ask permission to execute. It just runs. Why?
  2. Can it:
    • Access your files?
    • Fill your disk?
    • Crash your computer?

  

What if JS goes into infinite loop?

What happens if a remote site sends you an infinite loop? Can JS crash the tab / browser / OS?

Try this:


        <script>
            while ( true ) { }
        </script>

Or this:


        <script>
            var i = 1;
            while ( i > 0 ) { }
        </script>

  

Loops in Javascript

  


What if JS runs out of memory?

If JS goes into an infinite loop, needing more memory each time, it will run out of memory.
Browsers like Chrome are normally good at making sure only that tab/window crashes, not all tabs/windows.

However, you may discover that a JS coding bug can crash multiple tabs/windows.
Here is what seems to be the reason why:

  

ancientbrain.com      w2mind.org      humphrysfamilytree.com

On the Internet since 1987.      New 250 G VPS server.

Note: Links on this site to user-generated content like Wikipedia are highlighted in red as possibly unreliable. My view is that such links are highly useful but flawed.