window.location Does Not Work on Chrome Browser

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?

Answers


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';

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

window.location.href='google.com;
return false;

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


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>

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.


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.


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 .


Need Your Help

Writing a directive for an existing javascript object the “Angular” way

angularjs angular-directive

I'm working on trying to make an Angular directive that encapsulates the viewer.js functionality for the pdf.js project because I really like how their viewer looks, but I would like to be able to ...

Rotate left/right with number of moves using GCC assembly

c++ visual-studio gcc assembly x86

I have the following code to calculate rotate left/right in Visual Studio assembly.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.