Solved

spservices different results in IE and Chrome.

Posted on 2016-11-28
11
20 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
  • 6
  • 5
11 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
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
Comment Utility
It finally updated but I get a new error
ie error
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 82

Expert Comment

by:leakim971
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
'itemIDPPD' shows up as undefined
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now