Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact

My big idea: Ancient Brain


CA114      CA170

CA668      CA669      Projects

JavaScript (More)

See explanation.

Some things you can do with Javascript

  1. How to block (or redirect) a link to me from a certain site:
    var referrer = document.referrer;   
    var badstring = "BADSITE.COM";
    if ( referrer.indexOf(badstring) != -1 )
     window.location = "http://mysite.com/badsite.redirect.html";

    Exercise: Re-write the above with includes()
    Test it on your own site, with one page linking to another.

  2. Hide mailto: link from spambots. See example.
    s1 = "<a href=mail";
    s1 = s1 + "to:user";
    s1 = s1 + "id@dc";
    s2 = "u.ie>email me</a>";
    document.write ( s1 + s2 );

    Q. What does spambot need to extract this address?

  3. How to stop another site enclosing me in a frame.
    if (top != self)
     top.location.href = self.location.href;

    • Framekiller
    • It used to be that many big sites (like Google Images) would frame you. But not any more.

Linkis is for sharing pages on social media. It frames the pages that are shared.
A search for Linkis links to tcd.ie reveals the page above.
View source and you see it uses iframe to frame this tcd.ie page.
It adds extra bits to the page, even on top of the original page. It also hides the original page's URL.

Move CPU load onto client

Javascript can do all sorts of calculation on client side without burdening the server.
Good (for both client and server) to move CPU load onto client. Why?

Separate Javascript from HTML

If we label elements of a page, we can avoid mixing Javascript and HTML in Event attributes.
HTML labels the elements but does not assign Javascript functions:

 <input  id="thebutton" name="thebutton"   type="button"   value="Click to run function"   />

Javascript then assigns the functions at startup:

    document.getElementById('thebutton').onclick = fn;

Transforms of Javascript

If interpreted code is run millions of times, maybe it should be "compiled" to be as efficient as possible.

Compile other languages to JavaScript

You can trust that your user has a JavaScript interpreter.
You cannot trust that they have other client-side technologies.

There are schemes where you can write in other languages and compile to JavaScript.


The future of compile-to-JS:
Demo of Unreal Engine written in C, compiled to asm.js and running in Firefox web browser.


Javascript server-side and command-line

ancientbrain.com      w2mind.org      humphrysfamilytree.com

On the Internet since 1987.

Wikipedia: Sometimes I link to Wikipedia. I have written something In defence of Wikipedia. It is often a useful starting point but you cannot trust it. Linking to it is like linking to a Google search. A starting point, not a destination. I automatically highlight in red all links to Wikipedia and Google search and other possibly-unreliable user-generated content.