Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript堆叠错误_Javascript_Jquery - Fatal编程技术网

javascript堆叠错误

javascript堆叠错误,javascript,jquery,Javascript,Jquery,我有以下代码: $jq('.pop-tip').click( function(event) { width = ''; height = ''; var img = new Image(); img.onload = function() { width = 'width='+(this.width + 240); height = 'height='+(this.height +

我有以下代码:

 $jq('.pop-tip').click( function(event) {
        width = '';
        height = '';
        var img = new Image();
        img.onload = function() {
            width = 'width='+(this.width + 240);
            height = 'height='+(this.height + 360);              
        }
        img.src = 'http://www.domain.com/uploads/2011/10/dealoftheweek.jpg';        
        destination = $jq(this).attr('title');
        postPop = $jq(this).attr('href');
        alert ( width );
        window.open(postPop, 'tip_pop',''+ width +', '+ height +', scrollbars=0' ).focus();
        window.open(destination, '_parent','' );
        event.preventDefault();
)};

出于某种原因,它只适用于窗口上方的警报。open call为什么会出现这种情况以及如何修复它?

这是因为您正在设置仅在图像完全加载后才会发生的onload事件中的高度和宽度。 放置警报将使图像完全加载,当您单击“警报确定”时,将调用图像的onload并设置值。但是,如果您删除该选项,则不会设置“高度”和“宽度”值,因此它将不起作用


您可以通过调用onload事件中的其余代码来修复此问题

图像加载是异步的;加载图像时,代码将继续执行。然后点击window.open行,该行需要宽度和高度,但尚未定义。几毫秒后,图像加载,宽度和高度也加载

有了此警报,您可以将window.open调用延迟足够长的时间,以便图像完成加载,当您单击“确定”按钮时,维度变量就可以使用了

解决方案是在img.onload处理程序函数中填充window.open以及依赖于图像数据的所有内容


编辑:该死,我打字很慢:p和Tomalak。。。ROFL

每当你发布一个关于警报延迟执行和使事情正常运行的问题时,如果你的代码没有按预期的那样运行,仙女就会异步死亡,那么我们应该如何确定它应该做什么呢?试着在FF/Chrome中弹出你的js控制台shift-ctrl-J,看看这两个版本之间是否有任何错误。正如Tomalak愉快地暗示的那样,你的onLoad函数是最后要执行的,因此,通常宽度和高度尚未设置。感谢您的时间-非常感谢。