Running Multiple Times in IE

Nov 26, 2014 at 3:16 PM
Let me briefly explain my environment and then I will get to the issue that I appear to be experiencing.

SP2007 MOSS install. I do not have control of the master pages that have been customized. I am placing my code in a CEWP to run the solution.

When opening the page in IE the following code block runs 3 times. When opening in FF it only runs 1 time.
<!-- Add SPServices -->
<script language="javascript" type="text/javascript" src="/custompath/jquery.SPServices.min.js"></script>

<script>

    var thisUsersValues = $().SPServices.SPGetCurrentUser({
        fieldNames: ["ID", "Name", "Title", "FirstName", "LastName"],
        debug: false
    });
    
    var dateTime = new Date();
    CreateNewItem(thisUsersValues,location.href,dateTime);  

    
function CreateNewItem( userInfo, currentURL, currentDateTime) {
    $().SPServices({
        operation: "UpdateListItems",
        async: false,
        batchCmd: "New",
        listName: "PageTracking",
        valuepairs: [["Title", userInfo.Name], ["URLLocation", currentURL],["AccessTime", currentDateTime]],
        completefunc: function(xData, Status) {

            }
            });
            

    
}


</script>
Any help is appreciated. I have tried nesting in some options to make it only run once and it still ends up running multiple times.

Thanks!
Jim
Coordinator
Nov 26, 2014 at 3:41 PM
Jim:

I don't see anything obvious in your code. Are you using the Content Link in the CEWP to load the code above from a file? If not, it could be the fact that the code is inline in the CEWP. That's not a good idea for several reasons, but could be the issue.

How do you know it's running 3 times?

M.
Nov 26, 2014 at 3:50 PM
I know it is running 3 times because it inserts the data in the List 3 times, one right after another about 2 seconds apart per time. I also had placed an Alert on the page when testing. Unfortunately I am having to use a specialized/modified (by another team) CEWP that does not allow me to use the Content Link.

Something does not appear to be right on the page when using with IE, either way. I tried placing that code in Ready block and it still ran it multiple times.

Thanks for taking a look at the code. Oh well I will have to keep trying new approaches.
Coordinator
Nov 26, 2014 at 4:42 PM
I'd suggest stepping through it in the browser debugger to see where the looping is occurring.

Sometimes if you have the code inline, SharePoint ends up adding multiple CEWPs to the page, which could be what's going on.

M.
Dec 1, 2014 at 9:36 PM
I was able to get this working by checking if var thisUsersValues had a value in it. If it did not have any values it would run, otherwise it would just skip.

On another note. I added this code to a subsite and I need to reference the list in the correct location. Please help with how I would add it to the code above in the UpdateListItems.

Thanks!
Coordinator
Dec 1, 2014 at 9:41 PM
Weird. I'm still not sure what was going on there.

You can use the webURL option to point to the site where the list lives.

M.