UserAgent Switcher to mobile web - javascript

I am using javascript as useragent to redirect main website to mobile website. but i can not switch to desktop view in mobile device.
Any ways to redirect to the main website on mobile device by link "Full Website"?
This is javascript i am using:
<script type="text/javascript">// <![CDATA[
var mobile = (/iphone|ipad|ipod|android|blackberry|mini|windowssce|palm/i.test(navigator.userAgent.toLowerCase()));
if (mobile) {
document.location = "/mobile";
}
// ]]>
</script>

Add this as link:
Desktop version
And the javascript (you need to implement the code mentioned as comments):
function goToDesktopVersion(){
// 1.) set a cookie to remember you want the deskop version
// 2.) set window.location to your desktop version
}
And consider the cookie in your detection code (implement commented code):
function keepDeskopVersionCookieIsSet(){
// find out if the cookie is set and return true or false
}
...
var mobile = ...
if (mobile && !keepDeskopVersionCookieIsSet() ) {
document.location = "/mobile";
}
The cookie is needed in order that the mobile client will not get redirected again to the mobile version after the "Desktop version" link was clicked.
A cookie is a small piece of data which is stored on the client's browser to keep some information. In this case, this is the information that the user wants to keep the desktop version of your page. Cookies are always sent between server and client to each other, so you can the set on the client (browser) or on the server as well. In a browser, you can set a cookie using Javascript. Instead of writing all the code from scratch which is needed to save the cookie, I would recommend to use some existing helper code which does the work for you.

Related

Html is there a way to send a concat from desktop to mobile phone?

On my website i have button that triggers SMS to my phone. And it works fine for mobile users.
Start Chat
But what can i do with desktop users? Is there a way to send this contact to their phone? or what is common approach about that?
You could use something like an SMPP protocal to send sms via a server side script like PHP. Along with some tricks...
Example PHP SMPP lib Code: https://github.com/onlinecity/php-smpp
Things to do:
Find an smpp provider. Possible free one ->
https://smppex.rubybox.ru/
Detect if the browser is desktop or phone:
var pathname = $(location).attr("pathname");
if (
/android|webos|iphone|ipad|ipod|blackberry|nokia|opera mini|opera mobi|skyfire|maemo|windows phone|palm|iemobile|symbian|symbianos|fennec/i.test(
navigator.userAgent.toLowerCase()
)
) { /* mobile code */ } else { /* desktop code */ }
Then dverride the default behaviour of the link to a click event in Javascript: Override default behaviour for link ('a') objects in Javascript
Desktop does not support sending SMS messages.
Unless you have an application to send messages on your device, if you have an application, you will need to open the application, you can check this link
how to start up a desktop application in client side
But there are other ways such as sending an email and you can do this
start email
and u can start chat in whatsapp like this :
start whatsapp

javascript redirect only pc users not mobile

I am trying to redirect pc users who are using adblock to a certain page.but i dont want to redirect mobile users.
here is my code
<script src="/assets/js/ads.js" type="text/javascript"></script>
//the bait for adblock
<script type="text/javascript">
if(document.getElementById('ElvJCLbfcHDP')){
alert('Blocking Ads: No');
} else {
alert('Blocking Ads: Yes');
}
as you can see this only shows if the ads are blocked or not.but what I want to do is check if users are coming from mobile or PC then redirect only PC adblock users to a certain page and let mobile users use site as it is.
i found this
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
// some code..
}
but as u can see it only detects if users is from mobile and then run the code.i want it to check if user is from PC and then run the redirect
Use ! logical not operator to alter the statement
if(!/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
// Desktop / pc
}
Don't try to redirect based on a test for a device.. You'll spend every moment of your time updating the list and wondering why some devices that are on your list are getting through. navigator.userAgent is notoriously unreliable.
From MDN:
Deprecated This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of
being dropped. Avoid using it and update existing code if possible;
see the compatibility table at the bottom of this page to guide your
decision. Be aware that this feature may cease to work at any time.
The NavigatorID.userAgent read-only property returns the user agent
string for the current browser.
The specification asks browsers to provide as little information via
this field as possible. Never assume that the value of this property
will stay the same in future versions of the same browser. Try not
to use it at all, or only for current and past versions of a
browser. New browsers may start using the same UA, or part of it, as
an older browser: you really have no guarantee that the browser agent
is indeed the one advertised by this property.
Also keep in mind that users of a browser can change the value of this field if they want (UA spoofing).
Typically, a desktop can be rooted out simply by the width of the window (as measured in CSS pixels, not hardware pixels).
if(window.innerWidth > 1280){
location.href = "desktop path";
} else {
location.href = "mobile path";
}

