0

Why is the behaviour of this code different in each browser? What should I change to accomplish the same behaviour in all of them? In Firefox it is fine, it uses the url /some/path/?searchedText=sometext, in IE it does nothing when I click on inputButton and in Chrome it fails because it encodes ? like this: /some/path/%3FsearchedText=sometext instead of the /some/path/?searchedText=sometext

html

<input id="inputText" type="text" class="form-control" placeholder="Searched text"> <input id="inputButton" type='button' value='Search' class="btn" onclick="myFunction()"/>

javascript

function myFunction() {
var text = document.getElementById('inputText').value;
var location = "some/path/?searchedText=";
window.location.pathname = location + text;
}
asked Dec 4, 2016 at 9:56
2
  • What version of IE? Commented Dec 4, 2016 at 10:09
  • It is the newest version 11. Commented Dec 4, 2016 at 10:12

2 Answers 2

1

You are using window.location.pathname which is great way to avoid explicity passing the starting (http://www.something.com) hostname but it also treats query string as path and therefore encodes them you can avoid that by manully constructing each url component and then use window.location.href

function myFunction() {
 var text = document.getElementById('inputText').value;
 var protocol = window.location.protocol;
 var hostname = window.location.hostname;
 var url = protocol + '//' + hostname + "/some/path?searchedText=" + text;
 window.location.href = url;
}
answered Dec 4, 2016 at 10:41
Sign up to request clarification or add additional context in comments.

Comments

0

You could use the property search.

window.location.search = '?searchedText=' + text;
answered Dec 4, 2016 at 10:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.