1

An onclick function is supposed to fire up a new window with a string and the textbox value appended to the url, but it doesn't seem to work. (The string will populate another textbox in the new window. This however, functions properly.)

When I use window.location it works but I don't want the page to open in the same window.

I'm very new to Javascript, thank you for your help.

Javascript

function changeLocation(){
var textboxpw = document.getElementById("key");
window.open{'http://website.com?secret="+textboxpw.value), '_blank';}

HTML

<input type="text" name="key" id="key" />
<input type="submit" value="Go" onclick="changeLocation(); return false;" />
helenanananah
  • 11
  • 1
  • 2

2 Answers2

5
function changeLocation(){
    var textboxpw = document.getElementById("key");
    window.open('http://website.com?secret='+encodeURIComponent(textboxpw.value), '_blank');
}
v7d8dpo4
  • 1,399
  • 8
  • 9
1

The encodeURIComponent() function encodes a URI component.

This function encodes special characters.

In addition, it encodes the following characters:

, / ? : @ & = + $ #

Example:

var uri = "http://w3schools.com/my test.asp?name=ståle&car=saab";
var res = encodeURIComponent(uri);

output:

http%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab

The open() method opens a new browser window.

syntax: window.open(URL,name,specs,replace)

If your code block works fine except new window then try:

function changeLocation(){
    textboxpw = document.getElementById("key");
    url = encodeURIComponent('http://website.com?secret='+textboxpw.value);
    window.open(url, '_blank');
}
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42