how to pass parameters to another aspx file with javascript?

Discussion in 'ASP .Net' started by Chris, Apr 17, 2009.

  1. Chris

    Chris Guest

    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.
     
    Chris, Apr 17, 2009
    #1
    1. Advertising

  2. Chris

    Chris Guest

    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

    "Mark Rae [MVP]" <> schreef in bericht
    news:%...
    > "Chris" <> wrote in message
    > news:...
    >
    >> I tried this, but the parameters are not passed to the other page.

    >
    > You're trying to use server-side syntax in JavaScript - that's never going
    > to work. Also, don't forget the semi-colons...
    >
    > <script type="text/javascript">
    > function fen(param1, 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=' + param1 + '&Item1='
    > + param2, "", x);
    > }
    > </script>
    >
    > ClientScript.RegisterStartupScript(GetType(), "redirect", "fen(" + param1
    > + ", " + param2 + ");");
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
     
    Chris, Apr 17, 2009
    #2
    1. Advertising

  3. Chris

    Chris Guest

    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



    "Mark Rae [MVP]" <> schreef in bericht
    news:...
    > "Chris" <> wrote in message
    > news:...
    >
    >> Does there 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?

    >
    > window.location = 'page2.aspx';
    > location.href = 'page2.aspx';
    >
    >
    >> If not, I did self.close() in order to close the first window, but with
    >> IE7, I get a message asking me whether i want to close it. Any way to
    >> avoid that message?

    >
    > Not in this case.
    >
    > It used to be possible to exploit one of the many security holes in IE6 by
    > using window.opener=null;window.close(); but this has been fixed in later
    > versions of IE, and never worked reliably in Mozilla-based browsers
    > anyway...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
     
    Chris, Apr 17, 2009
    #3
  4. Chris

    Tim Williams Guest

    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

    "Chris" <> wrote in message
    news:...
    > 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.
    >
     
    Tim Williams, Apr 18, 2009
    #4
  5. Chris

    Chris Guest

    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>



    "Mark Rae [MVP]" <> schreef in bericht
    news:u%...
    > "Chris" <> wrote in message
    > news:%...
    >
    >> Sorry if i come back here, but there is still a problem:
    >> 1) i get "type expected", so i did GetType(string)

    >
    > Please don't do that...
    > http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx
    >
    >
    >> (are there other types?)

    >
    > No.
    >
    >
    >> 2) the param are not passed. I can see in the address bar this:

    >
    > Sorry, I hadn't considered / realised that you were using VB.NET instead
    > of C#...
    >
    > In which case, it's probably something like:
    >
    > ClientScript.RegisterStartupScript(Me.GetType(), "redirect", "fen(" +
    > param1 + ", " + param2 + ");", True)
    >
    > You may also need to surround the params with single quotes in the
    > JavaScript...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
     
    Chris, Apr 18, 2009
    #5
  6. Chris

    Chris Guest

    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 ...

    "Mark Rae [MVP]" <> schreef in bericht
    news:...
    > "Chris" <> wrote in message
    > news:...
    >
    >> If I remove onload="fen()" in the <body> tag, I get a JavaScript error
    >> ('xxx' is not defined)

    >
    > ???? You don't need the onload line! The whole point of the ClientScript
    > method is that it injects the JavaScript into the page as it is built on
    > the server...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
     
    Chris, Apr 18, 2009
    #6
  7. Chris

    Chris Guest

    Thanks, it's ok now.

    "Mark Rae [MVP]" <> schreef in bericht
    news:...
    > "Chris" <> wrote in message
    > news:...
    >
    >> Sorry, I really don't see what's wrong ...

    >
    > 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>
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
     
    Chris, Apr 18, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vish
    Replies:
    2
    Views:
    1,752
    Steve C. Orr, MCSD
    Jul 16, 2003
  2. sridhar kumar ch
    Replies:
    1
    Views:
    8,655
  3. sridhar kumar ch
    Replies:
    0
    Views:
    812
    sridhar kumar ch
    Jun 28, 2006
  4. Replies:
    4
    Views:
    1,568
    Richard Maher
    Sep 14, 2007
  5. Jesse Houwing
    Replies:
    0
    Views:
    1,219
    Jesse Houwing
    Apr 20, 2009
Loading...

Share This Page