last day (14 days later) » 

2:46 PM
0
A: Javascript Redirect Not Going To Correct URL

ShaiYou're comparing window.location.hostname to mob. Have you tried logging both to see what's happening when your width is < 1024? console.log(window.location.hostname); // -> website.com console.log(mob); // -> website.com/m/ As you can see, window.location.hostname is only...

 
Ok, thanks, but now it does nothing! when I load the page at less than 1024 it just stays on the .com site
 
@user3177012 Post your new code. Also post a console.log of any variables you have used in your new code, so we/you can see what comparison is being made.
 
Just have done!
 
@user3177012 You haven't posted the log so we can see what values are being compared. You also, presumably, only want to compare the beginning of the href, not the whole thing. Do some logging and you'll see what is going on for yourself (or, if not, you'll help us to help you).
 
I don't know how to log - My console says window.location.href is not a function
 
2:46 PM
@user3177012 If you don't know how to console.log, Google how do i console.log. We aren't your personal Googlers. Your error message is because you've written window.location.href(...). href is not a function, so you can't pass parameters to it. You can leave that line as window.location.replace(...), like you had it before.
 
I'm not asking anyone to Google anything. I've edited my original question as now it keeps refreshing the page
 
Still can't help you as you haven't given any clue to the value of window.location.href - which, again, you can obtain using console logging. But if I had to guess, you probably have something after the .com/m/? So location.href does not totally equal mob, correct? So the redirect will keep happening. Am I along the right lines?
 
No, there is nothing after .com/m
Is there another way of doing this? I had it working at one point but that was a few months ago when I was doing another project
I simply want it to redirect if the window is smaller than 1024px - So website.com/article.php?id=1111 redirects to website.com/m/article.php?id=1111
 
The most proper way to do it is to make the site responsive so you don't even need to have 2 separate versions for mobile/desktop. But failing that, yes what you're asking for should be straightforward. Just don't compare the whole URL – only compare the part at the first slash to see if it has /m/ or not
 
Ok, I've simplified the code to `if(window.innerWidth < 1024) {
window.location.replace("http://" + mob + window.location.pathname + window.location.search)
}` - This works but again it's adding another /m to the url
So now website.com/about.php, when shrunk down becomes website.com/m/m/about.php
 
3:04 PM
Because you need to check that they're not on the mobile site already, right? Otherwise they'll keep being redirected.

Try something like:

function isCurrentlyOnMobileSite() {
return window.location.pathname.indexOf('/m/') === 0;
}

if (window.innerWidth < 1024 && !isCurrentlyOnMobileSite()) {
// their screen is small, and they're not on mobile site. redirect to mobile site
} else if (window.innerWidth >= 1024 && isCurrentlyOnMobileSite()) {
// their screen is big, and they're currently on mobile site. redirect to desktop site
 
Sorry but now it does nothing at all when the screen is less than 1024, just stays on the full .com site
I don't really care about directing them to the full site if not on a mobile, I just need to make sure that mobile users are directed to the /m/ pages
 
You filled in the bits where I just wrote comments, right? You didn't just copy and paste straight from me without reading, understanding, and filling in the gaps did you?
 
No - It's continuously redirecting when on Mobile
 
paste your current code?
 
<script>

var reg = "website.com";
var mob = "website.com/m";

function isCurrentlyOnMobileSite() {
return window.location.pathname.indexOf('/m') === 0;
}

if (window.innerWidth < 1024 && !isCurrentlyOnMobileSite()) {
if(window.location.href != reg)
window.location.replace("http://" + reg + window.location.pathname + window.location.search)
} else if (window.innerWidth >= 1024 && isCurrentlyOnMobileSite()) {
if(window.location.href != mob)
window.location.replace("http://" + mob + window.location.pathname + window.location.search)
 
3:22 PM
You've got some of your old code there, it's clashing. Get rid of these lines:

if(window.location.href != reg)
if(window.location.href != mob)
Those are handled already by the isCurrentlyOnMobileSite() and !isCurrentlyOnMobileSite()
 
Now it says that reg is not defined?
 
Then you probably haven't defined reg.
 
Can you please just say what you mean so that I can fix this - I don't understand Javascript very well
I removed those lines but still constantly redirects
 
If it says reg is not defined, then you have missed out the bit where you define the reg variable. You may find it helpful to develop an understanding of the fundamentals of programming (and debugging) – there are a range of tutorials and books out there on Javascript for beginners. Once you have the basics, come back to StackOverflow and ask questions about specific gaps in your knowledge.
 
I just want this up and running today - IS THERE A WAY TO FIX IT???
 
3:35 PM
You're redirecting mobile users to the reg site, and reg users to the mob site: i.e. the wrong way round. Swap your two window.location.replace lines.
You're checking the pathname for anything starting with /m (you removed my other slash) - so if they go to website.com/mayonnaise it will say they are on the mobile site (when they're not).
If you want /m/ OR /m with nothing after it, then you'll probably want to use regular expressions.
 
Ok, that's all working fine now
 
If you want to make a web site using Javascript, learn Javascript. There are no (good) shortcuts (well - pay someone) - it is difficult to master and requires effort. We'll help you on StackOverflow when you get stuck - but you need to show a minimal understanding of the topic before than happens. It will probably take (much) more than an afternoon. Best of luck.
 

last day (14 days later) »