Solved

spservices different results in IE and Chrome.

Posted on 2016-11-28
11
95 Views
Last Modified: 2016-12-01
Why is the following working in IE only?  It does not work in Chrome or firefox.  In firefox, I believe it has something to do with spservices.

/ JavaScript source code
$(document).ready(function () {
    SP.SOD.executeOrDelayUntilScriptLoaded(retrieveData, "sp.js");
    //'SP.ClientContext',
    itemProjectNumber = getQueryString('projNumber');

    //Check to see if project number exist. If so, change buttons to update from submit
    var itemIDPPD = getID(itemProjectNumber, "Procurement Plan (Design)");
    if (itemIDPPD) {
        alert(itemIDPPD);
        $("#design_procPlan").hide();
        $("#updDesign_procPlan").show();
    }
    else {
        $("#design_procPlan").show();
        $("#updDesign_procPlan").hide();
    }

    $("#updDesign_procPlan").on("click", function () {
        updateItem(itemIDPPD, "Procurement Plan (Design)");
    });

});

var getQueryString = function (field, url) {
    var href = url ? url : window.location.href;
    var reg = new RegExp('[?&]' + field + '=([^&#]*)', 'i');
    var string = reg.exec(href);
    return string ? string[1] : null;
};

function getID(pid, category) {
    var qry = "<Query><Where>" +
				     "<And>" +
         				"<Eq>" +
            				"<FieldRef Name='PlanningCategory' />" +
            				"<Value Type='Choice'>" + category + "</Value>" +
         				"</Eq>" +
         				"<Eq>" +
            				"<FieldRef Name='ProjectNumber' />" +
            				"<Value Type='Text'>" + pid + "</Value>" +
         				"</Eq>" +
      				"</And>" +
   				  "</Where>" +
   			  "</Query>";

    $().SPServices({
        operation: "GetListItems",
        async: false,
        listName: "Project Projections",
        CAMLViewFields: "<ViewFields>" +
      					"<FieldRef Name='ID' />" +
      					"<FieldRef Name='PlanningCategory' />" +
      					"<FieldRef Name='ProjectNumber' />" +
   					 "</ViewFields>",
        CAMLQuery: qry,
        completefunc: function (xData, Status) {    //<-- In firefox, when I'm stepping through each line, it skips this to line 64
            //alert(xData.responseXML.xml);
            itemID = $(xData.responseXML.xml).find("z\\:row").attr("ows_ID");
            //	        alert(itemID);
        }
    });

    return itemID;
}

Open in new window

0
Comment
Question by:Isaac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 41904980
You're using asynchronous ajax call which mean, itemid is not immediately availbale so you CAN'T return it at the end of your function
The following should fix it :
$(document).ready(function () {
	SP.SOD.executeOrDelayUntilScriptLoaded(retrieveData, "sp.js");
	//'SP.ClientContext',
	itemProjectNumber = getQueryString('projNumber');

	//Check to see if project number exist. If so, change buttons to update from submit
	getID(itemProjectNumber, "Procurement Plan (Design)", part2);
});

var part2 = function(itemIDPPD) {

	if (itemIDPPD) {
		alert(itemIDPPD);
		$("#design_procPlan").hide();
		$("#updDesign_procPlan").show();
	}
	else {
		$("#design_procPlan").show();
		$("#updDesign_procPlan").hide();
	}

	$("#updDesign_procPlan").on("click", function () {
		updateItem(itemIDPPD, "Procurement Plan (Design)");
	});
};

var getQueryString = function (field, url) {
	var href = url ? url : window.location.href;
	var reg = new RegExp('[?&]' + field + '=([^&#]*)', 'i');
	var string = reg.exec(href);
	return string ? string[1] : null;
};

