Posts Tagged ‘XHTML’

Check if at least one checkbox is selected with JavaScript

Thursday, November 3rd, 2011

Scenario: You want to create for instance a quiz or survey. You have x number of checkoxes in your HMTL form with alternative answers to the questions. You have different values to this checkboxes, like e.g. the database id that you need to save the users answer in the db. You could of course create a loop and track which id the checkbox is connected to, but it is most often safer to store the id in the checkbox value property.

Problem:

  • You do not know how many checkboxes  which will be created in a e.g. PHP for-loop with data selected from a database.
  • There can be other form elements as well, you have to filter them out.
  • You do not know the name of the checkboxes so you can not access them with eg. form.checkoxname.value; .
  • You do not know the id, you can not use document.etElementById(‘id’), or it is complicated to get the number of checkboxes without using JQuery’s .serialize();  getElementsById()
  • You do not want to use JQuery for this single function if you are not going to use JQuery for other tasks on your siite.

Solution: Use elements.length. As in form.elements.length or document.elements.length. Use the “type” property to check if it is a checkbox or another form element, like this:

function checkCheckBoxes( form ){
	console.log(form.elements.length);
	for(i=0; i<form.elements.length; i++){
		if(form.elements[i].type == "checkbox") console.log("checkbox");
		if(form.elements[i].type == "checkbox"){
			if(form.elements[i].checked){
				return true;
			}
		}
	}
	alert("You have to select at least one checkbox!");
	return false;

}

In my case I have 1 text input tag, 1 hidden input tag,   and 3 textboxes. This piece of code will therefore output this in the JavaScript  console:

5
checkbox
checkbox
checkbox

Where 5 is the length and it verifies that it is three checkboxes in the if() test inside the for loop, and set the for-loop to loop at  form.elements.length;, We can use the same if() test to filter all other elements than the checkboxes. So the final code will look like this:

function checkCheckBoxes( form ){
	for(i=0; i<form.elements.length; i++)
		if(form.elements[i].type == "checkbox" && form.elements[i].checked)
			return true;

	alert("LYou must select at least one alternative!");
	return false;
}
The <form> tag looks like this:
 <form name=”options” method=”post” action=”form_check.html” onsubmit=”return checkCheckBoxes(this)”>
This is a pretty small piece of code, but it saved my day when I finally found figured it out. I thought that it might will be useful for others as well. Feel free to use it as much as you want.
Cheers!

Flash and SEO

Monday, May 12th, 2008

Flash and SEO has been quite a problem and the main argument for the people in the business who is against Flash. I’ve planned to do something about this for a long time and finally I found a project that was suitable to try this. First I will say that many people think that making a website completely in Flash is craziness, and very often I do agree. However sometimes the client wants a “flashy” site with lots of animations and stuff and how am I to deny them that?

Since the introduction of SWFObject it’s been quite easy to provide the user with html content for those who does not have flash installed, and this is what I’ve done. Since I use amfphp a lot and also for my little CMS for flash, all the content except from the images are stored in a mySQL database. This makes it easy to extract all the content and echo it out in the flashcontent div tag.

I did this for a project called moelo.no and after a while Google index the whole site. To make it even more search friendly I used Apache Rewrite Engine to replace the query string URL with more search friendly URLs. I was surprised how easy easy it was to write the rewrite rule. Here is the all the lines I had to write to make it work:

RewriteRule ^kategori/([0-9]+)/underkategori/([0-9]+)/prosjekt/ ([0-9]+)(.*)\.html$ index.php?KategoriID=$1&UnderkategoriID=$2&ProsjektID=$3
RewriteRule ^kategori/([0-9]+)/underkategori/([0-9]+)(.*)\.html$ index.php?KategoriID=$1&UnderkategoriID=$2
RewriteRule ^kategori/([0-9]+)(.*)\.html$ index.php?KategoriID=$1

Now basically what this does is rewrite a url that looks like this:

http://www.moelo.no/index.php?KategoriID=1&UnderkategoriID=6

to this:

http://www.moelo.no/kategori/1/underkategori/6/

Then I add a suffix that takes the title of the article as filename and adds a .html extension to the end to to the URL to make it even more search friendly. The rewrite engine rules just ignores this. Now the URL looks like this:

http://www.moelo.no/kategori/1/underkategori/6/boliger.html

The next trick was to actually show the right content in the flash version as well when a user clicked a link in Google. I did this very easy by passing the arguments to SWFObject and this way I could load them as variables in flash:


var so = new SWFObject("http://www.moelo.no/index.swf", "index", "1000", "600", "8", "#ffffff");
so.addParam("quality", "best");
so.addVariable("kategoriid", "4");
so.addVariable("underkategoriid", "15");
so.addVariable("prosjektid", "122");
so.addVariable("forsiden", "");
so.write("wrapper");

Finally I added a rule in the robot.txt file that blocks the SWF-file from all robots.

It works like a charm!

The next job is to rewrite my hole flash based CMS into OOP because the hole shit is written in procedural code. This happened because it started as a minor project a long time ago and it has escalated for each project, and this makes it a pain in the ass to update and maintain.

Valid (X)HTML code and SEO

Tuesday, February 19th, 2008

I’ve had several discussions about creating websites with (X)HTML that validates according to W3C’s specifications. This discussion very often arise when SEO is discussed and I’ve often been thinking about the motives of the people who claims that a site with a 100% valid code is better optimized for search engines than website that do not validate. I’ve never heard any good argumentation for this. It just seems that some xhtml evangelists like to decorate themselves with a shiny “Valid XHTML”-bagde.

It has been done quite a lot of testing on valid vs. not valid code and Google and here are two of them:

1 – “Google actively ranked the websites with invalid HTML higher than the websites with valid HTML. Google even refused to rank one of the valid HTML websites altogether.”

2 – Well – the result is clear. From these 4 pages Google managed to pick the page with valid css and valid html as the preffered page to include in it’s index!”

This two tests points in two different directions. It looks like Google is pretty much unpredictable when it comes to W3C compliance.

Let’s check what Google thinks about valid code on their own website? Holy crap! 51 errors on a website with such a simple layout! Doesn’t look like Google care much about W3C validation at all!

Philipp Lenssen interviewed Matt Cutts at Googleplex:

“In more general terms, what do you think is the relationship between Google and the W3C? Do you think it would be important for Google to e.g. be concerned about valid HTML?

I like the W3C a lot; if they didn’t exist, someone would have to invent them. :) People sometimes ask whether Google should boost (or penalize) for valid (or invalid) HTML. There are plenty of clean, perfectly validating sites, but also lots of good information on sloppy, hand-coded pages that don’t validate. Google’s home page doesn’t validate and that’s mostly by design to save precious bytes. Will the world end because Google doesn’t put quotes around color attributes? No, and it makes the page load faster. :) Eric Brewer wrote a page while at Inktomi that claimed 40% of HTML pages had syntax errors. We can’t throw out 40% of the web on the principle that sites should validate; we have to take the web as it is and try to make it useful to searchers, so Google’s index parsing is pretty forgiving.”

(the whole article is here)

Please understand me correctly, I do mean and has always meant that valid code is what we should strive for, but let’s not get nazi about it. I think that this quote sums it all up pretty well:

“Creating valid templates should be a path, not a goal. The idea is to make your template as accessible as possible for humans and spiders, not to achieve a badge of valid markup. “