Show current date using Javascript in web browser(Client side date in web browser)

For client side scripting,

Go through following lines to product date in DD/MM/YYYY format:

var objToday = new Date();
var dd = objToday.getDate();
var mm = objToday.getMonth() + 1;  //January is 0
var yyyy = objToday.getFullYear();

if (dd < 10) { dd = ‘0’ + dd }
if (mm < 10) { mm = ‘0’ + mm }

var strToday = dd + ‘/’ + mm + ‘/’ + yyyy;

alert(strToday);       // will show like ’17/07/2013′

Advertisements

Retrieving cross-domain data by using jquery’s getJSON

Recently I found one limitation with jquery.ajax, problem was that when we want to retrieve data from other domain, Not from your current website.

Browser’s same-origin policy prevents requesting data from other domain.So jQuery.get, jQuery.post, jQuery.ajax does not work for it.

 One way to fetch data is by using jQuery’s getJSON method as follow:

var url = “http://yourdomain.com/sample-page.aspx?callback=?&#8221;;

jQuery(document).ready(

    function() {

        jQuery.getJSON(url, function (data) { alert(data.price); });      

});

 Here we have loaded JSON data located on another domain with JSONP callback, which can be done using the following syntax: url?callback=?.

jQuery automatically replaces the ? with a generated function name to call.

 

At Server side, data must be return in json format like:

{‘name’:’abc’,’price’:’44’}

 

 And additionally, you have to enclose your data inside function,

 For ex: dummyfunction({‘name’:’abc’,’price’:’44’});

 

Here ‘dummyfunction’ will be dynamic on each call to your page, and it will be passed to your dynamic page

Note callback=” in URL variable,

So if your are using asp.net then something like following will be there

Response.Write( Request.QueryString[“callback”] + “({‘name’:’abc’,’price’:’44’});”);

 

And with PHP, page response should be something as below,

echo $_GET[‘callback’].'({‘name’:’abc’,’price’:’44’});’;

Change page meta title by using javascript

I just seen one site was using this feature, and was changing its meta title/page title displaying in browser window title bar.

So sharing following useful, one line code snippet giving you idea about it.

<script type=”text/javascript”>

document.getElementsByTagName(‘title’)[0].innerHTML = “your dynamic title goes here…”;

</script>

Such small dynamism makes our website looks more interactive.

One thing to remember about this is that, changing meta title will not help SEO wise, as its changing with java script. Web crawler will have default page title in his record.

Web development small tips

Single quote in java script, may result in error

In Javascript,when using or concatenating string, use it with double quotes.
Or it may generate error.

Recommanded:
var str="Some String" + document.getElementById("txtName").value;

Should be avoid:
var str='Some String' + document.getElementById("txtName").value;


To overcome Date format related error, in .Net, with CompareValidator validation control
You can, specify culture in web config file.


Put Share Button on your website with ADD THIS

Go to http://www.addthis.com site, and it will ask you button layout you want to Choose.
And that site will display code snippet, which will contain one SCRIPT tag and HTML lines.

So you have to add script line in your page and put ‘html’ tags at place where you want to display share icons.


To set content in tinyMCE editor box by using javascript
var editorInstance = tinyMCE.getInstanceById("ctl00_cpMain_txtCampaignDesc"); //specify clientid of textarea field
editorInstance.getBody().innerHTML = "This is dynamic text set by javascript.";