Legacy Archive (2014): Originally written in Chinese on doubixie.com and translated into English for this edition. All archive posts · Home

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 target href, 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 diy module 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.