Block visitors from internet explorer or microsoft edge [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I need a Javascript script to block internet explorer or microsoft edge visitors to prevent accessing my website on blogger
Edit*
Someone help me and answered the question across using user-agent

First of all, I don't recommend using that on your website. if there is some functionality that doesn't support this browser you can show a notification or popup telling the user to switch his browser to get a better experiment.
but if you see that is the better solution you can check this code example from here to check the user browser:
let userAgent = navigator.userAgent;
let browserName;
if(userAgent.match(/chrome|chromium|crios/i)){
browserName = "chrome";
}else if(userAgent.match(/firefox|fxios/i)){
browserName = "firefox";
} else if(userAgent.match(/safari/i)){
browserName = "safari";
}else if(userAgent.match(/opr\//i)){
browserName = "opera";
} else if(userAgent.match(/edg/i)){
browserName = "edge";
}else{
browserName="No browser detection";
}
document.querySelector("h1").innerText="You are using "+ browserName +" browser";
And this for redirect to another page:
window.location.replace("http://www.EXAMPLE.com");

Every browser and app which access a website has something called a "User Agent". The UA is part of the header that is sent/received with each HTTP call.
Since accessing your website is nothing else than sending a GET-request, you can handle this by checking the user agent in the header and re-route users to a page where you can tell them that they should a different browser.
Or you could entirely block the access by not displaying any elements if the user agent is either Edge or IE.
Here is an example on how to get the user agent:
https://www.w3schools.com/jsref/prop_nav_useragent.asp
If you want a better example this could also help:
https://learn.microsoft.com/en-us/microsoft-edge/web-platform/user-agent-guidance
TL;DR: All you need to do is to check the user agent. You can either do this by pattern matching or simpler by searching if the string contains something that's only there in IE/Edge browsers and act accordingly, e.g. by re-routing or not showing/hiding all elements. (I'd avoid the 2nd suggestion, since it's not user friendly)

Related

Location access isn't being asked on some browsers or security settings so my web app gets stuck loading

I have created a portfolio web app in mainly Javascript which asks for (but dosen't require) location access.
It works fine for me and my tutor signed it off, however someone has said it didn't work for them on safari. I believe their security settings are preventing the access to location question from popping up and it therefore leaves them on the loading screen which is a simple bootstrap spinner.
If location access is blocked the website still works just slightly differently, but it seems that because the location access question isn't being asked on his computer it's getting stuck.
I think basically location access isn't granted or denied and I haven't allowed for this scenario in the code so it's getting stuck.
My question is how to best approach this?
i'm currently using:
if (!navigator.geolocation)
to determine whether location access has been granted or not, ideally there would be an 'or' that I could add to this line e.g:
if (!navigator.geolocation || navigator.geolocation == unknown)
but I'm not sure what would be the correct syntax/keyword for 'unknown' would be?
Hope someone could help, thanks
The best approach is to enclose the geolocation check and run the code that depends on it in an if block with a return, so it exists the function. Run the code that doesn't depend on it outside the block. If the browser doesn't ask or explicitly denies permission, the code should run with no issues.
This is an example
function useGeolocation() {
let div = document.getElementById("Result");
if (Navigator.geolocation) {
div.innerHTML = 'We got geolocation';
//run code that uses geolocation.
return; //We did our work so exit.
}
//gelocation failed, fall back to non-geolocation dependent code.
div.innerHTML='Executing non geolocation code';
}
useGeolocation();

Navigator vibrate break the code on ios browsers

I want to use navigator.vibrate on my page.
This is my code:
var canVibrate = "vibrate" in navigator || "mozVibrate" in navigator;
if (canVibrate && !("vibrate" in navigator))
{
navigator.vibrate = navigator.mozVibrate;
}
$(document).on('click', '.answer', function (eve) {
$this = $(this);
navigator.vibrate(222);
// some other code ...
This works on Android devices but on iOS (I tested on Firfox, Chrome and Safari on some iOS devices) the code will be broken at this line.
Why is that?
Apple's mobile web browser simply does not have support for it.
Firefox and Chrome for iOS are wrappers around Safari's rendering engine.
Quentin is correct that Apple devices do not support the API.
The given code fails to check for vibration support when actually calling the method. To avoid the vibrate function being caught undefined:
const canVibrate = window.navigator.vibrate
if (canVibrate) window.navigator.vibrate(100)
We don't want our app to break down and be unusable on iOS devices.
But we really want to use navigator.vibrate() on Android or wherever possible.
One thing you can do is you can create your own policy over browser policies. Ask "Can we make iOS devices ignore navigator.vibrate()"?
The answer is "Well, yes you can do that by using a user agent parser."
(Such as Faisal Salman's UAParser to detect if the user's device was an iOS or Mac OS device.)
In your code, wrap all the navigator.vibrate() calls inside conditions like,
if(nameOfUsersOS != "iOS" && nameOfUsersOS != "Mac OS") { navigator.vibrate(); }
Note: You must replace nameOfUsersOS with your own variable name.
Note: This is only one possible approach. Policy makers of Apple can and sometimes do change their minds. That means in the future they could allow the good Vibration API just like they allowed the Web Speech API recently. You must use the solution in kotavy's answer unless your policy is like "no vibration for Apple users forever".

How do we check if a browser's auto-update is enabled through javascript

I want to check form the javascript if a browser's auto-update is enabled.
The reason I want this is that our software supports only a set of Browser versions and we want to alert them if auto-update is on.
Is there any way we can do this?
Abhi
No. Browsers are highly sand-boxed and will give you almost no information about the users system.
You'll instead have to just do something like check the version and alert them if it isn't up-to-date, with maybe some information on how to check if they have auto-updated turned on.
Not sure why you require a set version, but one thing to do is check if the browser supports a specific method (One that your app requires).
If not, tell them to update their browser. This example test if the user has WebSockets:
if (window.WebSocket) {
alert("BROWSER SUPPORTED");
} else {
alert("BROWSER NOT SUPPORTED");
}

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";
}

Any way to detect URI schema is available in windows using JavaScript in IE

I found few ways to detect where window support current URI scheme for Firefox and Chrome browser, if application not registered i will download and register those application uri
Chrome
var appWindow = window.open('alert:"Hello%20World"',"_self");
if(appWindow!=null){alert("it is worked")}
Firefox
$("body").append('<span id="__protoProxy"></span>');
function queryWord(aWord)
{
var protoProxy = document.getElementById('__protoProxy');
if (protoProxy)
{
var word = aWord.replace('"','\"');
protoProxy.innerHTML = '<div style="display:none;"><iframe id="iframe01" src="alert://'+ word + '"></iframe></div>';
}
}
queryWord('hello world');
if(document.getElementById('iframe01').contentDocument.body.innerHTML!=""){alert("it is worked")}
IE
but in IE i am unable to perform this action even though URI is not registered in windows, IE open window to select app from windows store.
Is there any way to detect in IE ?
Is there any way to detect it commonly across all browser ?
How does citrix launcher works in all browser?
This question appears to be a duplicate, however since there is a bounty I receive an error that it cannot be closed.
In any case, check out this thread and see if it works for you.
Link.

Categories

Resources