0

This seems to be a weird scenario but the asp.net's RegisterStartupScript does not throw any scripts on the page for firefox. This works fine for chrome. The code is as below:

ScriptManager.RegisterStartupScript(this, GetType(), "testfunc", "<script type='text/javascript'>function testfunc(){ alert('hi123'); } testfunc(); </script>", false);

When I do "View page source" for chrome the code is seen but for firefox its nothing. Also alert gets fired for chrome but, obviously, for firefox there is no alert.

I have also tried with the last parameter in the above mentioned code i.e. addScriptTags as true and removing the script tag from inside but to no avail.

The following is the screenshot for your reference. The firefox version is 39.0 and that of chrome is 44.0.2, if this information matters.

enter image description here

Please let me know if any more information is required from my end.

Please help! Thanks in advance!

samar
  • 5,021
  • 9
  • 47
  • 71
  • Are you trying to register javascript or AJAX script?.I see you are specifying script type=javascript. – shreesha Aug 12 '15 at 13:18

2 Answers2

0

To register javascript to the page use ClientScriptManager.RegisterStartupScript .check the link for correct usage

use it like this

ClientScript.RegisterStartupScript(this.GetType(), "testfunc", "<script type='text/javascript'>function testfunc(){ alert('hi123'); } testfunc(); </script>", false);

or like this

this.ClientScript.RegisterStartupScript(this.GetType(), "alertscript","<script>alert('Hai');</script>");
shreesha
  • 1,811
  • 2
  • 21
  • 30
  • I am getting compile error as "No overload of method RegisterStartupScript takes 4 arguments". A similar error occurred for the second solution you have provided. – samar Aug 20 '15 at 06:16
  • Hi Shreesha, I got it working finally. I moved the same code to preRender event and it worked! Thanks for your help! – samar Aug 20 '15 at 06:43
0

I got this working by moving the related code to the prerender event of the page.

So the code looks like the following

protected void Page_PreRender(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this, GetType(), "testfunc", "<script type='text/javascript'>function testfunc(){ alert('hi123'); } testfunc(); </script>", false);
}

I figured out the solution from the question here

Hope this helps others!

Cheers!

Community
  • 1
  • 1
samar
  • 5,021
  • 9
  • 47
  • 71