Calling a Javascript function in a foreach loop

Discussion in 'ASP .Net' started by =?Utf-8?B?TWljcm9Nb3Ro?=, Jul 6, 2006.

  1. Hi,

    I'm trying to call a Javascript function within a foreach loop. I am loop
    over a series of users and I want to call the JS function which opens a new
    window, passing in the user id to each call for the JS function.
    So far I've done this:

    foreach (USER _user in users)
    {
    string jscriptString = "<script language='JavaScript'>";
    jscriptString += "pdfWinOpen(" + _user.userID + ");";
    jscriptString += "</script>";

    Response.Write(jscriptString);
    }

    But I can't seem to get the javascript to fire. File which opens in the new
    window has a window.close(); in body onload method. So I easily close the
    opened window.

    How can I fire off the open window JS event?

    Thanks

    Stephen
     
    =?Utf-8?B?TWljcm9Nb3Ro?=, Jul 6, 2006
    #1
    1. Advertising

  2. =?Utf-8?B?TWljcm9Nb3Ro?=

    Hans Kesting Guest

    > Hi,
    >
    > I'm trying to call a Javascript function within a foreach loop. I am loop
    > over a series of users and I want to call the JS function which opens a new
    > window, passing in the user id to each call for the JS function.
    > So far I've done this:
    >
    > foreach (USER _user in users)
    > {
    > string jscriptString = "<script language='JavaScript'>";
    > jscriptString += "pdfWinOpen(" + _user.userID + ");";
    > jscriptString += "</script>";
    >
    > Response.Write(jscriptString);
    > }


    Why a Response.Write? Careful: it doesn't mix well with the regular
    asp.net way of building a page. See Page.RegisterStartupScript for a
    better way. The Response.Write output will end up *before* the regular
    output, where it *might* (not sure) be ignored by the browser.

    >
    > But I can't seem to get the javascript to fire. File which opens in the new
    > window has a window.close(); in body onload method. So I easily close the
    > opened window.


    So if I understand correctly, that new window closes itself
    immediately?

    >
    > How can I fire off the open window JS event?
    >
    > Thanks
    >
    > Stephen


    What do you see in the html source of the page that should contain this
    javascript? Does the javascript you expect exist? Does it look OK?
    Any script warnings?

    Hans Kesting
     
    Hans Kesting, Jul 6, 2006
    #2
    1. Advertising

  3. Hi,

    Thanks for the help, after taking about this here, we've decided that we
    might have to create a small WIndows Form app which open a mini browser
    calling my generate PDF page for each user.

    Stephen

    "Hans Kesting" wrote:

    > > Hi,
    > >
    > > I'm trying to call a Javascript function within a foreach loop. I am loop
    > > over a series of users and I want to call the JS function which opens a new
    > > window, passing in the user id to each call for the JS function.
    > > So far I've done this:
    > >
    > > foreach (USER _user in users)
    > > {
    > > string jscriptString = "<script language='JavaScript'>";
    > > jscriptString += "pdfWinOpen(" + _user.userID + ");";
    > > jscriptString += "</script>";
    > >
    > > Response.Write(jscriptString);
    > > }

    >
    > Why a Response.Write? Careful: it doesn't mix well with the regular
    > asp.net way of building a page. See Page.RegisterStartupScript for a
    > better way. The Response.Write output will end up *before* the regular
    > output, where it *might* (not sure) be ignored by the browser.
    >
    > >
    > > But I can't seem to get the javascript to fire. File which opens in the new
    > > window has a window.close(); in body onload method. So I easily close the
    > > opened window.

    >
    > So if I understand correctly, that new window closes itself
    > immediately?
    >
    > >
    > > How can I fire off the open window JS event?
    > >
    > > Thanks
    > >
    > > Stephen

    >
    > What do you see in the html source of the page that should contain this
    > javascript? Does the javascript you expect exist? Does it look OK?
    > Any script warnings?
    >
    > Hans Kesting
    >
    >
    >
     
    =?Utf-8?B?TWljcm9Nb3Ro?=, Jul 6, 2006
    #3
  4. Hans Kesting wrote:
    > Why a Response.Write? Careful: it doesn't mix well with the regular
    > asp.net way of building a page. See Page.RegisterStartupScript for a
    > better way. The Response.Write output will end up *before* the regular
    > output, where it *might* (not sure) be ignored by the browser.


    Historically, Javascript has been used anywhere in the pages, so
    browsers are quite patient with less nicely formed pages. It will
    probably run the script wherever it is.

    However, the script uses the function "pdfWinOpen". If that function is
    not written to the page *before* the calls are written, it will not
    exist when the calls are executed.
     
    =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=, Jul 6, 2006
    #4
    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. Kevin Spencer

    Re: foreach loop error

    Kevin Spencer, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    373
    Kevin Spencer
    Aug 22, 2003
  2. Luc Kumps

    Re: foreach loop error

    Luc Kumps, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    394
    Luc Kumps
    Aug 22, 2003
  3. =?Utf-8?B?SnVzdGlu?=

    Problem with foreach loop

    =?Utf-8?B?SnVzdGlu?=, Oct 22, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    338
    Hans Kesting
    Oct 22, 2004
  4. Islam Elkhayat

    loop througth Literials using foreach!!

    Islam Elkhayat, Feb 10, 2005, in forum: ASP .Net
    Replies:
    16
    Views:
    778
    Scott Allen
    Feb 12, 2005
  5. Isaac Won
    Replies:
    9
    Views:
    457
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page