Why people don´t make scriptswitcher

A lot of people is talking about an unobtrusive javascript. OK, but I don't understand why they have not came with idea to add users some possibility to switch off javascript on sites. It shouldn't be to complicated, if your scripts are really unobtrusive.

In case that of onobtrusive js:

  1. site work completely without script

  2. script is in your head section

  3. if you are clever all are in one script

  4. even if you need script at the end of your body tag you can add it into one single script

Anyway you can never know, that in some end user browser is problematic with your script. So why donť let user to simple switch them off without complicated browser settings.

How to add script to another script

Bellow is simple example how to include scripts in another header script

function loadAnotherScript(){
//use some onload event

This technic will allow you to keep all the scripts in one file and in combination with php you can any time send maximum two scripts for a site. One header script and one through this header script.

How to send php as js?

Again very simple thing.

header("Content-type: text/javascript");

Why to send php as js? Because than you can split javascript to small snippets a put them together on final site. Try to study this keywords dynamic css with php on Google to see what I mean, because it again seems no one do such things with js.

Final Scriptswitcher, what I am talking about.

My php and english language are not good, so sorry for that. For this reason I change someone else styleswitcher code, which looks very simple for this demonstration and can be found at a lot germans phorums. So code is here:

setcookie("scriptsrc", $js_name, time()+(60*60*24*30), "/scriptswitcher/", ".xy.wz.cz");
echo "<?xml version=\"1.0\" encoding=\"windows-1250\"?>\n";
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1250" />
<script type="text/javascript" src="<?php echo $js_name?>.js"></script>
<form action="<?php echo $_SERVER['SCRIPT_NAME']?>" method="get">
<select name="scriptsrc">
<option value="site" selected="selected">Default</option>
<option value="none">None</option>
<input type="submit" value="Switch Script" />
<li><a href="index.php?scriptsrc=<?php echo $js_name?>" title="Script switch">Script switch</a></li>


Javascript Scriptswitcher

So I thing here is a long way for scripters to make they site really unobtrusive, and I hope someone more experienced in js, php and english will rewrite this idea to real english and explain everything what I forget to.


Anonymous Hassan said...

You are rightfully preaching unobtrusive javascript, but the last code example of this post is a perfect example of obtrusive php! (or html, depends on your view point).

Preach for clean coding on the server side too!

3:13 odp., února 27, 2007 
Blogger Mirek Komárek said...

I see, I am not php coder. I change it little bit, but I am not sure if it is better. Result is in new post.

4:21 odp., února 27, 2007 


<< Home