15

I have a javascript function that uses window.location. It works fine in Firefox and internet Explorer, but not in Chrome. I've tested this on both Ubunutu Hardy and Windows Vista. What is the underlying problem, and how can I circumvent it?

Bad Programmer
  • 893
  • 1
  • 13
  • 27

7 Answers7

44

The most common use of window.location is to make the browser load a new page. A common error is to assign the URL to the window.location object instead of it's property href. So, the correct way to do it is:

window.location.href = 'http://www.guffa.com';
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 4
    Thanks, Guffa. I was using window.location = 'url'. I updated to window.location.href and I'm still not getting redirected. Actually, I'm not getting any response from the function at all in Chrome. I created the following test function that works fine in IE and FF, but not Chrome: [code] function testfunc() { var code = document.getElementById("edit-report-types").value; alert(code); window.location.href = 'google.com'; } [/code] – Bad Programmer Mar 01 '10 at 22:10
  • @Someone: Instead of going to the page "google.com" in the current site, have you tried going to the site "google.com"? `window.location.href='http://google.com';` – Guffa Mar 01 '10 at 22:41
  • 1
    Yes. It still doesn't go there. It doesn't alert the code variable either, in Chrome. I took some code out in the previous comment, but I was declaring a variable right in the function (var code = "test!") and then alerting the variable. FF and IE shows the alert, even though Chrome doesn't. – Bad Programmer Mar 02 '10 at 01:13
  • What is your thought on the accepted answer in http://stackoverflow.com/questions/275092/windows-location-href-not-working-on-firefox3 ? – LCJ Dec 20 '12 at 12:40
  • 1
    @Lijo: Assigning a string to the `location` object is just a shortcut for assigning it to the `href` attribute. Assigning a string to a property that contains an object doesn't make much sense semantically, but it seems to be supported in most browsers nowadays. – Guffa Dec 20 '12 at 13:30
24

Try appending "return false;" to your javascript call like so...

window.location.href='google.com;
return false;
kleopatra
  • 51,061
  • 28
  • 99
  • 211
Baxtrum
  • 241
  • 2
  • 2
3

Try without window.. For example, use location.assign() instead of window.location.assign().

Peter O.
  • 32,158
  • 14
  • 82
  • 96
DnT
  • 61
  • 2
2

I was having this problem, and it ended up being that my javascript function was returning true after the window.location tag (due to nested functions). FF and IE never processed that far, while chrome did.

Derrick Bowen
  • 111
  • 1
  • 9
1

Just created the following html file and it alerted the window.location for me in Google Chrome 4.0 - are you using an old version?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
</head>
<body>

</body>
</html>
<script language="javascript" type="text/javascript">
alert(window.location);
</script>
amelvin
  • 8,919
  • 4
  • 38
  • 59
  • 1
    window.location='http://stackoverflow.com'; also works in Chrome 4.0 and redirects to this site. – amelvin Feb 27 '10 at 01:15
1

Resolved the issue. There wasn't a problem with the function or with Chrome. The function should be called by a drupal form element. I was adding the onclick event which called the function to the drupal form itself, instead of a particular form element.

Pretty much doing this:

$form['testform'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
        '#attributes' => array(
        'onchange' => 'testfunc()'),
 );

Instead of this:

$form['testform']['element1'] = array(
    '#type' => 'select',
        '#options' => options,
        '#required' => false,
        '#attributes' => array(
        'onchange' => 'testfunc()'),

);

Don't I feel silly.

Bad Programmer
  • 893
  • 1
  • 13
  • 27
  • 1
    Please accept your answer to your own question to save others coming along and trying to answer it (Click the tick on the left) – Basic Sep 21 '12 at 12:07
0

I had the same problem, and I wasn't careful enough to make sure that the new redirected url contained white spaces (shame on me).

So only Chrome stops this new location if the url is not standardized.

Make sure you have an UpdatePanel if you are using a Master Page .

Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129
corina
  • 1