function getID(pid, category, callback) {
	var qry = "<Query><Where>" +
		"<And>" +
		"<Eq>" +
		"<FieldRef Name='PlanningCategory' />" +
		"<Value Type='Choice'>" + category + "</Value>" +
		"</Eq>" +
		"<Eq>" +
		"<FieldRef Name='ProjectNumber' />" +
		"<Value Type='Text'>" + pid + "</Value>" +
		"</Eq>" +
		"</And>" +
		"</Where>" +
		"</Query>";

	$().SPServices({
		operation: "GetListItems",
		async: true,
		listName: "Project Projections",
		CAMLViewFields: "<ViewFields>" +
			"<FieldRef Name='ID' />" +
			"<FieldRef Name='PlanningCategory' />" +
			"<FieldRef Name='ProjectNumber' />" +
			"</ViewFields>",
		CAMLQuery: qry,
		completefunc: function (xData, Status) {    //<-- In firefox, when I'm stepping through each line, it skips this to line 64
			// alert(xData.responseXML.xml);
			itemID = $(xData.responseXML.xml).find("z\\:row").attr("ows_ID");
			if(typeof callback == "function") {
				callback(itemID);
			}
			// alert(itemID);
		}
	});

	//return itemID; // USELESS UNTIL YOU USE SYNCHRONOUS AJAX CALL
}

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 41904986
I see you put async: false
but modern browser disable and don't allow this as it froze the browser and it's not acceptable, human are master of the computer and should stay master of it. using async: false your computer say : "dont touch anything human, I'm working and I will give you hand on me when I will be ready for that"
0
 
LVL 5

Author Comment

by:Isaac
ID: 41905042
Strange.  Now it's appearing in Chrome and Firefox correctly but not IE.
Actually, the changes are not reflected in IE when I F12.  Even after I do a hard refresh, delete browser history and restart my computer, the changes are still not reflecting in IE.  very strange behavior
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 82

Expert Comment

by:leakim971
ID: 41905226
if you don't see the changes on IE, it may be related to your server, try to open the JS file directly in your IE browser
0
 
LVL 5

Author Comment

by:Isaac
ID: 41906017
It finally updated but I get a new error
ie error
0
 
LVL 82

Expert Comment

by:leakim971
ID: 41906033
it's an other code.
add a dot after "document"
document.getElementsByName
and not :
documentgetElementsByName
but again, this is over the issue described here...
0
 
LVL 5

Author Comment

by:Isaac
ID: 41906042
Actually, the file in the server never updated.  It reverted back to the old code and not with the changes you suggested.  It did the same in Chrome and Firefox.  I am contacting the SharePoint host right now.
0
 
LVL 5

Author Comment

by:Isaac
ID: 41906256
Ok.  It was my fault.  I had two of the same functions in my code.
Below is an excerpt of the code and attached is my code.
    getID(itemProjectNumber, "Procurement Plan (Design)", part2);
});


var part2 = function (itemIDPPD) {
    console.log(itemIDPPD);   //<--- UNDEFINED so it causes an error in other places.
    if (itemIDPPD) {
        //alert(itemIDPPD);
        $("#design_procPlan").hide();
       

Open in new window

Please see attached file.
0
 
LVL 5

Author Comment

by:Isaac
ID: 41906259
'itemIDPPD' shows up as undefined
0
 
LVL 82

Expert Comment

by:leakim971
ID: 41907805
we're over this question.
Currently, your query return nothing so itemIDPPD is null
<rs:data ItemCount="0"></rs:data>

result of th query look like : xData.responseXML.xml
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/"><GetListItemsResult><listitems xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
					<rs:data ItemCount="0">
					</rs:data>
				</listitems></GetListItemsResult></GetListItemsResponse></soap:Body></soap:Envelope>

Open in new window

0
 
LVL 5

Author Comment

by:Isaac
ID: 41908446
Thanks leakIm971!

I had to strip the code and break it down to bare minimum in jsFiddle for me to get it.  I think the problem in my call to the SharePoint list as you mentioned.

https://jsfiddle.net/isogunro/g2cr896a/
1

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question