Hello! This is the most annoying thing to me, I need redirections with Javascript often but window.location
Doesn't work in chrome? I could swear it used to because I have just noticed this problem. Here is what I do
window.location = "http://mywebsite.com";
Works in Firefox, but not chrome? (I don't even develop for IE I just redirect because it's rage inducing, I suggest everyone do this). Please help, thanks!

Recommended Answers

All 9 Replies

Not to be a jerk, but not developing for IE is like asking a majority of PC users to simply ignore your website...

As to your problem, you can use window.location. href = "http://mywebsite.com";

If you do not put in a protocol, it should append a relative path to the domain.

No it's not working, iv'e searched on google and gone through a list of different methods that work in almost every other browser except chrome. I despise Internet Explorer, if people that use it wont come to my site I'm fine with that I would rather that actually, and IE is at it's lowest point of use atm, people are starting to notice real browsers.

There must be something else going on here.

I just tested the following code on Chrome 23, Firefox 16, Opera 12.1 and IE9 and it works as expected. Feel free to post your code.

<!DOCTYPE html>
<html>
  <head>
  </head>
<body>
  <script>
     window.location = "http://www.google.com/";
  </script>
</body>
</html>
<select>
<option onClick="window.location = 'http://google.com';">First to last</option>
</select>

And I have also tried making it a function and calling it, and no such luck.

I'm not sure what the problem might be, but window.location.href = "http://www.friendster.com" is absolutely working fine across browsers.

The onclick event is not going to work for that scenario. try something like this:

<!DOCTYPE html>
<html>
<head>

</head>

<body>
<select id="dropdown" size="1">
  <option value="none" selected="selected">-- Please Select --</option>
  <option value="http://www.google.com">Google</option>
</select>

<script>
var menu=document.getElementById("dropdown");
menu.onchange=function() {
  var chosenoption=this.options[this.selectedIndex];
  if (chosenoption.value!="none") {
    window.open(chosenoption.value, "", "");
  }
}
</script>

</body>
</html>

The OnClick event does work in the scenario because it works in firefox, and I have tested it with an alert.

firefox's event bubble is "backwards" meaning it goes from top down. In theory, firefox is the "broken" one in your scenario.

Every other browser works via bubbling upwards, and the onclick event will fire on the root element, and go up the body to the document/window.

Your argument about how not wanting to work with IE and other "broken" browsers betrays your limited experience with javascript. All browers have their quirks. IE has more, for sure, but they are getting fewer. However, firefox is just as bad at implementing their own interpretation of ECMA as everyone else, and they break stuff that "should work right."

Start looking into developing for cross browser, and issues like this will start to happen less and less. Sure, it's more work for you now and you will have a TON to catch up on. In the long run, though, maintainability, code re-use, and managing your site will become so much easier.

If you wish to insist that firefox is the end all be all of browsers, then be sure to start phrasing your questions as "I want to make my code work in firefox" and stop worrying about Chrome or Safari or any other webkit, IE, or Konquerer based browsers, as they will ultimately all be different and have something different about them.

The solution by Jorge is the best anyone has given to make your issue non-existent across all browsers. You are asking for the value of a select after it has been changed. That is a change event (onchange). However, it may fail in IE since IE requires a blur() event, in some cases, for a change to be considered "complete" and the event fired.

Good luck!

Ryan

Thanks, everyone and I don't claim to be experienced at all in Javascript.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.