通用评论

example_multiple.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. var COMMENT_URL = '../static/js/comment.0.5.13.js';
  2. var RENDER_ELEMENT_COMMENT = 'comment'; // 渲染评论的标签的 ID
  3. /**
  4. * 异步加载 js 文件
  5. * @param {string} url 需要加载的 js 文件地址
  6. * @param {function} callback js 文件加载后的回调
  7. */
  8. function loadScript(url, callback){
  9. var script = document.createElement("script")
  10. script.type = "text/javascript";
  11. if (script.readyState){ //IE
  12. script.onreadystatechange = function(){
  13. if (script.readyState == "loaded" ||
  14. script.readyState == "complete"){
  15. script.onreadystatechange = null;
  16. callback();
  17. }
  18. };
  19. } else { //Others
  20. script.onload = function(){
  21. callback();
  22. };
  23. }
  24. script.src = url;
  25. document.getElementsByTagName("head")[0].appendChild(script);
  26. }
  27. /**
  28. * 渲染评论组件搭配指定的节点
  29. * @param {string} id element id
  30. * @param {string} type 评论的 type
  31. * @param {string} businessId 评论的 businessId
  32. */
  33. function render(id, type, businessId) {
  34. loadScript(COMMENT_URL, function() {
  35. window.renderComment({
  36. id: id,
  37. type: type,
  38. businessId: businessId
  39. })
  40. })
  41. }
  42. document.getElementById('render').addEventListener('click', function() {
  43. render(RENDER_ELEMENT_COMMENT, 1, 'test');
  44. })
  45. document.getElementById('re-render').addEventListener('click', function() {
  46. render(RENDER_ELEMENT_COMMENT, 1, 'test1');
  47. })