+ -
当前位置:首页 → 问答吧 → Discuz!论坛用户登录模块的javascript处理过程的代码分析

Discuz!论坛用户登录模块的javascript处理过程的代码分析

时间:2010-07-08

来源:互联网

本帖最后由 寂寞流星 于 2010-7-8 09:56 编辑



先看看弹出这个窗口是怎么实现的,
  1. <!--{else}-->
  2. <a href="$regname" onclick="floatwin('open_register', this.href, 600, 400, '600,0');return false;" class="noborder">$reglinkname</a>
  3. <a href="logging.php?action=login" onclick="floatwin('open_login', this.href, 600, 400);return false;">{lang login}</a>
  4. <!--{/if}-->
复制代码

这段代码在header.htm文件中,一个表示登录,一个表示注册的链接
点登录弹出第一张图片显示的窗口,点击时会执行一个叫
floatwin(’open_login’, this.href, 600, 400);函数,这个函数在common.js文件中,看看这个函数的执行过程:

  1. var cssloaded= new Array();
  2. function loadcss(cssname) {
  3. if(!cssloaded[cssname]) {//如果不存在
  4. css = document.createElement('link');//创建link标签
  5. css.type = 'text/css';//设置link的type
  6. css.rel = 'stylesheet';
  7. css.href = 'forumdata/cache/style_' + STYLEID + '_' + cssname + '.css?' + VERHASH;
  8. var headNode = document.getElementsByTagName("head")[0];//获取html head标签
  9. headNode.appendChild(css);//添加到head标签间
  10. cssloaded[cssname] = 1;
  11. }
  12. }

  13. function floatwin_wrapkeyhandle(e) {
  14. e = is_ie ? event : e;
  15. if(e.keyCode == 9) {//Tab键
  16. doane(e);
  17. } else if(e.keyCode == 27) {//退格键
  18. for(i = floatwins.length - 1;i >= 0; i--) {
  19. floatwin('close_' + floatwins[i]);
  20. }
  21. }
  22. }

  23. //FloatWin
  24. var hiddenobj = new Array();
  25. var floatwinhandle = new Array();
  26. var floatscripthandle = new Array();
  27. var floattabs = new Array();
  28. var floatwins = new Array();
  29. var InFloat = '';
  30. var floatwinreset = 0;
  31. var floatwinopened = 0;
  32. floatwin('open_login', this.href, 600, 400);
  33. function floatwin(action, script, w, h, scrollpos) {
  34. var floatonly = !floatonly ? 0 : 1;
  35. var actione = action.split('_');//以'_'将字符串分隔成数组
  36. action = actione[0];//action='open'
  37. if((!allowfloatwin || allowfloatwin == 0) &amp;&amp; action == 'open' &amp;&amp; in_array(actione[1], ['register','login','newthread','reply','edit']) &amp;&amp; w >= 600) {
  38. //如果action='open',窗口宽度小于等于600,则设置location.href;
  39. location.href = script;
  40. return;
  41. }
  42. var handlekey = actione[1];//=login
  43. var layerid = 'floatwin_' + handlekey;//='floatwin_login'
  44. if(is_ie) {//获取动画对象
  45. var objs = $('wrap').getElementsByTagName("OBJECT");
  46. } else {
  47. var objs = $('wrap').getElementsByTagName("EMBED");
  48. }
  49. if(action == 'open') {
  50. loadcss('float');//将弹出层的css,插入到模板中
  51. //floatwinhandle['login_0'] = 'floatwin_login';
  52. floatwinhandle[handlekey + '_0'] = layerid;
  53. if(!floatwinopened) {
  54. $('wrap').onkeydown = floatwin_wrapkeyhandle;//对键盘换键的不同操作
  55. for(i = 0;i < objs.length; i ++) {
  56. if(objs[i].style.visibility != 'hidden') {
  57. objs[i].setAttribute("oldvisibility", objs[i].style.visibility);
  58. objs[i].style.visibility = 'hidden';
  59. }
  60. }
  61. }
  62. scrollpos = !scrollpos ? '' : 'floatwin_scroll(\'' + scrollpos + '\');';
  63. var clientWidth = document.body.clientWidth;//对象可见的宽度
  64. var clientHeight = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
  65. var scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
  66. //sciprt= logging.php?action=login;
  67. if(script &amp;&amp; script != -1) {
  68. if(script.lastIndexOf('/') != -1) {
  69. script = script.substr(script.lastIndexOf('/') + 1);
  70. }
  71. var scriptfile = script.split('?');//scriptfile[0] = logging.php;
  72. scriptfile = scriptfile[0]; //scriptfile[1] = action=login;
  73. if(floatwinreset || floatscripthandle[scriptfile] &amp;&amp; floatscripthandle[scriptfile][0] != script) {
  74. if(!isUndefined(floatscripthandle[scriptfile])) {
  75. $('append_parent').removeChild($(floatscripthandle[scriptfile][1]));
  76. $('append_parent').removeChild($(floatscripthandle[scriptfile][1] + '_mask'));
  77. }
  78. floatwinreset = 0;
  79. }
  80. //floatscripthandle[logging.php] = [logging.php?action=login, floatwin_login];
  81. floatscripthandle[scriptfile] = [script, layerid];
  82. }
  83. if(!$(layerid)) {
  84. floattabs[layerid] = new Array();
  85. div = document.createElement('div');//创建div标签
  86. div.className = 'floatwin';//设置创建div的css,id,宽,高
  87. div.id = layerid;
  88. div.style.width = w + 'px';
  89. div.style.height = h + 'px';
  90. //div.style.left = floatwinhandle['login_1'] = ((clientWidth - 600) / 2) + 'px';
  91. div.style.left = floatwinhandle[handlekey + '_1'] = ((clientWidth - w) / 2) + 'px';
  92. div.style.position = 'absolute';
  93. div.style.zIndex = '999';
  94. div.onkeydown = floatwin_keyhandle;
  95. $('append_parent').appendChild(div);//将创建的div标签插入到append_parent标签中
  96. $(layerid).style.display = '';
  97. $(layerid).style.top = floatwinhandle[handlekey + '_2'] = ((clientHeight - h) / 2 + scrollTop) + 'px';
  98. $(layerid).innerHTML = '<div><h3 class="float_ctrl"><em><img src="' + IMGDIR + '/loading.gif"> 加载中...</em><span><a href="javascript:;" class="float_close" onclick="floatwinreset = 1;floatwin(\'close_' + handlekey + '\');">&amp;nbsp</a></span></h3></div>';
  99. //插入html
  100. divmask = document.createElement('div');//创建div标签
  101. divmask.className = 'floatwinmask';//设置css,id,width,height,left,top,position
  102. divmask.id = layerid + '_mask';
  103. divmask.style.width = (parseInt($(layerid).style.width) + 14) + 'px';
  104. divmask.style.height = (parseInt($(layerid).style.height) + 14) + 'px';
  105. divmask.style.left = (parseInt($(layerid).style.left) - 6) + 'px';
  106. divmask.style.top = (parseInt($(layerid).style.top) - 6) + 'px';
  107. divmask.style.position = 'absolute';
  108. divmask.style.zIndex = '998';
  109. divmask.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=90,finishOpacity=100,style=0)';
  110. divmask.style.opacity = 0.9;
  111. $('append_parent').appendChild(divmask);//将创建的div标签插入到append_parent标签中
  112. if(script &amp;&amp; script != -1) {
  113. //如果存在?,则后面加&amp;,否则加?
  114. //script = logging.php?action=login&amp;infloat=yes&amp;handlekey=login';
  115. script += (script.search(/\?/) > 0 ? '&amp;' : '?') + 'infloat=yes&amp;handlekey=' + handlekey;
  116. try {//执行ajax,通过ajax获取login.htm模板
  117. ajaxget(script, layerid, '', '', '', scrollpos);
  118. } catch(e) {
  119. setTimeout("ajaxget('" + script + "', '" + layerid + "', '', '', '', '" + scrollpos + "')", 1000);
  120. }
  121. } else if(script == -1) {
  122. $(layerid).innerHTML = '<div><h3 class="float_ctrl"><em id="' + layerid + '_title"></em><span><a href="javascript:;" class="float_close" onclick="floatwinreset = 1;floatwin(\'close_' + handlekey + '\');">&amp;nbsp</a></span></h3></div><div id="' + layerid + '_content"></div>';
  123. $(layerid).style.zIndex = '1099';
  124. $(layerid + '_mask').style.zIndex = '1098';
  125. }
  126. } else {
  127. $(layerid).style.width = w + 'px';
  128. $(layerid).style.height = h + 'px';
  129. $(layerid).style.display = '';
  130. $(layerid).style.top = floatwinhandle[handlekey + '_2'] = ((clientHeight - h) / 2 + scrollTop) + 'px';
  131. $(layerid + '_mask').style.width = (parseInt($(layerid).style.width) + 14) + 'px';
  132. $(layerid + '_mask').style.height = (parseInt($(layerid).style.height) + 14) + 'px';
  133. $(layerid + '_mask').style.display = '';
  134. $(layerid + '_mask').style.top = (parseInt($(layerid).style.top) - 6) + 'px';
  135. }
  136. floatwins[floatwinopened] = handlekey;
  137. floatwinopened++;
  138. }
  139. }
  140.                
复制代码

作者: 寂寞流星   发布时间: 2010-07-08

不错,学习一下- -楼主也开始发技术贴了么

作者: Vassago   发布时间: 2010-07-08

哥是技术人呀。

作者: 寂寞流星   发布时间: 2010-07-08

学习下  牛人啊

作者: 暖暖   发布时间: 2010-09-07

查看一下

作者: zjf1990   发布时间: 2013-04-26