Can't get snippet to run on page load

Mostly because I don’t know how :slight_smile:

I have a script which works fine when pasted into the console, but I would like for it to run on Page Load.

var params = arguments[0],

    step = params.step,

$ = skuid.$;

var userModel = skuid.model.getModel(‘User’),
    user = userModel.getFirstRow(),
   weekUpdateModel = skuid.model.getModel(‘WeeklyUpdate’),
   weekUpdate = weekUpdateModel.getFirstRow();
var userAlias = user.Alias + ’ Update - ’ + user.Today__c;
weekUpdateModel.updateRow(weekUpdate,‘Note’,userAlias);
$.each(weekUpdateModel.registeredItems,function(){
  this.refreshFields();
});

Thanks,

Jacob

You actually want this snippet to run AFTER page load,  because you need the model data to be available.  This is why it runs successfully from the console.  Models have loaded etc.  You need to wrap your snippet in a function that waits for the page to load before executing.  Somthing like this: 

(function(skuid){ var $ = skuid.$; $(function(){ Your Function goes here...... }); })(skuid);


Actually if you create a new Javascript resource of type Inline you will get this code prepopulated for you.  

Thanks Rob! Learning JS by sheer force is challenging sometimes.

Page loading snippet

Hi Rob, This works fine for a page. But when i use this page as a popup in another, its not working. Can u please suggest a solution for it? Thanks!

Check out J’s discussion here: https://community.skuidify.com/skuid/topics/page-include-inline-javascript-not-running-has-anyone-do…

(function(skuid) {&nbsp; &nbsp; <br>&nbsp; &nbsp; var $ = skuid.$;<br>&nbsp; &nbsp; // Register a snippet to run<br>&nbsp; &nbsp; skuid.snippet.registerSnippet('HelloWorld', function() {<br>&nbsp; &nbsp; &nbsp; &nbsp; console.log("Go Skuid!");<br>&nbsp; &nbsp; });<br>&nbsp; &nbsp; // Called on page load<br>&nbsp; &nbsp; function init() {<br>&nbsp; &nbsp; &nbsp; &nbsp; skuid.snippet.getSnippet('HelloWorld')();<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; // Run the snippet initially on page load<br>&nbsp; &nbsp; $('.nx-page').one('pageload', function() {<br>&nbsp; &nbsp; &nbsp; &nbsp; init();<br>&nbsp; &nbsp; });<br>})(skuid);

Thanks! Found the solution :slight_smile: