views:

163

answers:

4

Hi,

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');"&gt;demo&lt;/a&gt;

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  A: 

How about doing this:

<a href="#" onclick="redirect(); return false;">
  demo
</a>
marcgg
-1 window.location isn't a function.
Aaron Digulla
@aaron, I'm not sure what you mean, could you elaborate on that?
marcgg
@Aaron: Where did he say that window.location was a function?
Andy E
A: 

Try:

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

in the function.

Lucas Jones
+6  A: 

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>
Guffa
@Guffa: Setting window.location will still work. Try it: `window.location = "http://stackoverflow.com";`. Good spot on the syntax error though, I must admit I overlooked it :)
Andy E
@Andy: Assigning the url to the location object only works in some browsers.
Guffa
@Guffa: Ah, fair enough. I would be interested to know which (if any) of the top browsers it wouldn't work in, though.
Andy E
+1  A: 

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>?

Phil H
@Phil: You don't think it's likely that the code he wrote in the question is just a simplified example of what he's trying to do?
Andy E
@Andy E: Yes, but he hasn't shown us that. If he wants a js function to change the window location, that would be a different question - the specific example was a redirect in a hyperlink. A less simplified question might help - making javascript run when the user clicks a link is not the same problem.
Phil H