homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

location.href not working in IE
location.href IE bug

 1:13 am on Mar 18, 2007 (gmt 0)
I'm having trouble using location.href as part of a javascript function in IE (I'm using XP and IE6). I can get it to work in Firefox (both for PC and Mac).

I've searched both this site and others on the internet, and the closest thing that I've found was this bug from MS, but I'm not using anchor tags.


Any help would be appreciated.

Here is a sample code to show the error (the actual code is different, but this code also shows the same error).



<script type="text/javascript">

function sendtopage(){

var Page = "new_page"

//comment out alert. Does not work prior to location.href
//alert("You are being forwarded");
document.location.href=(Page + ".htm");
alert("You are being forwarded");
} // end of sendtopage()

function check(){
var hash= document.login.password.value;
var hash_sha1 = "a";

if (hash == "") {
alert ("Nothing entered");
else if ( hash == hash_sha1) {
else {
alert ("Incorrect. Please type the letter a");
} // end of check()





<form name='login'>
<h4>Enter "a" to be sent to a new page<br>
<input TYPE='password' NAME='password' SIZE=15 MAXLENGTH=15><br>
<input TYPE='submit' VALUE='Login' ONCLICK='check()'>
<input TYPE='reset' value='Clear'>





 7:34 am on Mar 18, 2007 (gmt 0)

Try to play with setting form action value to desired url instead of location may be.


 4:16 pm on Mar 18, 2007 (gmt 0)

document.location.href=(Page + ".htm");

You need to use window.location.href or simply, location.href to change the page in the window, not document.location.

document.location (now deprecated in favour of document.url) is not the same as window.location.

document.location is supposed to be read only. However, Netscape (and it appears FF) allow this property value to be changed.


 4:08 am on Mar 21, 2007 (gmt 0)

Sorry I haven't been able to check back earlier.

I've tried using both the window.location.href and the plain location.href, but neither will work. Any other suggestions?

P.S. You can cut and paste the above code and use it to create an HTML page and test it out. All you need is another HTML page name "new_page.htm" to see if it works.


 4:17 am on Mar 21, 2007 (gmt 0)

First - are you running it from a local file? If so then the script may be blocked for security reasons. Upload it and test it.

Second - try using top.location='' etc..


 4:32 am on Mar 21, 2007 (gmt 0)

I am testing on a local file, but the scripts are not being blocked (the alert will show up in IE, but the page redirection will not). Also, I do have a page running on a web server, and I still have the same issue.

The top.location.href still does not solve the issue. Any other suggestions?


 4:45 am on Mar 21, 2007 (gmt 0)

Try changing

<input TYPE='submit' VALUE='Login' ONCLICK='check()'>


<input TYPE='button' VALUE='Login' ONCLICK='check()'>

No actual submit seems to be required and side effects may be an issue.


 5:15 am on Mar 21, 2007 (gmt 0)

Thanks, that solved it. I changed the

<input TYPE='submit' VALUE='Login' ONCLICK='check()'>


<input TYPE='button' VALUE='Login' ONCLICK='check()'>

Now, everything works - I can even have the alert prior to the page redirect, instead of after (which I couldn't before without an error).

Thanks for your help.


 7:15 pm on Mar 24, 2007 (gmt 0)

Just a thought... an alternative would be to be to keep your button as a 'submit' button and use the onsubmit event of the form element instead, but always return false (preventing the form from actually being submitted):

<form name="login" onsubmit="check(); return false;">
<input type="submit" value="Login">

Either way, preventing the form from submitting seems to be the key (as daveVk suggested) in getting it to work in IE.


 3:18 pm on Mar 28, 2007 (gmt 0)


Thanks for for the suggestion, it works great. I like the fact that I can still keep the type='submit', since the form now lets users press the 'enter' button to login (instead of clearing out the password when using the type='button')


 6:29 pm on Mar 28, 2007 (gmt 0)

Just FYI. I have used the
window.location.href=[i]URI[/i] before, but I believe the proper way to do it is to use the accessor [w3schools.com].

To wit:


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved