Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

69
Views
Need help to have javascript only run once per user when they click button

Where and what do I need to add to this script or button to have it only run once per user. Currently everytime the page is refreshed it adds a new duplicate record to sharepoint list.

Here is a sample of the javascript code - what and where do I add lines in to make it only run once per user and not duplicate every time the page is refreshed


<script type="text/javascript">
 $(document).ready(function()
 {
  ExecuteOrDelayUntilScriptLoaded(AddListItem, "sp.js");
 });
function AddListItem(){
    var ListName = "MyList";
    var context = new SP.ClientContext.get_current(); // the current context is taken by default here
    //you can also create a particular site context as follows
    //var context = new SP.ClientContext('/Sites/site1');
    var lstObject = context.get_web().get_lists().getByTitle(ListName);
    var listItemCreationInfo = new SP.ListItemCreationInformation();
    var newItem = lstObject.addItem(listItemCreationInfo);
    newItem.set_item('Title', 'This is new item');
    // set values to other columns of the list here
    newItem.update();
    context.executeQueryAsync(Function.createDelegate(this, this.onSuccess),
        Function.createDelegate(this, this.onFailure));
 
     function onSuccess() {
 
        alert('Item created: ' + newItem.get_id());
    }
 
    function onFailure(sender, args) {
 
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
}
</script>


<html>
<body>
<input type="button" value="submit" id="btnSub" onclick="AddListItem();" />
    </html>
</body>   

It works and post data to mylist however every time the page is refreshed it adds another record.

7 months ago ยท Juan Pablo Isaza
Answer question
Find remote jobs