Welcome to WebmasterWorld Guest from 54.145.209.34

Forum Moderators: open

location.href not working in IE

location.href IE bug

   
1:13 am on Mar 18, 2007 (gmt 0)

5+ Year Member


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.

http://support.microsoft.com/kb/190244/en-us

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).

<html>

<head>

<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) {
sendtopage();
}
else {
alert ("Incorrect. Please type the letter a");
}
} // end of check()

//sendtopage();

</script>

</head>

<body>

<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'>
</form>

</body>

</html>

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

10+ Year Member



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)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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)

5+ Year Member



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)

WebmasterWorld Senior Member vincevincevince is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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)

5+ Year Member



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)

WebmasterWorld Senior Member 5+ Year Member



Try changing

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

TO

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

5+ Year Member



Thanks, that solved it. I changed the


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

TO


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

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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)

5+ Year Member



Penders,

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)

WebmasterWorld Senior Member 10+ Year Member



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:

window.location.href.[red][b]assign([/b][/red][i]URI[/i][red][b])[/b][/red]
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month