admin管理员组文章数量:1415664
I have a script that I want to pop a window after 5 page views. The java script works fine on the default.aspx page with a link to call it. But I want to launce it from my default.aspx.cs page after my session var count gets to 5. How can I do this? Is it possible?
default.aspx
<script type="text/javascript">
window.name = "Register";
function popWin(link) {
var w = window.open(link.href, link.target, 'width=500,height=600,resizable');
return w ? false : true; // if popup blocker, use the default behaviour of the link
}
</script>
Default.aspx.cs page
if (Session["PagesViewed"].ToString() == "5")
{
//Call my Javascript function How?????
}
I have a script that I want to pop a window after 5 page views. The java script works fine on the default.aspx page with a link to call it. But I want to launce it from my default.aspx.cs page after my session var count gets to 5. How can I do this? Is it possible?
default.aspx
<script type="text/javascript">
window.name = "Register";
function popWin(link) {
var w = window.open(link.href, link.target, 'width=500,height=600,resizable');
return w ? false : true; // if popup blocker, use the default behaviour of the link
}
</script>
Default.aspx.cs page
if (Session["PagesViewed"].ToString() == "5")
{
//Call my Javascript function How?????
}
Share
Improve this question
asked Dec 31, 2011 at 19:54
CsharpBeginnerCsharpBeginner
1,7738 gold badges40 silver badges68 bronze badges
3 Answers
Reset to default 4You can output javascript into a LiteralControl
from your code behind:
.aspx:
<asp:Literal id="myLiteral" runat="server" />
Code behind:
myLiteral.Text = "<script type='text/javascript'>popWin('url');</script>";
When rendered this way, the output script will call the function - make sure it is lower in the page than where the function was defined to ensure it exists.
In ASP.Net you can do the following:
Page.ClientScript.RegisterStartupScript(
this.GetType(),
"openpopup",
"popWin('www.someurl.');",
True);
If you need more control over your scripts placement @Oded has a better approach - as trying to call a function that has not been defined isn't a good idea...
You cannot call javascript functions directly from C#. However, what you could do is pass a <script>
to the browser that executes the function.
response.Write("<script>popWin(something);</script>");
本文标签: cCalling Javascript function from Code behind pageStack Overflow
版权声明:本文标题:c# - Calling Javascript function from Code behind page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745209725a2647808.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论