Thursday, March 26, 2009

Closely coupled code Vs Loosely coupled code

I saw many of the users don't know meaning of this and there differences. I would like to post it here for the users.
=>

Basically, closely coupled code means that one part of the code is strongly dependent upon another part. This means that any change in one part may have unforeseen/unwanted effects on another part.

Ideally, loosely coupled code allows for separate parts of the application to not have that strong dependency on other parts. This has a number of benefits, such as keeping bugs isolated to one area of the code (instead of having cascading effects throughout the code) and it makes it easier to re-use portions of the code.

e.g. when you use "global" within a function, you then closely couple that function to whatever it is that initially defines that variable. If for some reason that other part of the code is changed to rename that variable or do things you didn't anticipate, it might break that function. Likewise, you might not realize that the function is doing things to that variable, causing unforeseen bugs in the application. And of course it becomes difficult to re-use such a function in another context if that same variable name does not exist there and have the same meaning/usage.

Monday, March 16, 2009

<img> tag problem while XSLT transformation

Problem: When i use < img src="" /> tag in XSLT i face problem while transforming, normally i get Blank page or some unkown error.

Answer:

Normally when you use <img> tag, there may be various reason. Like When image tag is written you must use / at the end <img src="" />

Also when you want to use some value select into the tag you get white space. Normally you might be doing something like this.
<img src="<xsl:value-of select="node1/node2" />" />

Actually what you have to write is <img src="{node1/node2}" />
Just remove all that select and just curly braces at start and end. There may be certain times that you may have to write <img src="{node1/@node2}" /> i.e. I mean you have to use @
at the last note.

Using value-of select is not allowed in any tag. the above given procedure is the solution to it.

If you still face the problem you reply to same post. I would edit the post and more solutions.

Friday, March 13, 2009

Javascript : Add / Remove <option> to / from <select>

How to add or remove <option> to or from</select>

The technique used for this works for DOM complaint browsers. i.e. works in IE 6.0+, Firefox 1.0+, Safari 3 and Opera.

First step is lets write code for SELECT box.

<select id="selbox" name="selbox">
<option value="1">Text 1</option>
<option value="2">Text 2</option>
</select>


Next step is to write javascript for adding.

function addoption(){
var op = document.createElement('option');
var elSel = document.getElementById('selbox');

op.text = "Sample Text"; //write text for option.
op.value = "val"; //write value for option.

try{
elSel.add(op,null); //in IE won't work.
}
catch (er){
elSel.add(op); //for IE.
}
}


Thirs step is to write javascript for removing option which is selected.

function removeoption(){
var elSel = document.getElementById('selbox');

var i;
for (i = elSel.length - 1; i>=0; i--) {
if (elSel.options[i].selected) {
elSel.remove(i);
}
}
}


This was the complete sample code which will help you out to get learn. I have code which is more generic which i will write below :


<html>
<head>
<title>Javascript Add / Remove options from select.</title>
<script type="text/javascript">
<!--
var _doc = document;

var uloki = {
addoption: function(txtb,selb){
var em=_doc.getElementById(txtb).value;
var elSel=_doc.getElementById(selb);
var te= _doc.createElement('option');
te.text = em; te.value = em;
if(em != "" ){
try{
elSel.add(te,null); //in IE it wwont work
}
catch (er){
elSel.add(te); //for IE
}
_doc.getElementById(txtb).value="";
}
},
removeoption: function(selb){
var elSel = _doc.getElementById(selb);
var i;
for (i = elSel.length - 1; i>=0; i--) {
if (elSel.options[i].selected) {
elSel.remove(i);
}
}
}
};
//-->
</script>
</head>
<body>

<input type="text" name="tbox" id="tbox">
<input type="button" value=" Add " onclick="uloki.addoption('tbox','selb')">
<br/><br/>

<table>
<tr>
<td>
<select name="selb" id="selb" size="4">
<option value="dt">Default Text</option>
</select>
</td>
<td> <input type="button" value="Remove Selected" onclick="uloki.removeoption('selb')"> </td>
</tr>
</table>
</body>
</html>


I think you have gone through the example explained and also sample i made. Well remember size of select box should be always more than 2. if you remove size or keep it below 2 then it wont function properly.

So this was how it works : Javascript Add / Remove <option> tag from / to <select> tag.


Thanks & Put comments if you found bugs / or even if it helped you.

Lets start out...

Hi,
  Myself, Umakant Patil. I have been working in Javascript and PHP since 3 years. From the day i started i faced lots of problems. Always used to seek /search answers over internet. But it was really a difficult job to go and find from variuos site.

  Overall i will love to post the question / problems over here with answers for my readers. A complete set problems and solutions for both Javascript and PHP, which may help you while writing code. Remember i wil not teach PHP or Javascript. Its just part of help for PHP and Javascript.

  If you have or face any problem then you may email me at umakantpatil4[at]gmail[dot]com, or reply here the problem. I will try out my bets and answer the question on same blog as soon as possible for you. Hope you love work which i post here.

  Remember that if my blog helped you in solving problem do reply here so i can know that my blog is really helping you out so i can post more as i can.