Created
April 23, 2013 06:53
-
-
Save henryjuan/5441360 to your computer and use it in GitHub Desktop.
痞客邦留言表單下方的 JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
<!-- | |
(function($){ | |
var pixform = {}; | |
pixform.confirm_popup = false; | |
// 偵測是瀏覽器是否支援 input 的 placeholder 屬性 | |
var tmp = document.createElement('input'); | |
pixform.usePlaceholder = false; | |
if ('undefined' !== typeof tmp.placeholder) { | |
pixform.usePlaceholder = true; | |
} | |
pixform.checkCommentFormWithPlaceholder= function() { | |
var commenturl = $('#comment_url'); | |
var errMsg = ''; | |
if ('' === $('#comment_text').val()) { | |
errMsg = _('請輸入留言內容!'); | |
} | |
var commenturl_val = $.trim(commenturl.val()); | |
if (commenturl_val.length > 0 && !/^https?:\/\//.test(commenturl_val)) { | |
errMsg = _('請輸入正確的網址!'); | |
} | |
if (errMsg !== "") { | |
alert(errMsg); | |
return false; | |
} else { | |
if ('http://' === commenturl_val) { | |
commenturl.val(''); | |
} | |
return true; | |
} | |
}; | |
pixform.checkCommentFormWithoutPlaceholder= function() { | |
var commenturl = $('#comment_url'); | |
var commentmail = $('#comment_mail'); | |
var commentname = $('#comment_showname'); | |
var errMsg = ''; | |
if ('' === $('#comment_text').val() || $('#comment_text').hasClass('input-empty')) { | |
errMsg = _('請輸入留言內容!'); | |
} | |
var commenturl_val = commenturl.hasClass('input-empty') ? '' : $.trim(commenturl.val()); | |
commenturl.val(commenturl_val); | |
var commentmail_val = commentmail.hasClass('input-empty') ? '' : $.trim(commentmail.val()); | |
commentmail.val(commentmail_val); | |
var commentname_val = commentname.hasClass('input-empty') ? '' : $.trim(commentname.val()); | |
commentname.val(commentname_val); | |
if (commenturl_val.length > 0 && !/^https?:\/\//.test(commenturl_val)) { | |
errMsg = _('請輸入正確的網址!'); | |
} | |
if (errMsg !== "") { | |
alert(errMsg); | |
return false; | |
} else { | |
if ('http://' === commenturl_val) { | |
commenturl.val(''); | |
} | |
return true; | |
} | |
}; | |
pixform.setInputWithPlaceholder = function() { | |
var $captcha = $('#captcha'), $ad = $('#comment-area-ad'); | |
$.each(new Array($('#comment_showname'), $('#comment_text')), function(i, e){ | |
return (function($obj) { | |
$obj.focus(function(){ | |
$captcha.show(); | |
$ad.show(); | |
}); | |
})(e); | |
}); | |
}; | |
pixform.setInputWithoutPlaceholder = function() { | |
var $input, $captcha = $('#captcha'), $ad = $('#comment-area-ad'); | |
var columns = new Array(); | |
columns['comment_mail'] = "\u96fb\u5b50\u90f5\u4ef6 ..."; | |
columns['comment_url'] = "\u500b\u4eba\u7db2\u9801 ..."; | |
columns['comment_text'] = "\u7559\u500b\u8a00\u5427 ..."; | |
columns['comment_showname'] = "\u60a8\u7684\u66b1\u7a31 ..."; | |
for (var id in columns) { | |
$input = $('#' + id); | |
if (0 == $input.size()) { | |
continue; | |
} | |
$input.focus(function(){ | |
return (function($obj) { | |
if ($obj.hasClass('input-empty')) { | |
$obj.val('').css('color', '#000').removeClass('input-empty'); | |
} | |
if ('comment_showname' == $obj.attr('id') || 'comment_text' == $obj.attr('id')) { | |
$captcha.show(); | |
$ad.show(); | |
} | |
})($(this)); | |
}).blur(function(){ | |
return (function($obj) { | |
if (!$obj.val()) { | |
$obj.val(columns[$obj.attr('id')]).addClass('input-empty').css('color', '#bbb'); | |
} | |
})($(this)); | |
}).blur(); | |
} | |
}; | |
pixform.setCommentForm = function() { | |
if (pixform.usePlaceholder) { | |
pixform.setInputWithPlaceholder(); | |
} else { | |
pixform.setInputWithoutPlaceholder(); | |
} | |
$('#unfold-comment-extra').click(function(){ | |
$('#comment-extra').toggle(); | |
return false; | |
}); | |
$('#comment_open').click(function() { | |
if ($(this).is(':checked')) { | |
$('#nologin_hidden_msg').show(); | |
} else { | |
$('#nologin_hidden_msg').hide(); | |
} | |
}); | |
$('.json-form').live('submit', function(){ | |
if (pixform.usePlaceholder) { | |
if (!pixform.checkCommentFormWithPlaceholder()) { | |
return false; | |
} | |
} else { | |
if (!pixform.checkCommentFormWithoutPlaceholder()) { | |
return false; | |
} | |
} | |
var data = $(this).serializeArray(); | |
if (pixform.confirm_popup) { | |
var newWindow = window.open('', '_comment_popup', 'height=0,width=0'); | |
} | |
if ('undefined' !== typeof(pix.stringify)) { | |
JSON.stringify = pix.stringify; | |
JSON.parse = pix.parse; | |
} | |
$.post($(this).attr('action'), data, function(ret){ | |
if (ret.error){ | |
if (pixform.confirm_popup) { | |
newWindow.close(); | |
} | |
alert(ret.message); | |
if (ret.authcode_url) { | |
$('img.authcode-url').attr('src', ret.authcode_url); | |
$('input[name="authCode"]').val(''); | |
} | |
return; | |
} | |
if (pixform.confirm_popup) { | |
newWindow.location = ret.redirect; | |
return; | |
} else { | |
alert(ret.message); | |
window.location.reload(); | |
} | |
}, 'json'); | |
return false; | |
}); | |
$('a.reload-captcha').live('click', function(){ | |
var action = $(this).parents('form').first().attr('action'); | |
$.post(action, {"sToken": $('input[name="sToken"]').val(), "reload-captcha": 1} ,function(ret) { | |
if (ret.error) { | |
alert(ret.message); | |
return; | |
} | |
if (ret.authcode_url) { | |
$('img.authcode-url').attr('src', ret.authcode_url); | |
$('input[name="authCode"]').val(''); | |
if (ret.advert_url) { | |
$('#authcode-link-url').attr('href', ret.advert_url); | |
} | |
} | |
return; | |
}, 'json'); | |
return false; | |
}); | |
}; | |
pixform.setCommentForm(); | |
})(jQuery); | |
//--> | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment