Discuz! X3 Portal Customization Guide 1: Click-to-Next-Page Images
Original Chinese title: discuz x3 门户改造手册1-图片翻页
The Problem
- The Discuz! X3 portal is essentially a CMS, and as a CMS its content optimization leaves a lot to be desired. One issue I ran into is that clicking an image on an article view page only opens the image in a new window. If clicking an image jumps straight to the next page instead, it cuts down user clicks while boosting impressions and page views—you know the drill.
- It’s worth mentioning that Discuz!‘s built-in JS still conflicts with jQuery. An earlier post covered a solid workaround for that,
- but I figured I’d give vanilla JS a shot instead.
Approach
-
Two possible approaches: frontend and backend.
-
Backend approach: intercept Discuz!‘s database-saving logic when saving in the editor and wrap images in links. I spent a lot of time digging through the Discuz! source code for this, but unfortunately my PHP chops weren’t quite up to it, so I gave up after several failed attempts.
-
Frontend approach: all the page DOM is already right there. I noticed the
<td>displaying the article body has an ID, meaning I can grab the<a>and<img>elements inside it. Looking at the pagination section, there’s a<div class="pg">. That gives me a way to pull the targethref, and as a bonus, the image link on the last page can be pointed wherever you want. Again, you know the drill. -
IE compatibility has to be considered since this is vanilla JS.
-
Enough talk—here’s the code. Just drop a
diymodule onto any article view page to test and debug it.// ie6-8 has not the method of getElementsByClassName. So we need one; if(!document.getElementsByClassName){ document.getElementsByClassName = function(className, element){ var children = (element || document).getElementsByTagName('*'); var elements = new Array(); for (var i=0; i<children.length; i++){ var child = children[i]; var classNames = child.className.split(' '); for (var j=0; j<classNames.length; j++){ if (classNames[j] == className){ elements.push(child); break; } } } return elements; }; } //last pic var new_href="your website"; var pg = document.getElementsByClassName('pg'); if (pg.length != 0){ var pg_children = pg[0].children || pg[0].childNodes; for (var i = 0; i < pg_children.length; i++) { // The STRONG tag is the current page; the next A tag is the target link if (pg_children[i].nodeName == 'STRONG') // Check if it is an A tag if(pg_children[i+1].nodeName =='A') new_href = pg_children[i+1]; }; } // Get all images in the content and replace their href var td_name = document.getElementById('article_content').getElementsByTagName('a'); for (var i = 0; i < td_name.length; i++) { var td_name_img = td_name[i].getElementsByTagName('img'); if (td_name_img.length != 0){ td_name[i].href = new_href; } };
Test Results
- Runs cleanly on IE6–8, Chrome, Firefox, and Safari.
That’s about it.
遇到的问题
- x3 门户其实就是一个cms系统,作为一个cms,对内容的优化还是不够。遇到的一个问题就是内容页(view)点击图片都只是在新窗口打开这个图片。添加一个点击图片就跳转到下一页的话,不但能减少用户的操作,同时能增加展示和pv,结果你懂得。
- 这里不得不提下discuz自带的js还是和jq有冲突的。之前的文章里面有一个很好的解决方法
- 但是考虑到还是原生的js试试。
思路
-
两个思路,前端和后端
-
后端思路,在编辑器里面保存的时候修改discuz存入数据库的逻辑,在图片里面加入链接。为此耗费了大量时间看了不少discuz源码,无奈php学的还是太少,尝试多次之后放弃。
-
前端思路,既然页面dom都在了。看到页面文章内容展现的
<td>是带id,那就是可以获取到里面的<a>和<img>。在看了下分页的部分有<div class="pg">。那获取到需要的href也有了,拼接的问题,同样带来的好处是最有一页的图片链接可以任意指定。又是一个你懂得。 -
ie兼容性是需要考虑的。因为是原生js。
-
废话少说,放马出来。随便找一个内容页加入一个
diy模块就可以调试。// ie6-8 has not the method of getElementsByClassName. So we need one; if(!document.getElementsByClassName){ document.getElementsByClassName = function(className, element){ var children = (element || document).getElementsByTagName('*'); var elements = new Array(); for (var i=0; i<children.length; i++){ var child = children[i]; var classNames = child.className.split(' '); for (var j=0; j<classNames.length; j++){ if (classNames[j] == className){ elements.push(child); break; } } } return elements; }; } //last pic var new_href="your website"; var pg = document.getElementsByClassName('pg'); if (pg.length != 0){ var pg_children = pg[0].children || pg[0].childNodes; for (var i = 0; i < pg_children.length; i++) { //strong标签是当下页,下一个a则是目标链接 if (pg_children[i].nodeName == 'STRONG') //判断是不是a标签 if(pg_children[i+1].nodeName =='A') new_href = pg_children[i+1]; }; } //获取内容中所有的图片。更换href var td_name = document.getElementById('article_content').getElementsByTagName('a'); for (var i = 0; i < td_name.length; i++) { var td_name_img = td_name[i].getElementsByTagName('img'); if (td_name_img.length != 0){ td_name[i].href = new_href; } };
测试结果
- 安全运行 IE6-8 chrome ff Safari