Execute Javascript only on page load, not PostBack (SharePoint)
Asked Answered
Z

3

4

I'm trying to execute some JavaScript on page load on a custom page on a SharePoint site (it populates the people picker with the current user). The problem is that the code executes on postback too, which I don't want as it will reset any changes to the people picker.

I've tried using if(!IsPostBack) to no avail. Everything errors out at that point, giving

SCRIPT5009: 'IsPostBack' is undefined.

I can't find anything online to help with this. Any ideas? Thanks

Zoo answered 17/11, 2014 at 17:14 Comment(4)
IsPostBack is available in server side code, just clarifying, are you using that in javascript?Jamesy
Yes. But I can't get it to workZoo
I mean since that is available in server side code supporting property, you can not use that in javascript, rather look out the answer posted., if that does not work, let me know, i will try to post answer.Jamesy
Since it's on SharePoint, I don't have access to the codebehind. A co-worker shared a bit of code they have used that checks the document referrer. That function is working for me now. Thanks for the helpZoo
I
6

You can create a function like this:

function IsPostBack() {
    var ret = '<%= Page.IsPostBack%>' == 'True';
    return ret;
}
Implant answered 3/3, 2016 at 17:56 Comment(1)
Yes, that works - brilliant! I have been searching long and hard for a way to get my UpdatePanel content to be displayed immediately after (initial content display/) Page Load. That was fairly straightforward but the UI flickered because it was continually looping causing page loads - but no more! Thank you MrCADMan! :)Distribution
S
3

You may want to try the below. Use the JavaScript pageLoad method and use the isInAsyncPostBack Property of the PageRequestManager object to determine whether it's a postback. Refer the MSDN link here for more details.

<script type="text/javascript" language="javascript">
  function pageLoad(sender, args) {
    if (!Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()) {

     // call you JavaScript function in here

    }
  }
</script>
Stupefy answered 17/11, 2014 at 17:36 Comment(4)
With this, it is running on pageload and postbackZoo
Are you sure the js function is not being referenced from elsewhere in the code?Stupefy
It works for me when I tried this locally. If it's not working for you then you may have to post some of your custom page code and the JS code.Stupefy
I'm really new at all of this. Defining IsPostBack in the page worked for meZoo
S
3

IsPostBack is not a javascript variable, it's a .NET webforms variable that is only available on the server so the client will complain about it. So what to do then? I suggest this mish-mash in your control's html:

<% if(IsPostBack) { %> <!-- runs on server -->

<script type="text/javascript">
 alert('will only be printed to html if not postback');
</script>

<% } %> <!-- ends server if-block -->
Suckerfish answered 17/11, 2014 at 23:19 Comment(1)
Thanks. I was wrongly thinking it was a javascript variable. That was the root issueZoo

© 2022 - 2024 — McMap. All rights reserved.