Javascript:如何使用第三方获取浏览器缩放级别

时间:2013-04-18 14:32:03

标签: javascript flash browser

如何在所有已知浏览器中获取浏览器缩放级别? 这是How to detect page zoom level in all modern browsers?

的新解决方法

想法是使用flash或HTML5 Canvas来找到解决方案。

现在找到一种使用闪光灯的解决方案。

1 个答案:

答案 0 :(得分:2)

您需要使用JavaScript桥创建一个Flash文件,如下所示:

[SWF(width='100',height='100')]
public class SizeHack extends Sprite
{
    public function SizeHack()
    {
        if(stage){
            onAddToStage();
        }
        else{
            addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
        }
    }

    protected function onAddToStage(event:Event = null):void
    {
        removeEventListener(Event.ADDED_TO_STAGE, onAddToStage);

        // security settings to allow scripting between domains
        Security.allowDomain("*");
        Security.allowInsecureDomain("*");

        // register to javascript
        ExternalInterface.addCallback("getSize", onSizeRequet);

        // stage scale mode must be noscale
        stage.scaleMode = StageScaleMode.NO_SCALE;
    }

    private function onSizeRequet():Number
    {
        if(!stage){
            return 0;
        }
        return stage.stageHeight;
    }
}

然后在你的JavaScript中:

<script type="text/javascript">
    swfobject.registerObject("FlashResizer", "10.0.0");

    function getBrowserZoom()
    {
        return swfobject.getObjectById("FlashResizer")
            .getSize();
    }
</script>

<div id="flashResizerDiv" style="position: absolute; display: block; height: 100px; width: 100%; top: 0; left: 0;">
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="100%" height="100" id="FlashResizer">
        <param name="movie" value="resizer.swf" />
        <!--[if !IE]>-->
            <object type="application/x-shockwave-flash" data="resizer.swf" width="100%" height="100">
            <!--<![endif]-->
            <a href="http://www.adobe.com/go/getflashplayer">
                <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />
            </a>
            <!--[if !IE]>-->
            </object>
        <!--<![endif]-->
    </object>
</div>

现在,当您致电getBrowserZoom()时,您将获得浏览器的精确缩放。