Get the size of the screen, current web page and browser window

前端 未结 19 2373
面向向阳花
面向向阳花 2020-11-21 05:29

How can I get windowWidth, windowHeight, pageWidth, pageHeight, screenWidth, screenHeight,

19条回答
  •  迷失自我
    2020-11-21 06:10

    I wrote a small javascript bookmarklet you can use to display the size. You can easily add it to your browser and whenever you click it you will see the size in the right corner of your browser window.

    Here you find information how to use a bookmarklet https://en.wikipedia.org/wiki/Bookmarklet

    Bookmarklet

    javascript:(function(){!function(){var i,n,e;return n=function(){var n,e,t;return t="background-color:azure; padding:1rem; position:fixed; right: 0; z-index:9999; font-size: 1.2rem;",n=i('
    '),e=function(){return'

    width: '+i(window).width()+" height: "+i(window).height()+"

    "},n.html(e()),i("body").prepend(n),i(window).resize(function(){n.html(e())})},(i=window.jQuery)?(i=window.jQuery,n()):(e=document.createElement("script"),e.src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js",e.onload=n,document.body.appendChild(e))}()}).call(this);

    Original Code

    The original code is in coffee:

    (->
      addWindowSize = ()->
        style = 'background-color:azure; padding:1rem; position:fixed; right: 0; z-index:9999; font-size: 1.2rem;'
        $windowSize = $('
    ') getWindowSize = -> '

    width: ' + $(window).width() + ' height: ' + $(window).height() + '

    ' $windowSize.html getWindowSize() $('body').prepend $windowSize $(window).resize -> $windowSize.html getWindowSize() return if !($ = window.jQuery) # typeof jQuery=='undefined' works too script = document.createElement('script') script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' script.onload = addWindowSize document.body.appendChild script else $ = window.jQuery addWindowSize() )()

    Basically the code is prepending a small div which updates when you resize your window.

提交回复
热议问题