开发者

RunOnce for javascript On page Post back.?

<script language="开发者_如何转开发javascript" type="text/javascript">
    function pageLoad() {

            ShowPopup();

     }
</script>

The above code will call ShowPopup on pageLoad() . I need that ShowPopup method to work only first time

ie;

need to check IsPostback Property of the Page.

Can i do this from client side(JavaScript) .?


Sure, you can inject server-side property values into your markup by using script tags:

C#:

if (<%=(!Page.IsPostBack).ToString() %>) ShowPopup();

VB.NET

if(<%=(Not Page.isPostBack).ToString().ToLower() %>) ShowPopup();


You can simply leverage JavaScript closures and call it as often as you like

function once(fn) {
    return function() {
        fn.apply(this, arguments);
        fn = function() {};
    }
}

function pageLoad() {
    if(bPostBack == null)
        ShowPopup();

} 

pageLoad = once(pageLoad);

This way the pageLoad function will only be run once afterwards it only executes an empty function..

Or you could simplify things a bit:

function pageLoad() {
    console.log("foo");
    pageLoad = function() {};
 }

(Note the once function above is more generic.. but both do what you need)

Update: This obviously does not work if your page reloads as the JavaScrip will simply be re-run again and again. In that case you need to set a cookie or some other flag you can retrieve to check if the popup was already shown..

You could use the HTML5 Localstorage - But that's not supported on all browsers yet.. A better idea would be to generate a cookie that you can retrieve like described in this article: http://www.quirksmode.org/js/cookies.html


Yes you can by using a javascript variable like this:

In your code behind, when post back occurs call following code to register javascript block.

ClientScript.RegisterClientScriptBlock(GetType(), "postbackFlag", "var bPostBack = true;", true);

and in your javascript check the value of this variable to detect the occurance of post back:

<script>
function pageLoad() {
    if(bPostBack == null)
        ShowPopup();

 }   
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