Hani Hani - 1 month ago 14
Javascript Question

why window.location appending instead of replacing the URL in ie

I am getting wrong URLs in ie but not in firefox and chrome.

Basically, I have a textfield called text-search. I am using jQuery and rewriterule in htaccess to internally redirect pages. I am on localhost and all files are in a folder called test.

In firefox and chrome, if you enter 'hello' hit enter, 'hi' hit enter and 'goodbye' hit enter in the text-search box you get the correct URLs as

localhost/test/testing/hello

and

localhost/test/testing/hi

and

localhost/test/testing/goodbye

repectively.

In ie you get

localhost/test/testing/hello

and

localhost/test/testing/testing/hi

and

localhost/test/testing/testing/testing/goodbye

respectively

The problem here is that 'testing' is prepending. How to stop this from happening in ie. I could not find an answer to this problem on the web.

html and jquery code

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>


<base href="http://localhost/test/" />
<script src="jQuery.js"></script>
<script>
$(document).ready(function(){
$("#text-search").keyup(function(e){
if (e.keyCode == 13){
window.location.replace("testing/"+$('#text-search').val());
}
})
})
</script>
</head>

<body>
<input type='text' id='text-search'>
</body>
</html>


.htaccess

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^testing/(.+)$ /test/testing.php?string=$1 [L]


Can you please help me on this. Many thanks

Answer

window.location isn't a string, I'd be very careful with using it that way - it's actually a Location object.

Maybe this will help you:

var href = window.location.href;
window.location = href.replace(/testing\/.*$/, "testing/"+$('#text-search').val());

You can also do:

var href = "" + window.location;

to force the string cast, and pass-by-value.