Fixing Javascript Errors in WordPress SEO

In the last two versions of Yoast’s WordPress SEO plugin there’s been errors in the JavaScript that cause parts of tinymce to not work. The issue is some of the variables are not strings like expected, so when they try to run string methods against them, causing JavaScript to throw a syntax error. The simple fix is to add type checking for lines 189 and 200 of wp-seo-metabox.js. 

if ( !desc || desc.length < 1 ) {
	desc = jQuery("#content").val();
	if(typeof desc == 'string'){
		desc = yst_strip_tags( desc );
	}
	var descsearch = new RegExp( focuskw, 'gim');
	if ( desc.search(descsearch) != -1 ) {
		desc = desc.substr( desc.search(descsearch), wpseo_meta_desc_length );
	} else {
		desc = desc.substr(0, wpseo_meta_desc_length );
	}
	jQuery("#snippet .desc span").css('color','#777777');
}

for (var i in keywords) {
	if(typeof keywords[i] == 'string'){
		var kw			= yst_strip_tags( keywords[i] );
		var urlfocuskw 	= kw.replace(' ','-').toLowerCase();
		focuskwregex 	= new RegExp( '('+kw+')', 'gim');
		urlfocuskwregex = new RegExp( '('+urlfocuskw+')', 'gim' );
		desc 			= desc.replace( focuskwregex, ''+"$1"+'' );
		title 			= title.replace( focuskwregex, ''+"$1"+'' );
		url 			= url.replace( urlfocuskwregex, ''+"$1"+'' );
	}
}

New Formatting for XMLHttpRequest

Recently I decided to try reducing the size of hacking the HTTP request for cross browser support in AJAX. This little snippet has been tested and is working.

// new xml http object
var xmlHttp = new XMLHttpRequest() ? new XMLHttpRequest() :
( new ActiveXObject(“Msxml2.XMLHTTP”) ? new ActiveXObject(“Msxml2.XMLHTTP”) :
( new ActiveXObject(“Microsoft.XMLHTTP”) ? new ActiveXObject(“Microsoft.XMLHTTP”) : null ) );

Recently I decided to try reducing the size of hacking the HTTP request for cross browser support in AJAX. This little snippet has been tested and is working.

I find this new method easier to read and is much more elegant than the method bellow; which is the only method I’ve seen out there.

I know that I am not the only who has tried to shorten this section of code. Let me know what you think or how you handle it yourself.