Redirect from Sharepoint Site to Mobile Site Not Functioning for Android Users

I'm working with a SharePoint site in which the pages display poorly in mobile, so much so that it's essentially nonfunctional. I'll be rebuilding the site to solve those issues, but in the meantime, I'd like to redirect mobile users to a dedicated mobile site that's built outside of SharePoint. The problem is that my redirects are working fine for iPhones and not working for Android OS.
I've tried the following in the master page:
<script type="text/javascript">
if (screen.width <= 699) {
if (document.referrer.indexOf('http://site.html') == -1){
document.location = "http://site.html";
}}
</script>
and
<script type="text/javascript">
if(/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)){
if(document.URL !="http://site.html")
{
window.location ="http://site.html";
}
}
</script>
AND
<script type="text/javascript"> // <![CDATA[
if ( (navigator.userAgent.indexOf('Android') != -1) ) {
document.location = "http://SITE.html";
} // ]]>
</script>
I understand that Android users need to manually enable JavaScript, so I assume that's probably the issue. Any recommendations for alternatives?
Thanks.
When no javascript is available on Android, you need to move the redirect logic to the server. You have multiple options there (building your own HTTP Module, User Control or WebPart for SharePoint).
One solution which runs without development skills is using the IIS Rewrite Module (http://www.iis.net/downloads/microsoft/url-rewrite). You can configure it on user agent, see for instance IIS 7.5 URL Rewrite rule to handle request based on user agent.

Javascript mobile site redirect issues

I'm using code to redirect mobile devices that are under 699 pixels wide to go to our mobile site. This method utilizes JavaScript and cookies and follows some basic logic:
Do not run logic for redirect if cookies are disabled
Do not redirect if cookie skipmobile is set to 1
Redirect only if skipmobile is not 1, and your mobile device is listed below and under 699 pixels wide.
//{{Full Site Code}} Only run logic if cookies are enabled.
if(navigator.cookieEnabled){
//If the cookie skipmobile is already set do not redirect to mobile.
if (document.location.search.indexOf("skipmobile") >= 0) {
document.cookie = "skipmobile=1";
}
//If the device is one of the types listed below and is under 699 pixels wide, redirect to the mobile site.
else if (((/Android|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) && screen.width < 699)
&& document.cookie.indexOf("skipmobile") == -1)
{
document.location = "'.MOBILE_SITE.$direct.'";
}
}
On the mobile site I simply have a link like the following that someone can click to set the cookie
http://www.example.com?skipmobile=1
This code works properly for me and most people, but we are having customers saying When they click the full site link it sends them right back to the mobile site. According to the code this means they do have cookies enabled but their cookie isn't getting set.
Is there something I need to do to this code that's missing?
UPDATE: So this problem is a bit of an oddball. One of our employees is having the issue as well so we at least have a phone to test on. We have a live site and a dev site. It works for him of we go to the dev site and redirect but it doesn't for the live...
Does this help anyone come up with conclusions? The code is the same on both sites.
You should try deleting all cookies related to your site beforehand, as this should clear up any problems. This is a link to a great function that should do this for you:
Clearing all cookies with JavaScript
You could also put that you don't want it to cache for a while in the headers (using the cache control var) to make sure that if the phone is storing any problems these are removed

Mobile website redirect, Full Site link, without cookie. Cookie seems to prevent return to mobile site

I'm testing a few JavaScripts that redirect mobile users from a main website to a mobile website. What I have found uses cookies however, and the cookies seem to prevent returning users from going directly back to the mobile website, without clearing the browser's cookies, not just closing browser.
Can I do this with a variable instead of a cookie? Or PHP?
This simple script looks like it uses use a file value stored, but I can't get it to work.
<script>
if (document.location.search.indexOf("skipmobile") >= 0) {
document.cookie = "skipmobile=1";
}
else if ((document.location.hostname.match(/\.mobi$/) || screen.width < 699)
&& document.cookie.indexOf("skipmobile") == -1)
{
document.location = "mobile/";
}
</script>`
Mobile side link to full site has this ending:
http://www.domain.com/?skipmobile=1`
Any suggestions appreciated.
I chanced upon this article that you have, I am Neil and I work for handsetdetection.com. Just to let everyone of your reader know that there is also another way of redirecting your viewer to a mobile site that you have and automatically adjust the screen size and buttons to whatever type of mobile device they are using. However, having a mobile version of your site is not enough, you have to redirect your visitors from your main website to a mobile version of your site. It is called handsetdetection and it is quite easy to install as a plug in to any back end of your site.
Hope this helps,
Neil Summers

Categories

Resources