how to pass parameters to another aspx file with javascript?

C

Chris

Hi,

i want to redirect to another page like here below in asp.net, but in
javascript and without menubar, toolbar etc ... :

Response.Redirect(String.Format("/myserver/myapp.aspx?Item0={0}&Item1={1}",
param1, param2))


i tried this, but the parameters are not passed to the other page.

<script type="text/javascript">
function fen() {
var hd1 = param1
var hd2 = param2
x = "left=200,top=200,width=600,height=400,title=no,toolbar=no,
menubar=no,location=no,directories=no, scrollbars=yes"
a = window.open("/myserver/myapp.aspx?Item0={0}&Item1={1}", hd1,
hd2, "", x)
}
</script>


Thanks for help.
Chris.
 
C

Chris

Thanks.

Does it maybe exist a way in javascript to redirect like in vb.net but of
course with the toolbar properties etc .. instead of opening a second
window?

If not, i did self.close() in order to close the first window, but with IE
7, i get a message asking me whether i want to close it. Any way to avoid
that message?
Thanks again.
Chris
 
C

Chris

Sorry if i come back here, but there is still a problem:
1) i get "type expected", so i did GetType(string) (are there other types?)
2) the param are not passed. I can see in the address bar this:
http://laptop/myserver/myapp.aspx?Item0=undefined&Item1=undefined

I give the whole code:
Sub page_load()
....
param1="xxx"
param2="zzz"
ClientScript.RegisterStartupScript(GetType(String), "redirect", "fen(" +
param1 + ", " + param2 + ");")
End Sub

<script type="text/javascript">
function fen(param1,param2) {

x = "toolbar=no, menubar=no,location=no,directories=no,
scrollbars=yes"
a = window.open("/myserver/myapp.aspx?Item0=" + param1 + "&Item1=" +
param2, "", x)
self.close()
}
</script>

Thanks again
Chris
 
T

Tim Williams

There's no string formatting like that in js....

a = window.open("/myserver/myapp.aspx?Item0="+hd1+"&Item1="+hd2, ...

You may need to encode the parameter values depending of what type of data
you're passing.

Tim
 
C

Chris

Hi Mark, you never sleep?

i still have a problem:

with this code below, i get
http://myserver/myapp.aspx?Item0=undefined&Item1=undefined

If i remove onload="fen()" in the <body> tag, i get a javascript error
('xxx' is not defined) and of course the second window will never be opened.


<%@ Page Language="VB" %>
<%@ Import Namespace="System.IO" %>

<script runat="server">
Sub page_load()
param1="xxx"
param2="zzz"
ClientScript.RegisterStartupScript(Me.GetType(), "redirect", "fen(" + param1
+ ", " + param1 + ");", True)
End Sub
</script>

<html><head><title>test</title></head>
<body onload="fen()">
</body>
<script type="text/javascript">
function fen(param1,param2) {

x = "toolbar=no, menubar=no,location=no,directories=no, scrollbars=yes"
a = window.open("/myserver/myapp.aspx?Item0=" + param1 + "&Item1=" + param2,
"", x)
}
</script>
</html>
 
C

Chris

Ok, i removed onload="fen()" in the <body> tag.

I tried this:
ClientScript.RegisterStartupScript(Me.GetType(), "redirect", "fen(" + param1
+ ", " + param2 + ");", True)
this gives a javascript error: 'xxx' is not defined. This means that the
parameters are not passed from the server to javascript, no?

then this:
ClientScript.RegisterStartupScript(Me.GetType(), "redirect", "fen(" + param1
+ ", " + param2 + ");", False)
no javascript error; but the target window is not opened

then this:
ClientScript.RegisterStartupScript(Me.GetType(), "redirect", "fen(" + param1
+ ", " + param2 + ");")
same: no javascript error; but the target window is not opened

Sorry, I really don't see what' wrong ...
 
C

Chris

Thanks, it's ok now.

Mark Rae said:
1) The param1 and param2 variables weren't being instantiated...

2) There was no server-side form tag...

3) The string variables being passed to the JavaScript function didn't
have single quotes around them...


<%@ Page Language="VB" %>
<%@ Import Namespace="System.IO" %>

<script runat="server">
Sub Page_Load()
Dim param1 As String = "xxx"
Dim param2 As String = "zzz"
ClientScript.RegisterStartupScript(Me.GetType(), "redirect",
"fen('" & param1 & "', '" & param2 & "');", True)
End Sub
</script>

<html>
<head>
<title>test</title>
<script type="text/javascript">
function fen(param1,param2)
{
x = "toolbar=no, menubar=no,location=no,directories=no,
scrollbars=yes"
a = window.open("/myserver/myapp.aspx?Item0=" + param1 + "&Item1="
+ param2, "", x)
}
</script>
</head>
<body>
<form ID="form1" runat="server" />
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top