1

i'm trying to read text file that content this :

<p> copyright 2016 ..... </p>

from a file text in sharepoint library:URL

https://mydomaine.sharepoint.com/sites/sitecollection/mysite/assets/footer.txt

using javascript and inject it in my html master page in the footer <footer></footer> any solution?

1 Answer 1

2

Accessing the contents of a text file is relatively straightforward using an XMLHttpRequest object.

(function(){
    var serverRelativeUrlOfMyFile = "/sites/sitecollection/mysite/assets/footer.txt";
    var req = new XMLHttpRequest();
    req.onreadystatechange=handler;
    req.open("GET", serverRelativeUrlOfMyFile, true); // params: (method, url, async)
    req.send();
    function handler(){
        if(req.readyState == 4 && req.status == 200){
            // you can now access the file content via the responseText property
            document.querySelector("footer").innerHTML = req.responseText;
        }
    }
})();

The above code is roughly equivalent to the following jQuery:

var serverRelativeUrlOfMyFile = "/sites/sitecollection/mysite/assets/footer.txt";
$.ajax({
    url: serverRelativeUrlOfMyFile,
    type: "GET"
}).done(handler);
function handler(data){
    $("footer").html(data);
}
Sign up to request clarification or add additional context in comments.

4 Comments

what's the difference between the two codes? and wich one is recommended to use?
I personally recommend the first code example. The second one requires the jQuery library to have already been loaded; I included it in case you were already using jQuery and wanted a shorter piece of code.
thank you it works just fine i have only one issue : somme characters are not displayed corectely , is it related to the encoding type? and how to resolve it ?
One option is to save your text file using UTF-8 encoding. (In Notepad, for example, choose "Save As" and in the save dialog you can change the Encoding from "ANSI" to "UTF-8".)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.