Show HN: TopLevel – A New Way to JavaScript Your HTML
github.com
github.com
/*
<div id="writeHere"></div>
<script src="#"></script>
<div>For example here, we have multiple examples of code that execute</div>
<!--
*/
writeHere.innerHTML='<p style="font-weight:bolder">Hello World</p>'
alert("are we JavaScript files or are we HTML?")
/*
-->
*/
I saw this once upon a time while debugging some nasty code some publishers used - the whole site was in this style. It was one of the most ingenious things I've seen - the same file, parsed twice, once as HTML and once as JS.ps I don't envy anybody who has to spend their day working on code that looks like that!
1. Page is parsed as HTML, ignoring the JS since it's wrapped in HTML comments.
2. script src="#" loads itself.
3. Page is parsed as JS, ignoring the HTML since it's wrapped in JS comments.
Is it because each tag would then be parsed and run in order, ie you couldn't declare a function at the end of the page that you'd use at the top? It's the only explanation that comes to mind...
They were doing something funky with the ad tags that loaded on the page. Clearly they think it will deter people from discovering their naughty misdeeds
The way TopLevel works, is by getting the complete HTML content, putting it trough a javascript parser, and then rewriting it using document.write.
So this requires all content to be downloaded first, and the toplevel.js to be loaded, before a page can be displayed properly. This breaks all the improvements made in browser, and would significantly slowdown page use.
Also, its completely non standard, and a mis-use of js.
Nice hack however ;-)
---
1. Blocks incremental rendering?
Indeed ... for people not TeeWEE, "incremental rendering" means say a 2MB HTML file will be partially rendered prior to the entire payload being transferred. That is no longer the case here. 100% true. As an aside, I'll think hard about how to overcome this.
---
2. The description of how it works?
Correct. Very succinct. Good Job!
---
3. Significantly slow down a page?
This depends on the page. If it's a small-footprint HTML page which leverages templating, then the base page will be quite small.
If the design previously loaded unneeded dependencies which were only applicable to a subset of users, then this provides a very easy way out without requiring lots of technical knowledge - and probably a good speedup to boot.
As far as actual tests, I've refactored a number of personal projects with this library, and the time to where the spinner stops (if I load the two versions side by side) isn't noticeable through my eyes. I can't honestly tell which one is which.
---
4. A "mis-use of JS"?
It's certainly non-canonical - but I'd argue that many "standard" common practices got their start being just as heretical and then were later replaced by standardized ceremonial ways of doing things.
----------------------------------------
I can't tell you how many hours of time I've wasted on non standard "Standard" things simply because it saved some other guy 20 minutes in time somewhere else. It's a nice hack, but let's leave it at that.... try to improve your resume with other methods.
https://gist.github.com/kristopolous/7d68c3c7cec5f9ac5bc3
Lines 35-37 are the change.
Have fun.
I know the use of <plaintext> seems to be essential for the implementation, but I'm not sure that it is a reliable way. Are there any alternatives?
I have yet to get it working as reliably across the same wide range of browsers.
As far as it disappearing; In order for that to happen one browser group will have to take the first step of intentionally breaking legacy webpages - it seems antithetical to the "render as much as possible" features arm-race nature of the browser world.
Then again, I've been wrong plenty of times before.
The HTML interpreter packs up its bags and goes home - you can't "close" a <plaintext> tag if it's existing in a plain HTML page.
However, you can still manipulate it through the DOM.
Isn't this a SPOF? If the toplevel.js script fails to load/execute, the <img> will request "logo_.jpg" which does not exist. (The example implies that "logo_50x50.jpg" and "logo_200x200.jpg" exist.)
instead of JS we used PERL as the inline script language. oh yeah, in the end i wrote a templating language using this templating language until we ditched it for something sane, namely PHP 3 at that time.
We do this sort of conditional rendering with KO in our application and it works great.
It's quite a mature and focused library.
"Works Everywhere And Is 0.8 KB."
Everything is gzip/compressed. It is more interesting to know the real file size. > The stylesheet above will never load unless the branch is satisfied.
Or if the user has JS turned off.Due to the approach, I guess the start and end tags don't need to be <!-- and -->. It can be anything. The only reason they chose them I guess is that it relieves confusion, though I still think the insertion of the comments in the regular HTML tags looks a bit weird.
Cool though. Any performance examples from older/mobile browsers?
Not exactly a general purpose programming language (as XML really shouldn't be used that way, of course), but I think it takes a lot of the ugly verboseness out of most XML templating systems, including the OP's.
Maybe I should post a "Show HN" thread too. ;)