Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript : sending custom parameters with window.open() but its not working

<html>
<head>
<script>
function open_win()
{
    window.open("http://localhost:8080/login","mywindow")
}
</script>
</head>
<body>

<input type="button" value="Open Window" onclick="open_win()">

</body>
</html>

Hi ,

On click of a button , i am opening a new website (My web site ) I have two text fields ( One Text Field and another Password Field) , i am trying to send this values to the other opened window .

But its not working as I want.

I have tried the following ways

1.  window.open("http://localhost:8080/login?cid='username'&pwd='password'","mywindow")

2.  window.open("http://localhost:8080/login","mywindow")
    mywindow.getElementById('cid').value='MyUsername'
    mywindow.getElementById('pwd').value='mypassword'

Could anybody please help me if this is possible or not ??

Sorry for the incomplete details , its a Post request .

like image 920
Pawan Avatar asked Nov 06 '12 12:11

Pawan


People also ask

How does JavaScript window open work?

open() method open a blank window. name: It is an optional parameter and is used to set the window name. specs: It is an optional parameter used to separate the item using a comma. replace: It is an optional parameter and used to specify the URL URL creates a new entry or replaces the current entry in the history list.

What is the second parameter of window open () method?

The URL of the page to open. Optional. The target attribute or the name of the window.

How do you check if a window is already open in JavaScript?

The closed property of the window object. The closed property tells you whether a window opened using window. open() is still open or not. You see, once a window is opened (using JavaScript), it's closed property is immediately initialized, with a value of false.


1 Answers

To concatenate strings, use the + operator.

To insert data into a URI, encode it for URIs.

Bad:

var url = "http://localhost:8080/login?cid='username'&pwd='password'"

Good:

var url_safe_username = encodeURIComponent(username);
var url_safe_password = encodeURIComponent(password);
var url = "http://localhost:8080/login?cid=" + url_safe_username + "&pwd=" + url_safe_password;

The server will have to process the query string to make use of the data. You can't assign to arbitrary form fields.

… but don't trigger new windows or pass credentials in the URI (where they are exposed to over the shoulder attacks and may be logged).

like image 179
Quentin Avatar answered Sep 23 '22 13:09

Quentin