You might not need JavaScript
youmightnotneedjs.com
youmightnotneedjs.com
[1] I regularly nerdgasm at LibreOffice releases reading how many thousands LoC they removed from the code base.
ps: beside code golf, I think there should be a patch golf thing. Try the smallest changeset that fixes or add a feature.
[1] also, git surgically precise patch mindset helped too.
"Strive to optimize for less code, and the minimum level of abstraction."
That should be the appropriate level of abstraction. The appropriate level of abstraction being that which requires the least code to implement and least effort to reason about.
With some effort, the relevant jQuery methods that act as polyfills could be extracted.
However, a minified version of core jQuery is < 20Kb. A very low technical debt trade-off to support 99% of browsers.
Reasons to include inefficient code for compatibility reasons might include applications where maximum reach is valuable, or where the scale is so large that the "infinitesimal market share" percentage plays out into thousands of end users.
Did anyone get fired for that bit of programmer malpractice?
https://hackernoon.com/how-it-feels-to-learn-javascript-in-2...
Perhaps the right answer is to code know the basics but also have a handle on the popular frameworks too.
Yes more likely. In fact I would find informative to see the framework that you have built as an example of understanding web development.
To expand a bit I think learning the framework is easier once you have an understanding of the web platform's basic. For non web stuff I value more problem solving ability, general CS knowledge over specific technology knowledge. I usually don't set a language requirements in interviews and look more to someone being able to explain how the code they wrote works and why it's a better solution than say some inefficient brute force method. Which in my example when applied to web stuff would translate to why is implementing something this way in angular better than an alternative, what advantages did you gain from angular. Etc...
The code that uses it looks like this: https://github.com/serprex/openEtG/blob/master/views/MainMen...
The site looks like this: http://etg.dek.im
I recently worked on a site at my job where I was able to implement it with JS + C#/OWIN, all static html, only uses toastr/jquery. Was nice they let me break away from the illusion that the choice is only between WebForms or MVC
If you ask me to compare this to Angular, I'd have to say I've never used Angular so I don't know
But in my view, building your own framework first and then learning a standard equivalent gives you a much better understanding of the tech of the new framework which will pay dividends. For instance, I built my own PHP framework in 2001, and subsequently played around with Drupal/Django/other homegrown frameworks. When Rails dropped in 2004 I was immediately ready to jump on board because I had been through the same mental process as DHH had when he started building Basecamp in Ruby after having been frustrated by PHP. Whereas a lot of people complained about Rails' magic and lack of transparency, to me it was very easy to dive into the source code and understand the reasoning that led to its architecture.
Of course these things calcify over time, and you don't need to necessarily go very deep into every layer of the stack, but you have to at least be competent in a few layers or you'll be helpless the minute you hit a problem with a leaky abstraction. But in the case of JS, yes I think any time spent learning core JS is well-spent, and knowing only React/Angular is a real liability as a front-end engineer, especially because those frameworks are designed to solve heavy SPA problems which are only a tiny subset of the type of interactive web pages one might want to create.
Convention makes code readable and maintainable. A framework is essentially shared convention that you don't have to maintain. Making your own framework shows that you understand the value of convention.
A framework means that spaghetti code can only get so bad. A bad ad-hoc implementation of a web platform is an order of magnitude worse than bad code written on top of a popular web platform.
When I look at ugly React code, I know how to extract logic, where to push it, how to identify poorly-named concepts. When I look at ugly vanilla JS, I don't know what to make of anything.
Consult MDN (Mozilla developer network) and read their articles
I've never been able to fathom how anybody can do that without understanding the foundation that it's built on. At the bare minimum, how the heck do these people troubleshoot errors? If you have so much as a typo in your code, Angular is going to give you a completely cryptic jQuery error message.
It's also partially an artifact of JavaScript's lack of namespaces and scoping rules and prototypical inheritance because these make it easy to make a class of errors that are conceptually difficult to understand without holding a complex mental model in my head...and that's after standardization of browsers reduced the number of arbitrary rules that could cause bugs.
It would be cool to see the before (with AngularJS) & after (pure HTML) of this.
Nice example.
Some of these are cool examples. Others, like the modal, won't scale very well because now you can trigger modals by tabbing (breaks keyboard navigation) or most of the form examples as you'll likely hit many endpoints that require JSON (which yeah I know "you're making a web app why can't your endpoints accept a form?" but as we all know it's never that simple and you might want to provide different behaviors for, say, showing a progress bar or disabling certain features during the submissions or a THOUSAND other things).
I get axing dependencies. I really do. I wrote msngr.js entirely in ECMAScript 5 using zero dependencies so I'd have the most flexibility possible. But at the same time overcomplicating your code to the point where it's breaking standard practices or making it un-maintainable simply to axe a dependency is the wrong way to go.
But by all means keep making cool stuff that shows off how versatile CSS is! Just don't expect to use all the CSS tricks you learn in a production environment.
Regardless my other points still stand.
My point was hijacked and contorted here.
Talk about a tangent over a tiny, unremarkable comment that was meant to be light hearted.
Also, the examples don't support a lot of use cases... the accordian and tab examples, while interesting will submit goofy information if they're containing multi-part forms.
The image changer will break other bits of navigation, or similar changers by abusing the target. Similarly, the lightbox example doesn't have a slideshow support, and even if so, would probably be as/more complex than just using JS.
Also, how many of these examples work in IE11? This browser will be around a while for public facing sites, and has some really broken implementations of newer css features like calc (which doesn't work in several scenarios).
What is bad is code that's hard to maintain. When I did a stint of front end development recently, I went in with the mindset that "JavaScript is evil for the user and therefore should not be used when avoidable". I discovered after a while that although you can replace JavaScript with CSS sometimes, the resulting code is often much more difficult to understand and modify than the original JavaScript would have been. It's no longer in my interest to avoid JavaScript at all costs, as was the case before.
You touch on this with your "overcomplicating" sentence, but I feel like it didn't get the spotlight it deserved in your comment!
These "you"s are rhetorical BTW, I'm not addressing kqr directly.
We-e-ell, it's not a good language. But it's what we have.
I agree, though, that it can be used well and does have a place in writing good web pages and sites, but that when it is used to create horrible 'web apps' it is being used for evil.
If you just take your browser's defaults for all the typographical stuff like that, everything looks like trash.
The only things I may agree with is https and zipping.
"Fits on all your shitty screens" Does mean that it FITS ON ALL YOUR SHITTY SCREENS. Mine is a 1920x1080 (which is the minimal nowadays) and the 2nd and third iterations DO NOT FIT IT.
That thing around the web page, it is called a fucking window. It resizes. I'll do that if I want your fucking column-format.
14 style rulesets.
Most examples are equally compact in plain old CSS. In fact, many of them are just plain CSS displayed in a window labeled "SCSS".
The places using SCSS seem like they're simply being more sane in allowing a user to change numbers trivially. Instead of saying "just change all the 400px throughout to 500px", one can change the `$slide-width` definition in one place (usually at the top).
There's also the style preference of nesting to group. But neither of those make it a strict dependency.
---------
EDIT: I'm not saying that I disagree with your assertion that CSS is a bad place to layer logic from a maintainability standpoint; I'm more pointing out that blaming it on SCSS makes no sense in this context.
I don't see where I blamed anything on SCSS just found it amusing the page purports to axe dependencies (JavaScript) but uses SCSS. The rest of my post is far more on topic and interesting. Everyone is focusing on my SCSS poke though, heh.
Re: requirement - there I was referring to your opening:
> So let's show off what you can do without JavaScript...but also include SCSS as a dependency when the opening paragraph complains about a JavaScript dependency? Madness...
And explaining that although they used SCSS, it certainly isn't a requirement, nor even much simpler than the plain CSS.
I'd guess that most of the responses probably focussed on it because leading statements are often read louder than what follows them. They set the mood/tone in a way.
For example, the modal that appears when you click on the button actually doesn't do things on "clicking" but focusing. This means if you tab over the button it'll load the modal, then tabbing away means the modal is gone. I can't tab into the content in the modal at all. How does that work with accessibility?
Though I generally dislike the idea of saying content isn't there by making it present in the document but invisible.
Edit - Some of the default behaviours are also a bit pants. In chrome, the form validation on a pattern starts working after I hit submit, then angrily shakes at me for every keypress until it's valid. Then, it ignores invalid input as I type more! Please never use this for phone numbers or credit card numbers. Stop requiring an exact format when what people type varies, and stop telling me I'm putting in something wrong when you ask for a phone number and I give you something that'll connect to me if you type into a phone.
Maxlength is not a validation it's just ignores any more letters you type. Quietly ignoring your users input is probably not what you want.
The "Would you prefer a banana or a cherry?" just shouts "Please match the requested format" if I type "a cherry". I know chrome has nothing else to go on other than "the regex wasn't matched" but it's a bad end user experience.
The tabs example is something I need to tab onto then use left and right, I assume because it's a radio box underneath, not a series of links. Are tabs really radio buttons?
The same for the accordion. Which I've found if I add any tabbable content inside them then I'm focused on a hidden item. Great. Tab onto "tab 1", hit tab, focus has disappeared and I'm now potentially going to click on random items I can't see. This is because although you want to pretend the content isn't there just because it's not visible, it's still there! It's in your document.
Sure, you might not need javascript. But maybe you should still use it.
Localized custom validation messages for the provided pattern should be added as part of HTML spec. Someone should follow up on that, because it'll probably be too much of a waste of time for me.
In the meantime, this works and gets around a bug in FF: http://stackoverflow.com/a/20189012
<input type="text" id="username" required placeholder="Enter Name"
oninvalid="this.setCustomValidity('Enter User Name Here')"
oninput="setCustomValidity('')" />
where setCustomValidity could provide any text, even localized text from some other source like a DB, potentially, since it's JS. Requires a modern browser, though.> Sure, you might not need javascript. But maybe you should still use it.
Yes, but if you are writing for a general audience, make sure it's accessible: http://webaim.org/techniques/javascript/
Someone should put together a site that, given a set of browser restrictions the user provides, gives examples of each of these to show the most accessible and standards-compliant version of the code to use, minimizing external libraries required, with and without localization, because not everyone has the resources to provide localized content, unless localization could be provided by a built-in library in the browser or OS, which would be another great project.
Yes, there are sites that provide a matrix of supported browsers, but I don't think that would be as simple to use; developers like copy-and-paste examples, for better or worse, e.g. StackOverflow.
That sounds like a reasonable addition. Perhaps a series of validators and messages?
> Yes, but if you are writing for a general audience, make sure it's accessible: http://webaim.org/techniques/javascript/
Very true. Thanks for the link.
> Someone should put together a site that, given a set of browser restrictions the user provides, gives examples of each of these to show the most accessible and standards-compliant version of the code to use, minimizing external libraries required, with and without localization, because not everyone has the resources to provide localized content, unless localization could be provided by a built-in library in the browser or OS, which would be another great project.
This is something I'd like to see, because apart from a few things I know to look for (tabbing, for example), I don't know how well various devices and settings do with different content. Consistent approaches also help people working on things like screenreaders.
I don't see any features of this site which should require javascript, so they should probably "practise what they preach".
1. Classify each token (whether this is building up an AST or doing something more fuzzy/clever is immaterial for this discussion); 2. Construct a DOM tree (HTML) that is styled (CSS).
Step 1 does not have to happen client-side at all. Step 2 doesn't require JavaScript at all.
What's the lazy reason for not rendering that before serving the html page.
The validation examples and input types are a case in point. Even on browsers where they work, the alerts can't be styled and the behaviors can't be controlled.
It's not clear to me whether that's a problem. Isn't consistent UI a good thing? Isn't it faster to fill the forms correctly if alerts always appear in the same way?
My bad.
Do you know of any good reading material concerning these form validation popups?
From reading on MDN [0], they seem to be browser specific - like the Chrome connection tab [1] - but somewhat customisable via JS.
[0]: https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Form...
[1]: https://blog.digicert.com/understanding-the-google-chrome-co...
You should do the rest on the backend anyway.
Is it from the browser? Can someone explain?
Who goes first? Not me!
> You should do the rest on the backend anyway
Well yes, you should validate on the backend, but you shouldn't wait to only validate on the backend.
This works well enough for getting things done quickly, but it's hardly a good UX, and a far cry from "perfect enough," whatever that means.
Why? Because some users, such as those who use Tor, may have legitimate reasons to disable Javascript execution while browsing. Another reason is that Javascript-generated content can screw up your website's SEO when crawlers visit your site.
Using Javascript for your form validation is okay if your form will still work with Javascript disabled. Yes, the user will lose some functionality of not seeing pretty validations, but it's okay if they can still use your site. [1]
Where JS becomes a problem is when it's used in ways where disabling it completely breaks the page... Single page web apps: I'm looking at you.
-----
[1] One way to improve the experience of form input validations for Tor users could be to have both a JS runtime validation and server side validation of your form inputs, and then put the validation messages that both would generate in the same place so that they stay in sync.
> Where JS becomes a problem is when it's used in ways where disabling it completely breaks the page... Single page web apps: I'm looking at you.
Here's my point: if you disable Javascript and navigate to a URL where a single-page app is hosted, the page isn't broken, your execution environment is. If I make a React app, then ipso facto I'm making an explicit choice to exclude folks that don't want to enable Javascript.
HOWEVER
> I think a more important point is that you should design your website to still be functional even if Javascript is disabled.
There's absolutely room on the Web for all three models: document, interface, and application. And despite my disagreement with this point when it comes to client-side applications, I definitely agree when it comes to Web documents and Web interfaces to server-side applications. (Rails muddied the water something fierce on that last one, which has been the source of uncountable headaches for me.)
If you're building a game or something interactive, I get it. But many SPA's are used to give database-backed forms a pretty skin. It's the overuse of Javascript in this way, and the lack of graceful degradation for forms like this when Javascript is disabled, that I think is beginning to create concern.
True, but irrelevant to the issue of "can you do X on the client side without Javascript?". I can imagine a Javascript form validator that somehow makes the form unusable with Javascript turned off, but I've sure never seen one.
And server-side validation is website 101 stuff that you should always be doing regardless of the front-end technology, not a counter to the many good uses of front-end validation.
Your Back End should be validating the Front End. As it is possible for people to make their own HTTP Post messages to your Back End. This is actually fairly trivial to do if you can use wireshark+curl/hippie/wget.
Though, this site is cool in showing the power of CSS, I am not sure if there is any specific advantage of using CSS over JS to design tabs, sliders, etc. Just use the best tool for the job!
Avoiding Javascript is an advantage if you want your website to be accessible to Tor users.
If your website only handles basic things like form inputs and it breaks when Javascript is disabled, then you have not made it accessible.
That aside, these are some pretty cool examples. Are there ways of doing this with CSS which doesn't screw with my browser back button?
[1] https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_v...
You update the value where your color variable is defined, and then each style declaration that uses the color variable receives the new value automatically.
Using HTML/CSS hackery where you should be using JS instead creates an anti-SEO, non-semantic mess that causes headaches for others to work on and nightmares for anybody needing to extend or modify functionality.
Don't get me wrong, I love pure CSS solutions and try to use them as much as possible where it makes sense. But sometimes, it definitely doesn't make sense. JS isn't something users or developers should be scared of anymore, it's much better supported and much less intrusive than it used to be.
It's fun to see what you can partially achieve without JavaScript, but there is no shame in using JavaScript sensibly to enhance the usefulness of a web site.
It's understandable, though, because we are not imgur's customers: its customers are advertisers, and if we have JavaScript disabled then imgur's customers have less ability to violate our privacy. Thus, it's in imgur's best interest to encourage us to reduce our privacy & security, so that its customers may have their way with our browsers. I can't see that the imgur programmers would care about the fact that they can do amazing things with CSS.
But recently I've discovered that Target's website simply refuses to display products without JavaScript. This is insane: Target are, presumably, in the business of selling things. They ought to want to display a picture of their item and provide me a link to buy it; that's in their best interest. Building that sort of website is easier than building the sort of ecommerce site which cannot even display an image and a link without JavaScript. It should be in Target's best interest to do the right thing.
But it doesn't. Its programmers ought to be interested in a site like youmightnotneedjs.com, but are they? If they didn't bother to build their website properly before, is this likely to encourage them now?
I expect my back button to open the previous page, not to move an image slider.
I like the idea of using CSS over Javascript where it makes sense but in a lot of these examples it feels hacky.
I still use Chrome for development, though.
Safari, is not as bad as IE6 was and anyway no professional front-end developer would make a website that is not working properly on iOS Safari, so it's kinda obvious it will stay supported for a long, long time.
So I would consider what the OP is doing with the CSS, something that might also work on Safari, but with some polyfills.
Chrome/Google has terrible/no support of many browser layout & interaction features, such as CSS Regions & scroll snap points.
Also, Safari is just a much better browser than Chrome. We need to make sure people stop using Chrome and move to the default browser in their environment - Safari & Edge.
If you support the default browser, then you're going to be fine.
Hi, I'm Linux.
( By the way lynx is perfect to pierce through a lot of paywalls. )
The alternatives are Opera and Firefox. Everything else is like coding while wearing mittens.
It gets tedious switching the default browser setting over so I tend to just use Chrome for everything.
Not sure what old wives tales you have heard, but actually Safari was the first browser to 100% support ES6.
And compared to Chrome, it uses 1/3 less battery...
- Apple didn't attend EdgeConf 2015
- https://webkit.org/blog/ was not updated often enough
- Safari 9 had a seriously buggy IndexedDB (fixed in Safari 10)
I'm not the only one who think Safari is a joke compared to the competition. While IE6 was much much worse than what Safari is today, I feel like history is repeating itself again. And ES6 support is irrelevant when the main goal is to make sure websites renders and behave correctly, which is more painful for Safari than the other browsers.
I still have flexbox issues on Safari https://bugs.webkit.org/show_bug.cgi?id=136041
How does that opinion survive reality, e.g. Safari being the first browser to reach 100% ES6 compliance?
>I still have flexbox issues on Safari https://bugs.webkit.org/show_bug.cgi?id=136041*
So? All browsers still have Flexbox issues. Here are Chrome's:
https://bugs.chromium.org/p/chromium/issues/list?can=2&q=com...
Notably Fetch API and WebRTC for my use-cases.
Hopefully they'll figure it out eventually!
It's just my anecdotal view, but I believe that Chrome might be better for developers, but websites are made for the users, not the developers, and Safari is far superior on that front.
I can only assume you never went through the pain that IE6 delivered... It has been some years now but I still get nightmares about it. It literally made me question at one time if I didn't made the wrong choice to focus on web development.
Apple may be dragging its feet when adapting new api stuff, but the situation is by no means comparable as what the situation was with IE6. Not by a million miles.
FTFY :)
Also, if you want the benefits of inline styles, but need media queries and the like, check out Radium: npm.im/radium
The slider has the problem that it cannot dynamically size with the container width, and the amount of slider elements has to be known at compiletime (width: n * 100%).
File upload has the nastiness that you can not really independently style the inputbox with the filename and the button. Chrome has input[type="file"]::-webkit-file-upload-button, but I am not aware of a Firefox/IE solution. Also, you cannot combine the input[type="file"]::-webkit-file-upload-button with a general button selector in the same CSS rule, because Firefox doesn't know the ::-webkit-file-upload-button and discards the whole rule as invalid - you have to double declarate your button styles if you want uniform button look in Chrome. And, even on Chrome, you cannot visually separate the inputbox from the button (e.g. I have a 2-col layout, 320px/col and 16px margin, and I only can mess around with width, padding and margin instead of a clean separation).
To the people who whine about SASS: you can ALWAYS write the resulting CSS by hand, it just takes a boatload of manual typing.
I was surprised to see some Linux-style color picker pop up. It looks a lot like the one GIMP uses. Wonder if Firefox implemented that or they depend on something that does, and in the latter case, what's that dependency?
Doesn't mean you should bruteforce everything with just one tech. CSS is hard to maintain.
Preprocessors do nothing but splinter the CSS scene by layering odd syntax over an already creaking/complex CSS spec. Got nothing against compiling CSS in general - postprocessing features related to current and future CSS specs with PostCSS is a great idea. But dividing the community's attention between multiple competing preprocessors while it should have been concentrating on the spec is the worst thing to have ever happened to CSS.
Great things happened to JS when people got behind tools that pushed the language forward while keeping the current and future spec in mind (ES6/Babel). Shame CSS took an alternative approach imo.
Kinda like when Dante wrote in Latin about the utility of the Italian language.
Often you're better of to do a mix of both, because some things that are very complicated in CSS are two-liners in JavaScript.
And vice-versa, in modern browsers! Best to have both in your toolbox, definitely.
Maybe you can include a few lines of utility code, or a mixin, and forgo the requirement. If you're only targeting more modern browsers, you might not need anything more than what the browser ships with.
This site is fully copied from youmightnotneedcss.com, an excellent resource for vanilla CSS created by @somebody and @somebodyelse. But this time, we take a look at the power of modern native HTML and g as well as some of the syntactic sugar of DuJour. Because, you might not need scripts for that task at all! (Note: these methods can all be accessible, but the demos may not be. Please take a moment to test these before using in production)
I have some good news: http://intercoolerjs.org
You can add AJAX to your application without writing any javascript code, and you'll get to use REST (with real, honest to God HATEOAS) without even thinking about it:
http://intercoolerjs.org/2016/01/18/rescuing-rest.html
http://intercoolerjs.org/2016/05/08/hatoeas-is-for-humans.ht...
I understand why people try so hard to do without JS, but if you want to implement certain UI features, you're SOL. Either give up on them, or embrace the pain.
Why do people try so hard not to use tools that make their life easier?
You might not need a car - you can walk. But it makes life easier and it just works!
Assuming you're more proficient in JS than CSS ;) With CSS3 I find some animation is easier w/ transitions and keyframes than JS.
Note the absurd metric (lines and spaces) to justify how the blown-up version is considered to be smaller. Also note that the article consequently talks about "Node code" even though they are talking about client-side JavaScript.
From the article:
#gulp {
color : #0000ff;
padding : 10px;
}
This CSS takes up 4 lines, and at least 13 spaces. [...]
document.getElementById(‘gulp’).style.color=‘#00f’;
document.getElementById(‘gulp’).style.padding=‘10px’;
This Node code takes up only 2 lines and there are 0 spaces.
Essentially, we are saving lines, spaces, and files (delete
your .css documents!).Also, she called CSS "California Style Sheets" and called JS DOM manipulation "Node code".
Also, the header was obviously done in MS Paint.
Also, it's Jenn Schiffer.
I would honestly be interested in hearing from some people here about how you would go about writing a proper test for these examples. And if the answer is "webdriver screenshotting", I might just shake my head a bit.
I've never heard somebody say that. Can you elaborate?
It's the only reasonable cross platform client/server language which is well supported.
The image slider for example can't be paused. There's the CSS property "animation-play-state" which could have been used to do this, but that literally freezes the animation, whereas you typically don't care about the animation but don't want the slider to move on to the next image (i.e. finish the transition, then pause).
And of course depending on which browsers you're targetting, many of these are horrendously broken for most of your users.
>Codepen requires JavaScript to render the code and preview areas in this view.
- The modal dialog example
- Color picker
- Form validation
[0] http://gs.statcounter.com/#mobile+tablet-browser-ww-monthly-...
But why oh why does it not validate before I press submit? Is that a Chrome specific thing?
* If you hate your users
Seriously, after some minimum complexity the ability to create functions and loops its extremely important. As in you don't need a spoon and a knife to feed yourself, but it sure fucking helps.
If this simplifies the codebase, and gives a more uniform user experience, it's harder to argue against.
That giant ball JS that is included in most sites is absolute nonsense and is not needed in 99% of the cases. We include complete libraries where is should only be a portion.
I'm aware that this is changing, by "compiled" JS - putting together only the required elements - but usually those still have massive overheads.
In my opinion, you should use the tools for what they are the best for. Provide a static, readable HTML first from a backend code - that will run on anything, anywhere; it's accessible, and you've served the main purpose: serving content^1. If you need, add the styling and the animation with CSS. Only after that add the JS layer, which, in my opinion, should mostly be used for data exchange and change triggering.
^1 There are exceptions, there always are; application within the browser, where the main purpose differs from the content delivery. In certain numbers, such as domains, these are the minority; in actual use, it's a different story.
( Imagine a world where the data exchange is not tied together with JS and that we could but other UIs in front of the backends of webapps.. )
Surely they could have figured a way to do without them.