Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Peter Cooperx http://www.petercooper.co.uk/

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

Cross-browser way to retrieve styles for elements via the DOM

document.getElementById('whatever').style only gives local styles, but if you want those specified in external CSS definitions, you have to arse about (why, oh, why?) and do it like this:

function newGetStyle(nodeName, sStyle) {
	var x = document.getElementById(nodeName);
	var y;
	if (x.currentStyle) {
		y = x.currentStyle[sStyle];
	} else {
		try {
		y = document.defaultView.getComputedStyle(x,null).getPropertyValue(sStyle);
	  } catch(e) { }
	}
	return y;
}

Automatically reload stylesheets on HTML document

Code is from here and described in this great article by dc.

function updateStylesheets() {
	var i,a,s;
	a=document.getElementsByTagName('link');
	for(i=0;i<a.length;i++) {
		s=a[i];
		if(s.rel.toLowerCase().indexOf('stylesheet')>=0&&s.href) {
			var h=s.href.replace(/(&|\\?)forceReload=d /,'');
			s.href=h+(h.indexOf('?')>=0?'&':'?')+'forceReload='+(new Date().valueOf());
		}
	}
}

setInterval(updateStylesheets, 1000);

Dynamic CSS stylesheets in Rails

Found here (with more info). Probably the best new Rails blog around at the moment.

class StylesheetsController < ApplicationController
  layout  nil
  session :off
  def rcss
    if rcss = params[:rcss]
      file_base = rcss.gsub(/.css$/i, '')
      file_path = "#{RAILS_ROOT}/app/views/stylesheets/#{file_base}.rcss"
      @color = '#f77' # example setting
      render(:file => file_path, :content_type => "text/css")
    else
      render(:nothing => true, :status => 404)
    end
  end
end

Zebra stripes on table rows using Rails / RHTML

<% @projects.each_with_index do |project, i| %>
<% row_class = i%2 == 0 ? "even" : "odd" %> 
<tr class="<%= row_class %>">
......
<% end %>


In the CSS:

TR.even { background-color: #f00; }
TR.odd { background-color: #f00; }


etc..

Making bordercolor work in FireFox / Mozilla

IE and Safari support the bordercolor attribute directly on TABLE elements which colors the external and internal borders. FireFox does not, but you can use standard CSS to fix it.

TABLE { border: 1px solid #eee; }
TABLE TD { border: 1px solid #eee; }


Unlike most solutions, this also correctly changes the color of the cell borders.

Nice serif font set for CSS

font-family: "Hoefler Text", Baskerville, "Big Caslon", "Adobe Garamond Pro", Georgia, Palatino, "Times New Roman", serif;

&lt;body> tag CSS ID in Rails

By Ryan Carver of fivesevensix.com. Place in your application_helper.rb and call with <%= bodytag_id %>..

def bodytag_id
  a = controller.class.to_s.underscore.gsub(/_controller$/, '')
  b = controller.action_name.underscore
  "#{a}-#{b}".gsub(/_/, '-')
end
« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS