2

i added this to the head of my page:

<script type="text/javascript">

if (screen.width <= 800) {
    window.location = "http://m.domain.com";
}

</script>

i have a button on my mobile version called "Main Site". I want the button to go to my main site, but not redirect back to my mobile site. How do i do this?

also, i only want them to go there once. So, the next time they enter my website name, i want it to take them to mobile site

Hunter Mitchell
  • 7,063
  • 18
  • 69
  • 116

4 Answers4

2

If you already have a button to go your main site. Just link the main site to that button. What you can do extra is, use an url parameter to check and force the main layout. Something like

<a href="http://www.domain.com?forcelayout=main">Main site</a>

Check for this parameter, when you want to force the main layout. Or else, the script currently using will automatically do what you want to do.


Use the following function to read the query string. [Source]

function getParameterByName(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)')
                    .exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

Next you can use it in your logic, like this:

if (screen.width <= 800 && getParameterByName('forceLayout') != 'main') {
    window.location = "http://m.domain.com";
}
Community
  • 1
  • 1
Starx
  • 77,474
  • 47
  • 185
  • 261
  • well, i only have a button – Hunter Mitchell Jul 05 '12 at 02:33
  • so, that is all i have to do, just put that there and it forces the mobile user to go to the main site? – Hunter Mitchell Jul 05 '12 at 02:34
  • No, you have to change your script to bit, take consideration of the `forceLayout` URI Parameter. – Starx Jul 05 '12 at 02:34
  • so, i have this: and on my mobile site on the button: Main site – Hunter Mitchell Jul 05 '12 at 02:43
1

You could inspect document.referrer

<script type="text/javascript">
if (screen.width <= 800 && document.referrer.indexOf('http://m.domain.com') != 0) {
    window.location = "http://m.domain.com";
}
</script>
Musa
  • 96,336
  • 17
  • 118
  • 137
  • what would i put as the link href=""? – Hunter Mitchell Jul 05 '12 at 02:27
  • also, i only want them to go there once. So, the next time they enter my website name, i want it to take them to mobile site – Hunter Mitchell Jul 05 '12 at 02:28
  • If the person came from the mobile site the referrer will be `http://m.domain.com/whatever` so `if (screen.width <= 800 && document.referrer.indexOf('http://m.domain.com') != 0)` will evaluate as false and not redirect. If you want the button to be the only was to go to the mainsite you might be better off using cookies. – Musa Jul 05 '12 at 02:35
1

When someone clicks on your "Main site" button, before you redirect to your main site, create a "MobilOptOut" cookie and then test for the presence of that cookie in your screen.width if statement.

HeatfanJohn
  • 7,143
  • 2
  • 35
  • 41
  • @Starx I like your solution using a parameter better as there is a comment that "I only wan them to go there (main site) once". A cookie will make them always redirect to the main site until the cookie is deleted. My website uses both techniques. We use a parameter but also set a cookie from our back end after it checks the User Agent String for mobile browsers. – HeatfanJohn Jul 05 '12 at 02:50
  • I dont have any objection about your answer. It is really a good concept. I just want your answer to be a little bit useful to the OP and future visitors. SO, please complete your answer :) by giving a small example of what you are suggesting. – Starx Jul 05 '12 at 03:47
0

The client needs to manage the state of the current view the user wants to see. Since HTTP is a stateless protocol, you can use a cookie to persist the user's selection across HTTP requests.

When the user clicks the button to display the main version, delete the cookie as part of the click event. Likewise, set the cookie to your desired value when the user wants to view the mobile version. Since there are many ways to manipulate cookies with JavaScript that may be library dependent, below is pseudocode of what your page should do.

// Pseudocode:
// Sets a cookie named "view" with the value "mobile"
function setMobileView() {
    Cookie.set("view", "mobile");
}
mobileViewButton.onclick = setMobileView;

// Deletes the cookie named "view"
function setMainView() {
    Cookie.delete("view");
}
mainViewButton.onclick = setMainView;

Here is pseudocode for what your redirection logic would look like:

// Pseudocode for deciding the view to display
if (view === "mobile") {
  location.href = "http://m.domain.com";
}
Andy
  • 2,154
  • 3
  • 20
  • 16