RegisterStartupScript and Firefox

Discussion in 'ASP .Net' started by Dune88, Feb 13, 2008.

  1. Dune88

    Dune88 Guest

    I've got some code that uses Page.ClientScript.RegisterStartupScript to call
    a javascript function from the Page_Load method in the code behind.

    The code works fine in IE but the javascript function is not called at all
    in Firefox. I stripped my code back to the basics and all I have now is a
    webform with no controls on it and the following in the code behind:

    protected void Page_Load(object sender, EventArgs e)
    {
    Page.ClientScript.RegisterStartupScript(typeof(Page),
    "TestScript", "<script type='text/jscript'>alert('test script');</script>");
    }

    This very basic webform works in IE but not in Firefox.

    I'm running .NET Framework 3.5, IE 7 and Firefox 2.0.0.11. I added
    "onload="alert('onLoad')"" to the body tag to ensure that javascript was
    enabled and popups were not being blocked in Firefox. The onLoad alert came
    up fine but the alert added by the RegisterStartupScript did not come up.

    Can anyone shed any light on this?

    Cheers
    Dune88, Feb 13, 2008
    #1
    1. Advertising

  2. I haven't done much with RegisterStartupScript but it might be helpful to
    see what the resulting HTML is, especially if you've narrowed it down to a
    bare-bones page.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com

    "Dune88" <> wrote in message
    news:...
    > I've got some code that uses Page.ClientScript.RegisterStartupScript to
    > call
    > a javascript function from the Page_Load method in the code behind.
    >
    > The code works fine in IE but the javascript function is not called at all
    > in Firefox. I stripped my code back to the basics and all I have now is a
    > webform with no controls on it and the following in the code behind:
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > Page.ClientScript.RegisterStartupScript(typeof(Page),
    > "TestScript", "<script type='text/jscript'>alert('test
    > script');</script>");
    > }
    >
    > This very basic webform works in IE but not in Firefox.
    >
    > I'm running .NET Framework 3.5, IE 7 and Firefox 2.0.0.11. I added
    > "onload="alert('onLoad')"" to the body tag to ensure that javascript was
    > enabled and popups were not being blocked in Firefox. The onLoad alert
    > came
    > up fine but the alert added by the RegisterStartupScript did not come up.
    >
    > Can anyone shed any light on this?
    >
    > Cheers
    Jonathan Wood, Feb 13, 2008
    #2
    1. Advertising

  3. How about trying:
    script type='text/javascript'

    "jscript" is Microsoft - specific and the kind people at Firefox apparently
    don't take kindly to it.
    -- Peter
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    MetaFinder: http://www.blogmetafinder.com


    "Dune88" wrote:

    > I've got some code that uses Page.ClientScript.RegisterStartupScript to call
    > a javascript function from the Page_Load method in the code behind.
    >
    > The code works fine in IE but the javascript function is not called at all
    > in Firefox. I stripped my code back to the basics and all I have now is a
    > webform with no controls on it and the following in the code behind:
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > Page.ClientScript.RegisterStartupScript(typeof(Page),
    > "TestScript", "<script type='text/jscript'>alert('test script');</script>");
    > }
    >
    > This very basic webform works in IE but not in Firefox.
    >
    > I'm running .NET Framework 3.5, IE 7 and Firefox 2.0.0.11. I added
    > "onload="alert('onLoad')"" to the body tag to ensure that javascript was
    > enabled and popups were not being blocked in Firefox. The onLoad alert came
    > up fine but the alert added by the RegisterStartupScript did not come up.
    >
    > Can anyone shed any light on this?
    >
    > Cheers
    Peter Bromberg [C# MVP], Feb 13, 2008
    #3
  4. "Dune88" <> wrote in message
    news:...

    > protected void Page_Load(object sender, EventArgs e)
    > {
    > Page.ClientScript.RegisterStartupScript(typeof(Page),
    > "TestScript", "<script type='text/jscript'>alert('test
    > script');</script>");
    > }
    >
    > This very basic webform works in IE but not in Firefox.
    >
    > I'm running .NET Framework 3.5, IE 7 and Firefox 2.0.0.11. I added
    > "onload="alert('onLoad')"" to the body tag to ensure that javascript was
    > enabled and popups were not being blocked in Firefox. The onLoad alert
    > came
    > up fine but the alert added by the RegisterStartupScript did not come up.
    >
    > Can anyone shed any light on this?


    That's because you're telling the browser that it should process a script
    type of text/jscript - that will only work in IE, Opera and Safari:
    http://krijnhoetmer.nl/stuff/javascript/mime-types/

    If you *must* include the script tags, make sure you use
    type='text/javascript', which will also work in FireFox etc...

    However, it's much safer to let ASP.NET output the script tags dynamically
    according to what browser it detects, by using the boolean overload of the
    RegisterStartupScript method:
    ClientScript.RegisterStartupScript(typeof(Page), "TestScript", "alert('test
    script');", True);


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Feb 13, 2008
    #4
  5. Dune88

    Dune88 Guest

    Thanks for all the replies!

    Changing "jscript" to "javascript" worked perfectly as did using the boolean
    overload.

    Doh! I should have spotted that earlier!

    "Mark Rae [MVP]" wrote:

    > "Dune88" <> wrote in message
    > news:...
    >
    > > protected void Page_Load(object sender, EventArgs e)
    > > {
    > > Page.ClientScript.RegisterStartupScript(typeof(Page),
    > > "TestScript", "<script type='text/jscript'>alert('test
    > > script');</script>");
    > > }
    > >
    > > This very basic webform works in IE but not in Firefox.
    > >
    > > I'm running .NET Framework 3.5, IE 7 and Firefox 2.0.0.11. I added
    > > "onload="alert('onLoad')"" to the body tag to ensure that javascript was
    > > enabled and popups were not being blocked in Firefox. The onLoad alert
    > > came
    > > up fine but the alert added by the RegisterStartupScript did not come up.
    > >
    > > Can anyone shed any light on this?

    >
    > That's because you're telling the browser that it should process a script
    > type of text/jscript - that will only work in IE, Opera and Safari:
    > http://krijnhoetmer.nl/stuff/javascript/mime-types/
    >
    > If you *must* include the script tags, make sure you use
    > type='text/javascript', which will also work in FireFox etc...
    >
    > However, it's much safer to let ASP.NET output the script tags dynamically
    > according to what browser it detects, by using the boolean overload of the
    > RegisterStartupScript method:
    > ClientScript.RegisterStartupScript(typeof(Page), "TestScript", "alert('test
    > script');", True);
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    >
    >
    Dune88, Feb 13, 2008
    #5
  6. Dune88

    Misliplavo

    Joined:
    Jun 26, 2009
    Messages:
    4
    Hi all,
    I have a similar problem with FF and JavaScript :)
    It works in IE but not in FF ...

    <b>Javascript :</b>

    function ChangeMessage(element) {
    element.value = 1 - element.value;
    __doPostBack(element.id, '');
    }

    function ValidateAddClub(element) {
    alert(element);
    if (element.value == "Free") {
    return true;
    }
    return false;
    }


    <b>Code behind :</b>

    string javaScriptEvent = string.Empty;
    javaScriptEvent = string.Format("javascript:ChangeMessage({0});", this.hdnMessageStatus.ClientID);
    this.txtNewClubName.Attributes.Add("onblur", javaScriptEvent);
    // the first function works (?!)


    javaScriptEvent = string.Format("javascript:return ValidateAddClub({0});", this.hdnValidationStatus.ClientID); ClientID, UniqueID - same result...
    this.lbtnAdd.Attributes.Add("onclick", javaScriptEvent);
    // but this one doesnt...


    <b>HTML :</b> (page source)

    <div class="submiter">
    <a onclick="javascript:return ValidateAddClub(ctl00_cphPage_hdnValidationStatus);" id="ctl00_cphPage_lbtnAdd" class="roll" href="javascript:__doPostBack('ctl00$cphPage$lbtnAdd','')">ADD CLUB</a>
    </div>

    I would appreciate if someone answer to me...
    Thx... :)
    Misliplavo, Jun 26, 2009
    #6
    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. Mark
    Replies:
    1
    Views:
    552
    Natty Gur
    Oct 16, 2003
  2. Guest
    Replies:
    3
    Views:
    424
    Patrick Olurotimi Ige
    Dec 8, 2004
  3. Dav
    Replies:
    2
    Views:
    1,486
    Mark Rae
    Dec 21, 2006
  4. Moe Sisko
    Replies:
    0
    Views:
    440
    Moe Sisko
    Mar 10, 2008
  5. Suling

    AJAX and RegisterStartupScript

    Suling, Jun 22, 2009, in forum: ASP .Net
    Replies:
    2
    Views:
    1,026
    Suling
    Jun 23, 2009
Loading...

Share This Page