window.location problem

北战南征 提交于 2019-12-08 06:36:11

问题


I am facing on strange problem in ie6.

When i am using window.location to redirect page through javascript it works fine in all browser except ie6.

It works in ie 6 if i place just like below:

<a href="javascript:void(0);" onclick="javascript:window.location('http://www.demo.com');">demo</a>

but its not working for below code.

<a href="javascript:void(0);" onclick="javascript:redirect();>demo</a>
function redirect()
{
  window.location('http://www.demo.com');"
}

can you please figure out that whats problem here.

Thanks.

Avinash


回答1:


The javascript: protocol is only used if you have Javascript code in an URL. If you put it in an event handler it becomes a label instead.

The location member is not a function, it's an object. Set the href property to change the location.

You have an extra quotation mark after the code line in the function, which is probably causing a syntax error.

<a href="javascript:void(0);" onclick="redirect();>demo</a>

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



回答2:


How about doing this:

<a href="#" onclick="redirect(); return false;">
  demo
</a>



回答3:


If you want the page to redirect to demo.html when the user clicks a link, dare I suggest you use the universal, crossbrowser <a href="demo.html">demo</a>?




回答4:


Try:

window.location.href = 'http://www.demo.com';

in the function.




回答5:


Try:

window.event.returnValue = false; document.location.href='http://www.demo.com';



来源:https://stackoverflow.com/questions/1939332/window-location-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!