2009/06/24

escape single quote in javascript

Problem:

alias_term = trim(alias_term);
var str = '<a href="javascript:updt_mapping(\'alias\',\''+alias_term+'\');">Update Alias</a>';
Using above code, when the alias_term is something like k’nex, which has single quote, it would broke the javascript, for the javascript statement become updt_mapping(‘alias’, ‘k’net’)

Solution:

escape single quote, but you could not using escape function, alias_term = escape(alias_term), but replace function, alias_term = alias_term.replace(/\'/,"\\'");

2009/06/23

Some notes about yslow

Company’s webstie is slow. The boss wanted to know why and I am trying to find out why.

I think form web developer viewpoint, what you can do is optimize the web component. If your site hosted on a bad server, then you effort on optimizing the web component is minor. Anyway Firefox fox yslow seems to be a great tool helping you do the website optimization.

1. make fewer HTTP requests.

I think most of current sites have a same problem that a page contain too many external javascript scripts. yslow suggests to try combining them into one, however, most of time this is not a solution at all because you just could not combine them.

2. yslow suggest move all javascript at bottom.

If you should every javascritp snippets, which are relied to those javascripts at header, is running after document ready, then you may do this. Otherwise, you may break some of them.

3. Minify javascript and css

I do some of them. Just too lazy to do all of this, because you are going to update those minified files, that is pain. You have to edit the unminified version, test it and then minfied it again (unless you have a good eye to modify on a minified file)

4. Add expires headers and compress components with gzip

This can be done by updating apache setting …

5. Use a content delivery network

I think this is a big reason the site is slow, if you site hosted on a fast and stable content  delivery network, then you site is most likely running faster.

Note: Just found that yslow does not recognized old style JavaScript tags, like

<script language="JavaScript" src="/js/ajax.js"></script>
//yslow uses type to determine it is javascript or not
<script type="text/javascript" src="/js/ajax.js"></script>

2009/06/18

Remove all posts

Sometimes you may want to remove all posts from your Wordpress. You can do this from Wordpress dashboard, but you have to do delete them page by page, and each page has only 20 posts.

If you really wanted to remove all post at a time, here is Perl script to do so.

use WordPress::XMLRPC;
my $o = WordPress::XMLRPC->new({
username => 'admin',
password => 'yourpass',
proxy => '/path/to/your/xmlrpc.php',
});
#print $o->getPost();
my $post = $o->getRecentPosts(500); #500 meant get 500 posts
for $p (@$post) {
 $id =  $p->{'postid'};
 #print "$id\t";
 $o->deletePost($id);
}
PERL WORDPRESS XMLRPC Module can be found at here

2009/05/27

initial array in java

1. static one:

int[] a;           // Declare a to be an array of ints
a = new int[100];  // Allocate an array of 100 ints
//or in a line
int [] a = new int[100];
//string type
String [] b = new String[100];

2. dynamic one:

Note: ‘Java does not offer arrays that can be extended at run time, for primitives or Objects, so it is common to use a Vector for a set of elements that needs to be able to grow.’ from javalobby

Vector v = new Vector();
int count = v.size();
String[] dynArray = new String[count];
v.copyInto(dynArray);

2009/05/08

How to embed youtbe video on the webpage

Here is code:
<object width="336" height="300">
<param name="movie" value="http://www.youtube-nocookie.com/v/VCtIS9vKdWg&hl=en&fs=1&color1=0x006699&color2=0x54abd6&border=1"></param>
<param name="allowFullScreen" value="true"></param>
<param name="allowscriptaccess" value="always"></param>
<embed src="http://www.youtube-nocookie.com/v/VCtIS9vKdWg&hl=en&fs=1&color1=0x006699&color2=0x54abd6&border=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="336" height="300">
</embed>
</object>
Here is what it looks like:

2009/04/27

How to auto post to Wordpress

  1. Turn on remove_posting at http://yourdomain/wp-admin/options-writing.php
  2. Using below PHP code:
function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$categories=array(1)){
   $categories = implode(",", $categories);
   $XML = "<title>$title</title>".
   "<category>$categories</category>".
   "<body>$body</body>";
   $params = array('','',$username,$password,$XML,1);
   $request = xmlrpc_encode_request('blogger.newPost',$params);
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
   curl_setopt($ch, CURLOPT_URL, $rpcurl);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_TIMEOUT, 1);
   curl_exec($ch);
   curl_close($ch);
}

2009/04/14

mySQL date function

The SQL to get total order number and total order amount from orders table:

SELECT count(*), sum(order_total) 
FROM orders 
WHERE DAYOFMONTH(last_accessed) = DAYOFMONTH(now()) 
      AND MONTH(last_accessed) = MONTH(now()) 
      AND YEAR(last_accessed) = YEAR(now()) 
      AND record_state = 1

Note:

  1. DAYOFMONTH is synonym of DAY used to get day (ex: 1-31) from a data and time string (ex: 2009-04-14 11:44:02)
  2. Complete mysql date function can be found at here