News for hackers. Stuff that matters.
Re:Responsive JavaScript by criswell on Tuesday February 11, 2014 @07:05PM (#7221563)
I really like Jeremy Keith's approach to "responsive javascript". He uses `:after` and updates the content property based on media queries. It keeps it nice and DRY. Here's a small demo: http://jsbin.com/meqon/2/edit?css,js,output
Re:Responsive JavaScript by kaishiro on Tuesday February 11, 2014 @08:00PM (#7221778)
Yes! Was surprised this wasn't there and was happy to see someone toss it in the comments :)

Edit: How he got there is a neat, quick read for anyone interested: http://adactio.com/journal/5429/

Re:Responsive JavaScript by iMark on Tuesday February 11, 2014 @03:24PM (#7220147)
First time I've come across the matchMedia api. This would have been extremely useful in my last project.

Thanks for spreading the word.

Re:Responsive JavaScript by huskyr on Wednesday February 12, 2014 @04:09AM (#7223288)
In my experience, setting a handler for every onresize event can lead to performance problems if you've got a complex layout. It's better to wrap it in something like Underscore's throttle function (1) and only let it fire every 200ms or so.

1: http://underscorejs.org/#throttle

Re:Responsive JavaScript by gcb0 on Wednesday February 12, 2014 @01:02AM (#7222786)
more things that take the control out of the user. and add lots of complexity for no good reason.

why on earth have javascript to do CSS's work?

and why do you care about screen size changes? the only good thing it does is hint you about device orientation. But who says when i change from portrait to landscape i want a whole new site? sometimes turning a device just mean i want to fit one image better in the screen. not change all my navigation elements to fit the designer dream of multi-screen shenanigans.

yeah, i sound like the old fart who don't like shinny things and change. but people impressed by this sound like the kids adding animated gif after animated gif to their geocities page. now get off my lawn!

Re:Responsive JavaScript by nodelessness on Wednesday February 12, 2014 @01:57AM (#7222981)
Some behaviour may need to be initialised depending on the width of the viewport. For example: I may want to provide a side swipe menu in my mobile version that I do not want to initialise and become available in the web version.

Yes, you don't need a whole new site. But I as a developer may want to provide a better experience with the additional horizontal space I just received in the landscape mode. Or I may want to sneak in some ads on the side.

Media queries go only so far but they cannot help you to create behaviour that depends on the width of the viewport.

Re:Responsive JavaScript by gcb0 on Wednesday February 12, 2014 @05:18AM (#7223508)
Why users on tablet with huge screens should be denied Swype menu?

See you are just adding complexity to development and use of the site.

Re:Responsive JavaScript by phaer on Tuesday February 11, 2014 @04:37PM (#7220637)
"The benefit of this being that we can use the same media queries in the Javascript as we have used in our Javascript."

I guess the second mention of "Javascript" should be "CSS", right?

Re:Responsive JavaScript by matthewgifford on Wednesday February 12, 2014 @02:59AM (#7223127)
The one problem I've found with this approach is making sure that the CSS and JS breakpoints stay in sync as a site is maintained. At my previous job, we came up with a way of changing JS behavior based on CSS changes to DOM objects. http://blog.cloudfour.com/behavioral-breakpoints/
Re:Responsive JavaScript by matthewgifford on Wednesday February 12, 2014 @03:02AM (#7223135)
I don't think it's mentioned in the blog post, but we used csswatch jQuery plugin to watch for changes. https://github.com/leifcr/jquery-csswatch/
Re:Responsive JavaScript by tlack on Tuesday February 11, 2014 @07:00PM (#7221543)
Has any work been done on identifying the browsers connection speed so that we may serve high quality vs. low quality assets? The only thing I've been able to find for this is the MaxMind GeoIP Netspeed database, but I have no idea if the data is accurate enough to be valuable. I suppose you could measure latency, but not during the initial page load..
Re:Responsive JavaScript by matthewgifford on Wednesday February 12, 2014 @02:52AM (#7223107)
When you have a few hours, Google "responsive images".
Re:Responsive JavaScript by wwweston on Tuesday February 11, 2014 @03:23PM (#7220143)
I can see the appeal, but I wonder if this might not be better accomplished with an HTTP header that would explicitly give the viewport dimensions and/or some information about the connection type/speed.
Re:Responsive JavaScript by jitl on Tuesday February 11, 2014 @03:30PM (#7220197)
Viewports can change in size once the page has loaded. A Connection-Quality: GPRS, 512kbps; header would be nice. But then you still get an issue when phones must report or the user toggles on WiFi as soon as they see a few bad (low bitrate) assets.
Re:Responsive JavaScript by hayksaakian on Tuesday February 11, 2014 @06:46PM (#7221448)
Its more simple than that, imagine I rotate my screen.

Do now need to send every possible view port with the original header?

Re:Responsive JavaScript by wwweston on Friday February 14, 2014 @12:38AM (#7237120)
Yeah, suggesting viewport dimensions was a lazily considered and unfortunate choice on my part. Screen dimensions -- probably in particular the larger device width -- would be better, and now that pixel density is an issue, that as well.
Re:Responsive JavaScript by paulirish on Tuesday February 11, 2014 @06:48PM (#7221463)
That's the thrust behind the Client Hints proposal: https://github.com/igrigorik/http-client-hints
[deleted]
Re:Responsive JavaScript by andrenotgiant on Tuesday February 11, 2014 @04:06PM (#7220450)
Hm - I don't think the particular example they used (where the function called by onresize just changes a variable) really needs a clear/setTimeout variable.

I see what you mean though, if the function called by onresize was actually making visual changes to the page, the designer would very quickly figure out they need an clear/setTimeout layer.

Re:Responsive JavaScript by 2bit on Tuesday February 11, 2014 @04:29PM (#7220586)
Wrote a little layer on top of the matchMedia api: https://github.com/twobit/mquery
Re:Responsive JavaScript by Mithaldu on Tuesday February 11, 2014 @03:55PM (#7220375)
Sad to see another site making the mistake of thinking that Opera 12 and below are the same as Opera 15 and above, despite the two sharing literally zero code.
Re:Responsive JavaScript by lucid00 on Tuesday February 11, 2014 @03:13PM (#7220082)
aka "Progressive Enhancement"
Re:Responsive JavaScript by paultannenbaum on Tuesday February 11, 2014 @04:53PM (#7220741)
Nice resource. It would be cool if the author added links to the libraries that are being referenced.
Re:Responsive JavaScript by catshirt on Tuesday February 11, 2014 @04:56PM (#7220763)
the top examples use the native browser API, and there are links to related libraries below.