Calling a Javascript function in a foreach loop

G

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);
}

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
 
H

Hans Kesting

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
 
G

Guest

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
 
G

Guest

Hans said:
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.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top