28298

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

Answer1:

You can create a function like this:

function IsPostBack() { var ret = '<%= Page.IsPostBack%>' == 'True'; return ret; }

Answer2:

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 -->

Answer3:

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>

Recommend

  • SCRIPT5009: 'google' is undefined
  • WScript is undefined
  • Get or convert Week of year to ISO week
  • How to distribute Java-based software?
  • Getting error 'Cannot read property 'document' of undefined' while importing exp
  • Possible to “watch” both HAML and SASS at the same time?
  • Web.config system.webserver errors
  • How to revert to previous XCode version?
  • How can I set a binding to a Combox in a UserControl?
  • What and where is mdimport
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • PHP buffered output depending on server setting?
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Android screen density dpi vs ppi
  • Accessing IRQ description array within a module and displaying action names
  • Get object from AWS S3 as a stream
  • How would I use PHP exceptions to define a redirect?
  • How to extract text from Word files using C#?
  • Javascript Callbacks with Object constructor
  • Where to put my custom functions in Wordpress?
  • VBA Convert delimiter text file to Excel
  • ORA-29908: missing primary invocation for ancillary operator
  • Numpy divide by zero. Why?
  • Comma separated Values
  • How to disable jQuery.jplayer autoplay?
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • How to stop GridView from loading again when I press back button?
  • need help with bizarre java.net.HttpURLConnection behavior
  • How to get Windows thread pool to call class member function?
  • How can I remove ASP.NET Designer.cs files?
  • Bitwise OR returns boolean when one of operands is nil
  • python draw pie shapes with colour filled
  • sending mail using smtp is too slow
  • Is there any way to bind data to data.frame by some index?
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?