Please wait for 15 seconds...

Saturday, 8 September 2012



Last night I was working on a project and in many places I needed to explode and implode javascript variables just like in PHP. I searched on internet and for javascript explode function and few developers have written this kind of function but they are little bit complicated for my purpose so I decided to create a simple but effective method. Then I created below functions to solve my purpose. I hope you would like it.

Copy below code and paste it just before the <head> in your HTML page.


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
    experiment();
});

function experiment(){
var str = "apple#banana#orange#melon#mango";
var exp_data = explode('#', str);

 alert('Explode as array: '+exp_data);

alert('Implode with @'+ implode('@', exp_data));
}
function explode(delimiter, str){
return str.split(delimiter);
}
function implode(delimiter, arr){
var imploded_data = '';
for(var i=0;i<arr.length;i++){
imploded_data += arr[i];
if(i < (arr.length-1)){
imploded_data += delimiter;
}
}
return imploded_data;
}
</script>

Now save the file and load it in the browser. As the page loading will finish experiment will call and show you some result. Thanks for reading this tutorial.

Posted by Atul Prasad Gupta

0 comments:

Post a Comment

Subscribe Techsirius

Techsirius on Facebook