简体   繁体   中英

Javascript popup window not working

I manage a site that I did not build. The site features some links, where you can click the link, and a modal window opens with content from a different html file. It used to work, and now it doesn't.

I have compared all the relevant files between now and when I took over the site, but do not see any changes that would have affected this.

The popup windows are called thusly:

<?php bioLinkText('Daniel Jones', 'Read more about Dr. Jones'); ?></p>

The page it should open is /bios/daniel-jones.html

From the functions.php file:

function bioLinkText($name,$text) {
$name = strtolower(str_replace(" ","-",$name));
echo '<a href="/bios/'.$name.'.html" class="popUp">'.$text.'</a>';}

This part functions OK. But it used to create a modal window, and now, it just opens the link like a regular link.

From the global.js file:

 // AJAX Popups
    function popUp(page,randId) {
        $('body').append(
        '<div id="'+randId+'" class="pWin" style="display:none;position:fixed">'+
            '<span class="pHead">'+
                '<a href="'+page+'" target="_blank">Open in new window</a>'+
                '<span class="pClose">X</span>'+
            '</span>'+
            '<div class="pBod"></div>'+
        '</div>'
        );

        var top = (h/2) - 150;
        var left = (w/2) - 300;

        $('#'+randId+'.pWin').addClass('large').css({top:top+'px',left:left+'px'});
        $('#'+randId+' .pBod').html('<img src="/images/loading.gif" alt="loading"/>').load(page+' #content', function() {
            $('.pWin').show(300);
            $('.pBod #content').find('img').filter('#portrait').attr('src', function(index, src) {
                return '/bios/' + src;
            });
        });

    }

    $('.popUp').click(function() {
        var randId = randomString();
        var num = $('.pWin').length;
        if (num < 5) {
            var page = $(this).attr('href');
            popUp(page,randId);
            $('#'+randId+'.pWin').draggable({handle:'.pHead'}).resizable({alsoResize:'#'+randId+' .pBod', minWidth: 320, minHeight: 280, maxWidth: 800, maxHeight: 600});
        }
        return false;

    });

    function pClose(btn) {
        var pWin = btn.closest('.pWin');
        pWin.hide(200, function() { pWin.remove(); });
    }
    $('.pClose').live('click',function() {
        var btn = $(this);
        pClose(btn);
    });
    $(document).keyup(function(e) {
      if (e.keyCode == 27) {
            $('.pWin').hide(200, function() { $('.pWin').remove(); });
      }
    });

From the style.css file:

.popUp, .pHead a { padding-right: 16px; background: url(/images/external.gif) 100% 50% no-repeat; }

.popUp.noBg { background:none; padding-right:0; }

I have been trying to figure this out for 10+ hours. Any help would be greatly appreciated. The one thing is...I don't understand how the javascript function popUp would be invoked. Is that the missing ingredient?

Try this:

//Make sure the DOM is ready (If you call this function before '.popUp' exists, it wont be matched, and the '.click' handler wont be added.
$(document).ready(function() {

    $('.popUp').click(function(e) {
        //Prevent the default action (Clicking the button)
        e.preventDefault();
        //..Your code here
    });
});

Well, I figured it out. I changed the function, adding an onclick="popup()" property to the a href, and now it works:

function bioLinkText($name,$text) {
$name = strtolower(str_replace(" ","-",$name));
echo '<a href="/bios/'.$name.'.html" onclick="popup()" class="popUp">'.$text.'</a>';

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM