Saturday, March 24, 2012

open a javascript window from code behind

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

"Mark" <mark@.Z-Zvolution.nZt> wrote in message
news:clsfn2$g31$1@.lust.ihug.co.nz...
> 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
--

"Ganesh Ramamurthy" <ganesh@.kottsoftware.com> wrote in message
news:uy4QF7WvEHA.4076@.TK2MSFTNGP14.phx.gbl...
> 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
> "Mark" <mark@.Z-Zvolution.nZt> wrote in message
> news:clsfn2$g31$1@.lust.ihug.co.nz...
> > 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 Mark,
Check if you have any popup blocker installed in your browser. Also winxp
service pack 2 comes with a popup blocker.
HTH
srini

"Mark" wrote:

> 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
>
>
are you able to do the same using plain Javascript??

"Mark" <mark@.Z-Zvolution.nZt> wrote in message
news:clsh2u$gt0$1@.lust.ihug.co.nz...
> 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
> --
> "Ganesh Ramamurthy" <ganesh@.kottsoftware.com> wrote in message
> news:uy4QF7WvEHA.4076@.TK2MSFTNGP14.phx.gbl...
> > 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
> > "Mark" <mark@.Z-Zvolution.nZt> wrote in message
> > news:clsfn2$g31$1@.lust.ihug.co.nz...
> > > 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 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','toolb ar=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

0 comments:

Post a Comment