I am using the following JavaScript to detect the screen-width and use it as a constant across my template files through conditional statements to display / not-display portions of my site. While it has nothing much to do with my questions, but just in case… Yes I am using WordPress. Also I am already using mobiledetect PHP Library.
function getLayoutWidth() {
if (isset($_GET['width'])) {
define( "SCREEN_WIDTH", $_GET['width']);
} else {
echo "<script language='javascript'>n";
echo " location.href="${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}"
. "&width=" + screen.width;n";
echo "</script>n";
exit();
}
}
Important :
-
Other techniques are like…… Let’s assume if my site is 2MB in size, it will still load 2MB of content and then hide 1.5MB of it on mobile devices by using CSS properties like
display:none;
Whereas I don’t want that template part to load itself, thus not needing to hide anything. -
I am not looking to load an entire JavaScript library like jQuery or so to do this because
location.href="${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}". "&width=" + screen.width;
is the only JavaScript in my entire site. Rest everything is pure PHP, HTML and CSS based. I even disabled jQuery of WordPress in the front-end template.
UPDATE :
Since some friends did not get my point although I mentioned clearly that I don’t want to load the content into DOM at all, I am giving a bit more clarity here…..
For Example — @1200px I want only 1 Sidebar to be displayed. @1600px I want 2 Sidebars to be displayed and over 1600px I want 3 Sidebars to be displayed. Please don’t suggest Media Queries solutions as I already know it and that is exactly what I don’t want to do. I want to avoid loading the content into DOM. Kindly let focus be only and only on questions asked. Also please don’t post suggestions as answers. Let others with proper answers do it. Kindly post suggestions in Comment section.
My questions :
-
Is this a good / correct way to do from SEO stand point? If not why?
-
My URL is displayed as
example.com/my-product-category/my-product-name/?width=1440
How to remove/?width=1343
and display justexample.com/my-product-category/my-product-name
part?
5
Answers
You can retire the $_GET of the URL by the htaccess, I’m using RewriteRule.
To remove all php files extension: php.net.
Edit: this is for who doesn’t use WordPress
You can use this plugin:
https://wordpress.org/plugins/mobble/
It adds an
is_mobile()
-feature.In your theme you can now use
is_mobile()
instead of$_GET['width'] > ...
This way you don’t need a
?width=
in your$_GET
To set a cookie in javascript
In PHP
Simple answer, no, it is not good from a SEO standpoint. Or any other standpoint. Crawlers such as Googles are designed to completely ignore all
hidden
elements and thus you will lose big time SEO ranking if your content isnt getting fully crawled, and crawlers crawl each site multiple times masquerading as mobile devices to check if the site is mobile friendly as well.http://www.seonick.net/responsive-design-seo/
Not to mention the trouble of calculating your arbitrary cutoff point of .5mb serves no purpose if the content is merely hidden (since its all getting sent anyway thus saving no bandwidth).
You need to do this in pure CSS using media queries, it is the most compatible way and allows for a fluid design (changes on the go as the window resizes.
That will use one css file if the window is smaller than 700px and the other if it is over.
Another of my more favorite methods is to use the http://mobiledetect.net/ class. Its small and fast, more accurate and better flexibility. Load that class then just add classes to your body element depending on the visitors browser
Then style by targeting classes inside
body.phone
. This method also ensures you know if the browser is mobile BEFORE the DOM starts to process, meaning you can serve compressed versions of images through some simple logic rather than having CSS swap them out or just resize them or omit entire parts of the markup from being sent to the user at all ensure bandwidth is only used for parts of the DOM relevant to the users device.I do not think, that screen size/resolution is really what you want to adjust your view templates for mobile devices. Actually you want to know what plattform/device someone is using, think about Nexus 7, which has 1920 × 1200 or Sony Xperia Z5 with 2160 x 3840px.
I would look at user-agent and HTTP headers for the server side code, there are already good libraries for that: e.g. mobiledetect. For the client side the best practice is to use CSS3 Media Queries: Media Queries for Standard Devices