open a javascript window from code behind

  • Thread starter Thread starter Ganesh Ramamurthy
  • Start date Start date
G

Ganesh Ramamurthy

Hi Mark,

You can try this....

After your form tag in your aspx file type the folloowing

......
</form>
<script language="javascript">
<asp:literal runat="server" id="ltScript"></asp:literal>
</script>

In your code behind do the following


Literal ltScript = (Literal) Page.FindControl("ltScript");
ltScript.Text = "window.open('http://MyURL/MyPage.aspx','name','options');"

Regards
Ganesh
 
Hi Mark,
Check if you have any popup blocker installed in your browser. Also winxp
service pack 2 comes with a popup blocker.
HTH
srini
 
Hi all, how do I open a javascript window from a code behind page?
I was using

Response.Write("<script
language='javascript'>window.open('url','name','options');</script>")

but now for some reason it is not working..
Thanks in advance
Mark
 
Hi Ganesh, thanks for that info. I tried it but it is still not working. It
must be a problem with my spyware / browser setup stopping scripts as your
code looks fine.

Thanks again
Mark
--
 
Hi Mark,
i had the same problem as you and I found a little workaround...

just add a normal <asp:Button id=someButton runat=server></asp:Button>
in code behind

on the page load add this line

this.someButton.Attributes.Add("onClick",
"window.open('popupwindow.aspx','WindowName','toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=640,height=425');");


now the flow that happens is ..
1) the popup window is openened ...
2) the code behind for the first page is triggered

you can add <body onload="window.focus()"> to the body onload event of
your popup but the first page will be on top even with this code.

a workaround for this is to add this piece of javascript

<body onload="window.focus()" onblur="blurEvent()">

<script language="javascript">
var count = 0;

function blurEvent()
{
if(count == 0)
self.focus();
count = count + 1;
}
</script>


downside :
if the postback triggered stuff thats write to session or database ..
it will not be accessible on the first pageload of your popup page.
but you could adapt the blurEvent() function so it postsback the first
real load ... (count == 1)

Is it a nifty or nasty workaround ... i'll leave that up to you, but
it did help me out :)

Greets,
Tom
 
Back
Top