-3

I am designing a web page and want to display a code snippet if the website is viewed without www like http://example.com but not when viewed with http://www.example.com.

<script type="text/javascript">
if (window.location.href== "http://www.example.com") {
   window.location.href = 'http://example.com/'; 
}
</script>

Using this I can redirect users from www version to non www version. but it would be better if there is a way if the code which I want to add only display when address in the address bar is http://example.com and display something else when url is http://www.example.com. I think this could be done using similar javascript code. But using a document.write .

Avadhesh18
  • 27
  • 1
  • 9
  • 6
    Dont you want to do this on server instead? :) (.htaccess maybe) – Entity Black Jul 03 '14 at 14:29
  • Your visitors don't care about that sort of thing. Make the decision for them: http://stackoverflow.com/questions/234723/generic-htaccess-redirect-www-to-non-www – isherwood Jul 03 '14 at 14:29
  • @Windkiller No mate, I really want to do this with JS. – Avadhesh18 Jul 03 '14 at 14:46
  • @Avadhesh18 I'm gonna be honest: this seems like a terrible idea for a few reasons. 1) it causes an unnecessary redirect after the page has loaded. 2) it will surely confuse your users if content is different depending on which subdomain you use. 3) it will likely be a nightmare for SEO. 4) it's bad practice. 5) it's way simpler to use .htaccess and not have to worry about this silly JavaScript. – Jason Jul 03 '14 at 15:08

5 Answers5

1

Anyway the thing you are looking for is window.location.host.

This if you want to redirect (yeah but stupid)

<script type="text/javascript">
    if (window.location.host == "www.example.com") {
       window.location.href = window.location.protocol + '//example.com' + window.location.pahtname + window.location.hash ; 
    }
</script>

Good thing is, that it doesnt matter if user was on https://www.example.com/page/42#anchor856 or http://www.example.com/mywifehatesme , new address will be same. But bad is, this will create additional step in history (back button will redirect again = infinity loop), user will load page twice and if js is off, then it just doesn't work.

This is for hidding element

<script type="text/javascript">
    if (window.location.host == "www.example.com") {
        document.querySelector("anything").style.display = "none";
    }
</script>

EDIT: Prevent www users from loading:

When user without www comes, synchronous request is called. (users without js cant see it)

<script type="text/javascript">
    if (window.location.host == "example.com") {
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "example.com/specialData", false);
        xhr.responseType = "text";
        xhr.send();
        window.onload = function () {
            document.querySelector("div#wwwdoesntsee").innerHTML = xhr.responseText;
        };
    }
</script>
Entity Black
  • 3,401
  • 2
  • 23
  • 38
  • This is a good answer to my question but it will also first load the content and the hide it with javascript. I want something like one I posted in the edited question. BTW Thumbs up for http://www.example.com/mywifehatesme – Avadhesh18 Jul 03 '14 at 14:48
  • Ok I did something like that. Ofc instead of xhr you can use simple document.write or innerHTML. Since you didn't specify what exactly is in the part you want to hide, I made global solution... – Entity Black Jul 03 '14 at 15:02
0

You could have:

<script type="text/javascript">
window.onload = function() {
    if (window.location.href === "http://www.example.com") {
        document.getElementById("myId").style.display = "none";
        // Or, to prevent any further async loading...
        var toRemove = document.getElementById9"myId");
        toRemove.parentNode.removeChild(toRemove);
        // To populate dynamically, change below to style="display:none;" and...
        document.getElementById("myId").style.display = "block";
        document.getElementById("myId").innerHTML = "\
        .... content goes here .... \
        ";
    }
}
</script>

... and then later:

<div id=myId>
... Content I want to hide ...
</div>

(if using the third option above, instead have an empty div):

<div id=myId style="display:none;"></div>
blgt
  • 8,135
  • 1
  • 25
  • 28
  • This will just hide the content but will not stop it from loading. – Avadhesh18 Jul 03 '14 at 14:38
  • @Avadhesh18 if you dont want to load it, then revert the approach. If user reaches example.com without www, then use ajax to load additional content. – Entity Black Jul 03 '14 at 14:44
  • @Avadhesh18 You can prevent any async loading once your window opens up by removing the node (see above edit). If you want to prevent it from loading *at all* you have to either populate it dynamically, as Windkiller suggests, or do it in your server-side code – blgt Jul 03 '14 at 14:54
  • That would be great @Windkiller but in the edited code can't I just change switch (window.location.protocol) to switch (window.location.href) and file and http to URL's – Avadhesh18 Jul 03 '14 at 14:55
0

This sounds like an issue you should handle on the server. Redirect to "http://example.com" if user calls "http://www.example.com"

If you want different content on the different subdomains, you should probably set routing options at your domain provider to different files or directories.

0

I seriously wouldn't consider doing it that way.

This would not be classed as a 301 redirect and would be bad for your seo. if your using apache try this for the .htaccess file.

0

Thanks to all of you guys. But now I have found the exact solution based on the question I asked. The Features Are : This will not confuse visitors as I know what I want users to see, and this will also not require users to redirect from non www to www.

<script>switch (window.location.host) 
{
   case "example.com":
      document.write("You are a good visitor<BR>\n")
      break 
   case "www.example.com":
      document.write("You are a bad visitor<BR>\n")
      break
   default:
      document.write("You are ALIEN<BR>\n")
      break
}</script>
Avadhesh18
  • 27
  • 1
  • 9