I created a very simple mobile version for my website. I switch to it by detecting the visitor's viewing width of their screen. If it's less than 400 pixels wide, it switches the visitor to the mobile version.
Here's the question...
I want to give the visitor the option of swtiching back to the normal website. However, I can't just put a link to the normal website because it would detect their viewing width and send them right back to the mobile version.
What's the best method to allow them to switch back and forth between the mobile version of the website and the normal version of the website?
Based on what I've read, I should install a cookie that toggles each time the visitor changes their preference between the mobile version and the normal version of the website.
The problem is I have no experience creating a cookie and it's a little more complex than I anticipated. With that said, here's specifically what I want to do...
Using Javascript, I want to write a cookie that has a variable called mobileSwitch.
By default, I want the value of mobileSwitch to be 'off'.
I want to include a button on the page that will turn the value of mobileSwitch to 'on' and then take the visitor to the mobile version of my website.
I did end up using cookies and writing a little Javascript code to handle the cookie. But during my research, I did find another option. I didn't do a lot of research into it, but I think "sessions" in PHP can do the trick without cookies.
I did end up using cookies and writing a little Javascript code to handle the cookie. But during my research, I did find another option. I didn't do a lot of research into it, but I think "sessions" in PHP can do the trick without cookies.
I would not be so sure. A session variable is active only as long as the user remains within the same domain. If the user closes his browser or he quits the domain, the session is gone, so that no information are stored. Except, maybe, if you use a login user/password and a database, but this is another discussion.
But you may use PHP cookies, if you feel more comfortable than with JavaScript cookies.
It may not be legitimate (certainly not "professional"), but an easy option would be to have link back to alternate full-scale page that does NOT have a Javascript detecting viewer's screen size
You can avoid using cookies by using GET variables to control to auto-sensing. E.g In PHP:
Mobile website:
---------------------
<body>
<a href="index.php?auto=N" >click for normal version</a>
</body>
Normal Website:
----------------------
<body>
<?php
error_reporting(E_ALL ^ E_NOTICE);
if ( $_GET['auto'] == NULL ) { $auto='Y'; }
else { $auto=$_GET['auto'];
}
if ($auto=='Y') {
/* auto sensing code goes here */;
} ?>
<!-- continue here -->
</body>
Note: This is just an example of using GET variables. It's written in PHP bcause I'm more familiar with that than JavaScript. If the auto-sensing is in JS, it would make sense to do this in JS.
Bookmarks