简体   繁体   English

RegisterStartupScript不为Firefox asp.net编写任何脚本

[英]RegisterStartupScript not writing any script for firefox asp.net

This seems to be a weird scenario but the asp.net's RegisterStartupScript does not throw any scripts on the page for firefox. 这似乎是一个奇怪的情况,但是asp.net的RegisterStartupScript不会在页面上抛出任何用于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. 当我为Chrome执行“查看页面源代码”时,会看到代码,但对于Firefox则什么也看不到。 Also alert gets fired for chrome but, obviously, for firefox there is no alert. 同时会为Chrome触发警报,但是很明显,对于Firefox没有警报。

I have also tried with the last parameter in the above mentioned code ie addScriptTags as true and removing the script tag from inside but to no avail. 我也尝试了上述代码中的最后一个参数,即addScriptTags为true并从内部删除了脚本标签,但无济于事。

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. 如果此信息很重要,则Firefox版本为39.0,铬版本为44.0.2。

在此处输入图片说明

Please let me know if any more information is required from my end. 如果我需要更多信息,请告诉我。

Please help! 请帮忙! Thanks in advance! 提前致谢!

To register javascript to the page use ClientScriptManager.RegisterStartupScript .check the link for correct usage 要将JavaScript注册到页面,请使用ClientScriptManager.RegisterStartupScript。检查链接是否正确使用

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

I got this working by moving the related code to the prerender event of the page. 我通过将相关代码移到页面的prerender事件来完成此工作。

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! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM