Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 948
user3320324 Avatar asked Dec 25 '22 02:12

user3320324


2 Answers

You can create a function like this:

function IsPostBack() {
    var ret = '<%= Page.IsPostBack%>' == 'True';
    return ret;
}
like image 128
MrCADman Avatar answered Jan 21 '23 16:01

MrCADman


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 -->
like image 40
welegan Avatar answered Jan 21 '23 16:01

welegan