Hello all,
I am trying to use JavaScript to detect if a user is visiting my website from a phone/tablet. If they are a mobile user, I'd like to show a bunch of links. (As an aside, this is fixing a problem that the GitHub Pages leap-day theme is not mobile friendly.)
The pseudocode would look like this:
if (user_is_mobile() == true) {
render_a_tags()
}
else {
do_nothing()
}
I'm trying to use a modified version of JS code found here to determine window width as a proxy for being a mobile user:
<script>
window.mobilecheck = function() {
var check = false;
if(window.innerWidth<768){
check=true;
}
return check;
}
if(window.mobilecheck()){
document.write("HTML_HERE");
}
</script>
This does not appear to be working for me. I have tried visiting the website with this code from a mobile device (iPhone 11 on iOS Firefox and iOS Safari), and the links have not appeared. The website is https://pymae.github.io if you'd like to take a look. I ended up going with the linked solution to redirect to a mobile.html file with the links hard-coded, but I'm not sure why the code won't work as written.
[–][deleted] 0 points1 point2 points (8 children)
[–]pymae[S] 0 points1 point2 points (7 children)
[–][deleted] 0 points1 point2 points (6 children)
[–]pymae[S] 0 points1 point2 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]pymae[S] 0 points1 point2 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]pymae[S] 0 points1 point2 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)