1

I ran into an error while trying to gain access to the required libraries for JavaScript development. I copied this code straight from Microsoft's documentation, however when it is executed on my page through a content editor web part, I get the following error:

Unable to get property 'split' of undefined or null reference

It doesn't like the split call in the getQueryStringParameter() function. What exactly is the function trying to do with my document.URL, and what does the URL not have that causes it to not get processed properly?

<script 
src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js" 
type="text/javascript">
</script>
<script
type="text/javascript"
src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js">
</script>
<script type="text/javascript">
var hostweburl;

// Load the required SharePoint libraries.
$(document).ready(function () {

    // Get the URI decoded URLs.
    hostweburl =
        decodeURIComponent(
            getQueryStringParameter("SPHostUrl")
    );

    // The js files are in a URL in the form:
    // web_url/_layouts/15/resource_file
    var scriptbase = hostweburl + "/_layouts/15/";

    // Load the js files and continue to
    // the execOperation function.
    $.getScript(scriptbase + "SP.Runtime.js",
        function () {
            $.getScript(scriptbase + "SP.js", execOperation);
        }
    );
});

// Function to execute basic operations.
function execOperation() {

    // Continue your program flow here.

}

// Function to retrieve a query string value.
// For production purposes you may want to use
// a library to handle the query string.
function getQueryStringParameter(paramToRetrieve) {
    var params =
        document.URL.split("?")[1].split("&");
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == paramToRetrieve)
            return singleParam[1];
    }
}

6

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.