javascript动态内容加载后无法动态调整IMG大小

时间:2011-11-12 21:40:02

标签: php javascript ajax dynamic

我使用来自dynamicdrive的这个javascript将内容加载到div:

    var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
    var loadedobjects=""
    var rootdomain="http://"+window.location.hostname
    var bustcacheparameter=""

    function ajaxpage(url, containerid){
        var page_request = false
        if (window.XMLHttpRequest) // if Mozilla, Safari etc
        page_request = new XMLHttpRequest();
        else if (window.ActiveXObject){ // if IE
            try {
                page_request = new ActiveXObject("Msxml2.XMLHTTP");
                }
            catch (e){
                try{
                    page_request = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                catch (e){}
            }
        }
        else
        return false
        page_request.onreadystatechange=function(){
            loadpage(page_request, containerid)
        }

        if (bustcachevar) //if bust caching of external page
        bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
        page_request.open('GET', url+bustcacheparameter, true)
        page_request.setRequestHeader('charset', 'ISO-8859-1')
        page_request.send(null)
    }

    function loadpage(page_request, containerid){
        if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
        document.getElementById(containerid).innerHTML=page_request.responseText
    }

我使用一个名为“displayImage('url','width')”的自编写php函数来显示一个图像,然后动态调整大小到指定的宽度。

这在静态内容方面效果很好,但是,该功能拒绝处理动态内容。

任何人都可以在这个方面给我一个正确的方向吗? :/我不是很擅长javascript ...

编辑:

抱歉,忘了添加php功能,这里就是:

function displayImage($img_path, $height) {
$image_info = getimagesize($img_path);  
$new_dimension = imageResize($image_info[0], $image_info[1], $height);
$returnstr = "<IMG SRC=\"{$img_path}\" " . $new_dimension . "/>\n";
return $returnstr;
}

function imageResize($width, $height, $target) {
if ($width > $height) {
$percentage = ($target / $width); 
} else {
$percentage = ($target / $height);
}
$width = round($width * $percentage);
$height = round($height * $percentage);

return "width=\"$width\" height=\"$height\"";

}

1 个答案:

答案 0 :(得分:1)

PHP是一种服务器端语言,这意味着它创建页面,将其发送到浏览器,就是这样。它无法调整您稍后添加到页面的图像的大小。您要么JS要么这样做,要么在ajax页面本身使用该PHP函数,并动态加载已经缩小的图像。希望这会有所帮助。

相关问题