As I've understood it, there are two supposedly reliable methods of determining the viewport width and the screen width on mobile displays. The viewport width is the virtual pixels, and screen width is the physical pixels. That's how it's supposed to be. But it's all the same for me. I've tested the following page with Android 4.0.4's stock browser and Chrome for Android in its current version (it won't tell me its number).
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="info"></div>
<script type="text/javascript">
setTimeout(function () {
var virtualWidth = document.documentElement.clientWidth;
var physicalWidth = screen.availWidth;
document.getElementById("info").innerHTML =
'Virtual width: ' + virtualWidth + '<br>' +
'Physical width: ' + physicalWidth + '<br>' +
'Ratio: ' + (physicalWidth / virtualWidth);
}, 500);
</script>
</body>
</html>
Here's the results:
The stock browser reports 540 for both values, Chrome reports 360 for both. The ratio is always 1. My phone is a Motorola RAZR i, and it should have a physical width of something around 540 (portrait orientation) and a ratio of 1.5. What's wrong with this page or the method, or the browsers? It's really hard to do mobile webdesign if the tools are totally unreliable. (And I'm not yet speaking of the random text size in Chrome...)
Update: Here's a live URL for you to do your own testing. Get a QR code of it.
Related
I have a HTML code as:
<!DOCTYPE html>
<html lang="en">
<head>
<title>test page</title>
<style>
.hint {width:510px;}
#media only screen and (max-device-width: 720px) {
.hint {width:280px}
}
#media only screen and (max-device-width: 720px) and (orientation: landscape){
.hint {width:350px}
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<div class="hint">just a test
<script>alert($(window).width()); alert($('.hint').css('width'));</script>
</div>
</body>
</html>
It works fine on all iOS devices but behaves weird for some Android devices like Samsung Galaxy Note 4 as I can reproduce the issue.
For the default browser, it works fine. The alert result is 980px and 280px but for some browsers like UC browser, it's 510px.
Is there anything wrong with my media query code?
Thanks for any kind of tips!
You should take a look at how "Twitter Bootstrap" outlines their media query structure at http://getbootstrap.com/css/#grid-media-queries
Try comparing those with your own written media query structure, and if you are still stuck, you should check out Chris' written media query set at https://stackoverflow.com/a/21437955/4069464
Hope this helps!
Note: If after you have setup all the proper media query sets, and somehow this is still not working for the UC browser, then you probably have to read out in detail on how UC browser defines their mobile screen size (possibly they might have defined themselves a different set of definition of screen sizes)
So I've been trying this for a while now, and I can't seem to figure out the problem. I have an input
<input type="datetime" class="nativedatetimepicker" placeholder="somestuff" name="datetime-la" id="datetime-la"/>
And all I want to do is set the value for it
$("#datetime-la").val(anytime);
I've tried a variety of different values for any time like new Date(), Date().toISOString(), Date().toString() etc. Nothing works. However this code works fine with iOS as well as Android 2.33 - 4.04 on all other devices.
Now if I leave it blank (which is undesireable) it can set the date and time with whatever build in function cordova has and it sets the value of the input correctly. Am I missing something here? How come this issue only comes up with the Samsung Galaxy S3 and no on other phones such as the Atrix HD.
Update:
All right I tried just making it in a simple webpage, and I believe this is an issue with the device. This is the code I tried, and it didn't work:
<!DOCTYPE html>
<html>
<head>
</head>
<body onload="load()">
</body>
<script type="text/javascript">
function load(){
document.getElementById('datetime-la').value = "something";
}
</script>
<input type="datetime" class="nativedatetimepicker" name="datetime-la" id="datetime-la" style="width:50%;height:200px;"/>
</html>
I am working on a web app which has a width of 640px.
In the document head I set
<meta name="viewport" content = "width=640, user-scalable=no" />
so the content is nicely displayed and stretched horizontally.
This works perfectly on iOS but in Android the browser opens the website zoomed in so the user has to double click to zoom out and the entire page.
When I change the viewport setting to leave out the user-scalable tag like this:
<meta name="viewport" content="width=640" />
the Android browser adjusts nicely to the 640px - so it works.
The problem however now is, that users can zoom in and out on Android and iOS since the user-scalable tag is not set.
How can I forbid the scaling and at the same time set the viewport width to 640px on Android?
Trying rendering the viewport meta tag like so:
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
Setting scale settings will set user restrictions on how far they can zoom, and so if you set the initial and maximum to the same amount, this should fix the problem.
UPDATE: I was able to fix my bug for android devices all together by setting the below:
<meta name="viewport" content="width=640px, initial-scale=.5, maximum-scale=.5" />
I also noticed that some content, such as p tags were not flowing across the screen, so the hack for that would be to add the background-image property with empty string to any content that is stuck and is not going across the layout view. Hope this helps this time for you.
I wanted mobile to always show a website 640px wide because of a design that would break otherwise. (a design I did not make..) Thereby I wanted to disable zooming for mobile users. What worked for me me is the following:
- UPDATED 2013-10-31
First of all, there is no way you can do this without Javascript. You will have to check the user agent string. Therefore I created a mobile-viewport.js and included the script just before the closing tag:
function writeViewPort() {
var ua = navigator.userAgent;
var viewportChanged = false;
var scale = 0;
if (ua.indexOf("Android") >= 0 && ua.indexOf("AppleWebKit") >= 0) {
var webkitVersion = parseFloat(ua.slice(ua.indexOf("AppleWebKit") + 12));
// targets android browser, not chrome browser (http://jimbergman.net/webkit-version-in-android-version/)
if (webkitVersion < 535) {
viewportChanged = true;
scale = getScaleWithScreenwidth();
document.write('<meta name="viewport" content="width=640, initial-scale=' + scale + ', minimum-scale=' + scale + ', maximum-scale=' + scale + '" />');
}
}
if (ua.indexOf("Firefox") >= 0) {
viewportChanged = true;
scale = (getScaleWithScreenwidth() / 2);
document.write('<meta name="viewport" content="width=640, user-scalable=false, initial-scale=' + scale + '" />');
}
if (!viewportChanged) {
document.write('<meta name="viewport" content="width=640, user-scalable=false" />');
}
if (ua.indexOf("IEMobile") >= 0) {
document.write('<meta name="MobileOptimized" content="640" />');
}
document.write('<meta name="HandheldFriendly" content="true"/>');
}
function getScaleWithScreenwidth() {
var viewportWidth = 640;
var screenWidth = window.innerWidth;
return (screenWidth / viewportWidth);
}
writeViewPort();
The script checks if the visitor has an android (not chrome) or firefox browser. The android browser does not support the combination of width=640 and user-scalable=false, and the firefox browser does have a double screen width for some strange reason. If the visitor has a windows phone IE browser MobileOptimized is set.
I had the same situation, if you want the content to always fit the screen width without allowing the user to zoom in/out, use the following meta tags (this will work no matter what width you give)
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
I am working on an app for android tablets using phonegap. I am converting an existing webpage, so I would prefer to not change the css or html. I am testing using a galaxy tab (android 2.2)
I want to set the viewable width to be 800px and disable the user from zooming in/out.
If I use <meta name="viewport" content="width=800px>, then the width is set correctly but the user can still scale the page.
If I use <meta name="viewport" content="width=800px, user-scalable=no">, then the user can't scale the page, but the width will be smaller (ie. the page is zoomed in, and the user has to scroll around to view the page)
The only other thing I can think of is using <meta name="viewport" content="minimum-scale=x, maximum-scale=x"> where x is some magical number which I don't know how to find.
Any help would be appreciated.
this worked for me:
<script>
$(document).ready(function(){
var vpScale = window.outerWidth/800/window.devicePixelRatio;
var metas = document.getElementsByTagName('meta');
var i;
for (i=0; i< metas.length; i++) {
if (metas[i].name == "viewport") {
metas[i].content = "minimum-scale=" + vpScale + ", maximum-scale=" + vpScale;
}
}
});
</script>
The webserver wants to get info like screen size from each mobile handset that browses a webpage. The Javascript functions screen.width and screen.height return wildly inaccurate values.
Is there a way for the webserver to detect the screen size of the mobile handset? The client browser is webkit on Android.
You could try using CSS media queries, which should hopefully use the correct values.
<link rel="stylesheet" type="text/css"
media="only screen and (min-device-width: 320px)"
href="logResolutionScript?width=320" />
Use basically the same rules for other width and heights and a cookie to check if a client loads more than one stylesheet to get the correct value. You won't get the exact resolution, but it should be close enough. You can also check for orientation and use combinations of max-/min-(device)-width. Your file might end up with quite a lot of css-imports, but you should be able to pin down the resolution of the client quite accurately, unfortunately at the cost of a few HTTP-requests.
CSS3 Media Queries (Specification)
I found this article useful which mentions a meta tag that affects android and iPhone browsers which did what I needed:
<meta name="viewport" content="initial-scale=1.0">
From Mislav's article: What Mobile Safari does by default (i.e. without this directive) is display a zoomed-out, 980px-wide version of the page even if the layout itself is narrower. As content authors, with this directive we're saying "trust me, zoom to natural scale and I'll make sure it fits"
It seems to make the screen size and width values correct when read from JavaScript because the page is not zoomed (and if page is larger you can still scroll it). Alternatively there is probably a javascript variable to read out the zoom level.
For the iPhone this is documented here.
On Android you can work out the current zoom by adding an absolute div to the body of width 100%, and dividing the div's offsetWidth by window.innerWidth.
var iPadMeasureWidthNode = window.iPadWNode;
if (!iPadMeasureWidthNode) {
iPadMeasureWidthNode = window.iPadWNode = document.createElement('div');
// .ipad-measure-w {position:absolute; width:100%; top:-1px}
iPadMeasureWidthNode.className = 'ipad-measure-w';
document.body.insertBefore(iPadMeasureWidthNode, document.body.firstChild);
}
var zoominverse = 1000 / Math.round(1000 * iPadMeasureWidthNode.offsetWidth / window.innerWidth);
You can keep an element at 1:1 zoom by inverting (undoing) the amount of zoom:
// Not using scale3d because is hardware zooming which is ugly unreadable blurry pixel magnification.
node.style.webkitTransform = (zoominverse > 1) ? 'scale(' + zoominverse + ')' : '';
node.style.webkitTransformOrigin = (zoominverse > 1) ? '0 0 0' : '';
Zoom change is detected by window.onresize event (although resize event is delayed until after resize is completed... you can detect zoom start using the gesturestart event on iPad, or document.touchstart event and detect 2 fingers down).
Edit: After three corrections saying it doesn't work, I thought I better add an example showing it working. Tested works on: Android 4.1.2 normal browser, Android 4.1.2 Chrome, Android Opera Mobile 12.10, iPad 2 iOS4. (Didn't work on Android Firefox Mobile, and won't work in an iframe so jsfiddle won't work).
<!DOCTYPE html>
<html>
<head>
<style>
.ipad-measure-w {
position: absolute;
width: 100%;
top: -1px;
};
</style>
</head>
<body>
<button onclick="alertWidth()">alertWidth</button>
<div style="width: 1600px; height: 100px; background-color: blue;"></div>
<script>
function alertWidth() {
var iPadMeasureWidthNode = window.iPadWNode;
if (!iPadMeasureWidthNode) {
iPadMeasureWidthNode = window.iPadWNode = document.createElement('div');
iPadMeasureWidthNode.className = 'ipad-measure-w';
document.body.insertBefore(iPadMeasureWidthNode, document.body.firstChild);
}
var zoominverse = 1000 / Math.round(1000 * iPadMeasureWidthNode.offsetWidth / window.innerWidth);
alert(zoominverse);
}
</script>
</body>
</html>