Javascript window.location 不适用于 Chrome 浏览器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2345807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
window.location Does Not Work on Chrome Browser
提问by Bad Programmer
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?
我有一个使用 window.location 的 javascript 函数。它在 Firefox 和 Internet Explorer 中运行良好,但在 Chrome 中运行不正常。我已经在 Ubunutu Hardy 和 Windows Vista 上对此进行了测试。根本问题是什么,我该如何规避?
回答by Guffa
The most common use of window.locationis to make the browser load a new page. A common error is to assign the URL to the window.locationobject instead of it's property href. So, the correct way to do it is:
最常见的用途window.location是让浏览器加载一个新页面。一个常见的错误是将 URL 分配给window.location对象而不是它的属性href。所以,正确的做法是:
window.location.href = 'http://www.guffa.com';
回答by Baxtrum
Try appending "return false;" to your javascript call like so...
尝试附加“return false;” 像这样对您的 javascript 调用...
window.location.href='google.com;
return false;
回答by DnT
Try without window.. For example, use location.assign()instead of window.location.assign().
尝试没有window.。例如,使用location.assign()代替window.location.assign()。
回答by Derrick Bowen
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.
我遇到了这个问题,结果是我的 javascript 函数在 window.location 标记之后返回 true(由于嵌套函数)。FF 和 IE 从来没有处理过那么远,而 chrome 却做到了。
回答by amelvin
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?
刚刚创建了以下 html 文件,它在 Google Chrome 4.0 中为我提醒 window.location - 您使用的是旧版本吗?
<!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>
回答by Bad Programmer
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.
解决了问题。该功能或 Chrome 没有问题。该函数应由 drupal 表单元素调用。我正在将调用函数的 onclick 事件添加到 drupal 表单本身,而不是特定的表单元素。
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.
我是不是觉得很傻。
回答by corina
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).
我遇到了同样的问题,我没有足够小心以确保新的重定向 url 包含空格(我很丢脸)。
So only Chrome stops this new location if the url is not standardized.
因此,如果 url 未标准化,则只有 Chrome 会停止这个新位置。
Make sure you have an UpdatePanel if you are using a Master Page .
如果您使用的是母版页,请确保您有一个 UpdatePanel。

