homepage Welcome to WebmasterWorld Guest from 50.17.27.205
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
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
Quang




msg:3285120
 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.

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>

 

Moby_Dim




msg:3285310
 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.

penders




msg:3285535
 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.

Quang




msg:3288006
 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.

vincevincevince




msg:3288010
 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..

Quang




msg:3288013
 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?

daveVk




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

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.

Quang




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

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.

penders




msg:3292013
 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.

Quang




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

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

cmarshall




msg:3295622
 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:

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

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