Screen.width

1,378 views
Skip to first unread message

Chris Angel

unread,
Mar 24, 2012, 8:27:31 PM3/24/12
to phantomjs
Hello,

whenever a webpage's javascript is asking for screen.width and
screen.height, then phantomjs is showing the values of my pc, even i
have set viewportSize to something else. Is there a way to simulate my
screen size ?

regards

chris

Ariya Hidayat

unread,
Mar 24, 2012, 10:18:20 PM3/24/12
to phan...@googlegroups.com

There is no support for that right now. I think it's a good feature to
have, please file a feature request in the issue tracker.

Meanwhile, try to use the onInitialized handler to "fake" the screen
object, one existing example from mine:
http://ariya.ofilabs.com/2011/10/detecting-browser-sniffing-2.html.


Thank you!

Best regards,

--
Ariya Hidayat, http://ariya.ofilabs.com

Ivan De Marino

unread,
Mar 25, 2012, 2:12:12 PM3/25/12
to phan...@googlegroups.com
Wait, I think there is nothing wrong here.

The object "screen" refers to the "screen". Phantom is a browser, so it has to implement the interface.
The fact that it's reporting you your screen resolution, it's what it should do.

What you want to read is the VIEWPORT SIZE (that, yes, yourself have set).
A quick search on google returned a good tutorial: http://andylangton.co.uk/articles/javascript/get-viewport-size-javascript/

Unfortunately, those are the kind of API that never got standardized: every browser has a way to read the Viewport, but there are different ways per browser.
Javascript "ironing" libraries have been the ones to close gaps.
But it's simple enough to write a generalization function that works everywhere (see article above).

So, in short: read the viewport, not the screen size. Those are 2 different things.
Ivan
--
Ivan De Marino
Coder, Technologist, Cook, Italian

blog.ivandemarino.me | www.linkedin.com/in/ivandemarino | twitter.com/detronizator

Ariya Hidayat

unread,
Mar 25, 2012, 6:02:48 PM3/25/12
to phan...@googlegroups.com
Some library, notably for mobile platforms, also detect and use the
screen dimension, usually to handle or workaround orientation issue.
Example: jQuery Mobile. Thus, I think it's valid to have a support for
screen object manipulation.

OTOH if onInitialized works well for such uses cases, then there is no
really a desperate need to construct a new public API.

Regards,

Ivan De Marino

unread,
Mar 25, 2012, 6:50:24 PM3/25/12
to phan...@googlegroups.com
So you are proposing an API that, when setting the screen, actually sets the viewport too?
And while the viewport has actual effects on the browser rendered surface, screen really doesn't (we don't have one :) ).

But overriding the object can still be a good idea too.

Ariya Hidayat

unread,
Mar 25, 2012, 10:07:02 PM3/25/12
to phan...@googlegroups.com
> So you are proposing an API that, when setting the screen, actually sets the
> viewport too?

They are different. In the normal desktop, viewport is the browser
window while screen is the monitor.

In all cases, override of the screen object might be sufficient and
there is no need for the API.


> And while the viewport has actual effects on the browser rendered surface,
> screen really doesn't (we don't have one :) ).

The effect will be indirect, as in the case with e.g. jQuery Mobile.

Chris Angel

unread,
Mar 28, 2012, 6:49:26 PM3/28/12
to phantomjs
hello,

i have no luck with

window.screen.__defineGetter__("width", function() { return 1234;});

nor

screen.__defineGetter__("width", function() { return 1234;});

any suggestions ?

Ariya Hidayat

unread,
Mar 28, 2012, 8:58:31 PM3/28/12
to phan...@googlegroups.com
> i have no luck with
> window.screen.__defineGetter__("width", function() {  return 1234;});

I assume you haven't redefine completely the screen object, it won't
work that way.

Here is a complete example:

var page = require('webpage').create();

page.onInitialized = function () {
page.evaluate(function () {
(function () {
window.screen = {
width: 1000,
height: 500
};
})();
});
};

console.log(JSON.stringify(page.evaluate(function () { return
window.screen })));
phantom.exit();

Reply all
Reply to author
Forward
0 new messages