Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirection based on URL - JavaScript

If someone types in 'www.morgancc.edu', I want it to redirect to our mobile site located at 'www.morgancc.edu/m' However, I only need to redirect with this exact URL. I don't want it to redirect if you go to something like 'www.morgancc.edu/programs', which is what it is currently doing. Here is the code I have so far:

<script type="text/javascript">
if (window.location = "www.morgancc.edu") {
   window.location.href = 'http://www.morgancc.edu/m/'; 
}
</script>
like image 971
Terri Eades Avatar asked Aug 02 '13 17:08

Terri Eades


1 Answers

location.hostname with an empty path is what you seem to want

if (window.location.hostname == "www.morgancc.edu" && 
    window.location.pathname=="" ) {
   window.location.href = 'http://www.morgancc.edu/m/'; 
}

Alternatively look at the href

if (window.location.href== "http://www.morgancc.edu") {
   window.location.href = 'http://www.morgancc.edu/m/'; 
}

You may need to add some / here or there

like image 78
mplungjan Avatar answered Nov 05 '22 15:11

mplungjan