Monthly Archives: January 2013

convert URL query string to object

As we discussed about how to convert object to query string. Here we are going for reverse engineering. i.e. convert query string to object.

First of all thing about how to get url query sting through JavaScript? It’s pretty simple.

Window.location.search;

location.search gives us a query string which is located in url address. Guess our current url address is:

http://www.sourcecodemart.com?page=12&post=M123&s=

var qryString = window.location.search
console.log(qryString);

Output:

?page=12&post=M123&s=

Now, It’s time to remove ? (question mark from the string.)

qryString = qryString.substring(1);
// output : page=12&post=M123&s=

Convert string to array

qryStringArr = qryString.split(‘&’);
// output : [“page=12″,”post=M123″,”s=”]

split() function returns array. It uses for converting list to array.

I think you understood whole thing and get idea about next step. Right? :) Yes, exactly. The for loop.

for(i=0; i<qryStringArr.length; i++){
paramvalue = qryStringArr[i].split('=');
jsonobj[paramvalue[0]] = paramvalue[1];
}

In loop we have taken one by one array value and split it and hold in JSON object.

Whole example:

var qryString = window.location.search;
qryString = qryString.substring(1);
var qryStringArr = qryString.split('&');
var jsonobj = {}, paramvalue = '';
for(i=0; i<qryStringArr.length; i++){
paramvalue = qryStringArr[i].split('=');
jsonobj[paramvalue[0]] = paramvalue[1];
}
console.log(jsonobj);

output is:

Object { page=”12″ , post=”M123″ , s=””}

WordPress bloginfo()

I have started learning WordPress and gather some required information about it. Here I am talking about WordPress bloginfo() function. It returns information about your site.

bloginfo() parameters

bloginfo() parameters

Bloginfo() is most useful function in wordpress. Using bloginfo() function you can get information which you have given in User Profile and General Options (settings -> general) from the WordPress administrator panels. Bloginfo() function can be used in anywhere in PHP code.

How to use Bloginfo()

<?php bloginfo( parameter );?>

If you use bloginfo() without parameter it returns name of blog. It works same like bloginfo(‘name’);

Parameters

name                    =             sourcecodemart

description         =             sharing knowledge

admin_email      =             sourcecodemart@gmail.com

charset                 =             utf-8

html_type           =             text/html

language              =             en-US

For more details about parameters click here.

Examples

Show Blog Title

<h1><?php bloginfo(‘name’); ?></h1>
<a href=”<?php bloginfo(‘url’); ?>” title=”<?php bloginfo(‘name’); ?>”>
<?php bloginfo(‘name’); ?>
</a>

switch development to production server automatically

In order to define database credential most of time we make one page like db.php and define all DB variables as follows for development and production server:

define(‘HOSTNAME’,’localhost’);
define(‘USERNAME’,’root’);
define(‘PASSWORD’,”);
define(‘DATABASE’,’testdb’);

//define(‘HOSTNAME’,’hostname’);
//define(‘USERNAME’,’username’);
//define(‘PASSWORD’,’password’);
//define(‘DATABASE’,’testdb’);

After moving on production server DB variables as like:

//define(‘HOSTNAME’,’localhost’);
//define(‘USERNAME’,’root’);
//define(‘PASSWORD’,”);
//define(‘DATABASE’,’testdb’);

define(‘HOSTNAME’,’hostname’);
define(‘USERNAME’,’username’);
define(‘PASSWORD’,’password’);
define(‘DATABASE’,’testdb’);

I have seen most of Core PHP programmers are used to with above method for defining DB variables. There will be chance to come error if we forget to switch comment. I also get rid of this and found solution.

Solution is:


Apply above method and forget above your DB variable’s page until credential are not change.

What do you say?

How to check Dom element exists using jQuery?

Sometimes, we need to check is Dom element exists or not. If yes then process next step. It’s on JavaScript validation or before fire any trigger for rendering data through JavaScript.

Generally, we have done mistake to check is HTML Dom element is alive or not using jQuery.

For example,

jQuery(‘#elementid’);

Here jQuery always return blank object if it is not exists so that in this case you always get blank object and your cursor go in condition. Please Run below code.

if(jQuery(‘#elementid’)) alert(‘I am in’);

But keep in mind for executing above code Dom element should not be in HTML. In order to give proper condition you always need to check length of object.

alert(jQuery(‘#elementid’).length);
if(jQuery(‘#elementid’).length) alert(‘I am in’);

Now, run above code you get alert if Dom element exists on HTML.

HR Interview Question – Tell me about yourself?

Generally, Interviewer may start interview with this question (Tell me about yourself?).

Please pause here and give your introduction to yourself and keep in mind what you have given. Now read.

  • Just focus on your professional for this question answer.
  • Don’t include your family in this question until interviewer will not ask about your family.
  • Just tell about your education, field of interest and hobbies.
  • Keep in mind hobbies should be in your resume and it should be for your relaxation and refreshment.

For example,

I am……….. (Your name with surname).
I completed………. (Your last education).
I am coming from……… (City name).
I want to become………… (Your goal).
My hobbies are…………….. (Watching Movies, Playing Cricket, Listening Music)

Keep in mind if interviewer ask tell me about yourself? then give answer of above all things but they ask about any particular thing then give only that question’s answer.

Did you remember your introduction?

So compare with your answer and above answers. Let me know what you find on comparison and if you have any suggestion or more tips on this question.

Comment Please…… about your feeling!

Clear all form element using jQuery

I always want to common function to reset all form elements. Generally, this code snippets is used where user fill up whole form or edit whole form but does not want to submit and wants to clear all elements or there are many search field and need to reset them frequently.

Please have a look below code that does the same thing.


function resetFormElements(frm){
$(frm).find(':input').each(function() {
switch(this.type){
case 'password':
case 'select-multiple':
case 'select-one':
case 'text':
case 'textarea':
$(this).val('');
break;
case 'checkbox':
case 'radio':
this.checked = false;
}
});
}

Note : I noticed that some time in IE selectbox is not getting cleared. So you can modify script and put

$(this).attr("selectedIndex",0);

for ‘select-one’ case. It will work in both IE and Mozilla.