Here is the JavaScript I have to load a URL into a Div:
Expand|Select|Wrap|Line Numbers
- function setResponseHtml(pUrl, pDiv) {
- var lHttp = getHTTPObjectHtml();
- var lUrl = pUrl;
- lUrl = lUrl.replace("+","%2b");
- if (isBusy) {
- lHttp.onreadystatechange = function () {}
- lHttp.abort();
- }
- lHttp.open("GET", lUrl , true);
- lHttp.onreadystatechange = function() { getHttpResponseText(pDiv, lHttp); };
- if (window.XMLHttpRequest) { // Mozilla check
- if (!isBusy) {
- isBusy = true;
- lHttp.send(null);
- }
- } else { // IE Check
- isBusy = true;
- lHttp.send(null);
- }
- }
- function getHTTPObjectHtml() {
- http_request = false;
- if (window.XMLHttpRequest) { // Mozilla, Safari, ...
- http_request = new XMLHttpRequest();
- if (http_request.overrideMimeType) {
- http_request.overrideMimeType('text/html');
- }
- } else if (window.ActiveXObject) { // IE
- try {
- http_request = new ActiveXObject("Msxml2.XMLHTTP");
- } catch (e) {
- try {
- http_request = new ActiveXObject("Microsoft.XMLHTTP");
- } catch (e) {}
- }
- }
- return http_request;
- }
- function getHttpResponseText(pDiv, pHttp) {
- var lHttp = pHttp;
- if (lHttp == null) // just in case!
- lHttp = gHttp;
- if (lHttp.readyState == 4) {
- isBusy = false;
- var status = "";
- try {
- status = lHttp.statusText;
- if (lHttp.status == 200) {
- var htmlDocument = lHttp.responseText;
- document.getElementById(pDiv).innerHTML = htmlDocument;
- }
- } catch(e) {
- status = "Trouble accessing it";
- document.getElementById(pDiv).innerHTML = e.message;
- }
- } else {
- if (getAjaxLoadingMessage() != '' && getAjaxLoadingMessage() != null) {
- // only display a loading message if it is defined
- setVisible(pDiv, true);
- document.getElementById(pDiv).innerHTML = getAjaxLoadingMessage();
- }
- return;
- }
- }
- var gAjaxLoadingMessage = '<b>Loading...</b>';
- function setAjaxLoadingMessage(pMessage) {
- gAjaxLoadingMessage = pMessage;
- }
- function getAjaxLoadingMessage() {
- return(gAjaxLoadingMessage);
- }
urlString is a string containing the URL that I wish to load in the div, and divString a string containing the id of the div I wish to load the URL into.
I've tested my code on IE before and it worked properly with a very small file with nothing in except for a couple words. I think the problem may go back to these pages, both the one the users calls the function from and the page to be loaded, contain SharePoint Web Parts. That is the only difference I can think of between the original pages I used to test the code and the pages I am working with now.
An error occurs in IE when trying to load the Div, the error being "[object Error]" and the error message being "Unknown runtime error" if I print e and e.message respectively from the getHttpResponseText function.
This is the section of code where the error occurs:
Expand|Select|Wrap|Line Numbers
- try {
- status = lHttp.statusText;
- if (lHttp.status == 200) {
- var htmlDocument = lHttp.responseText;
- if (gAjaxForeground) {
- setVisible(pDiv, true);
- }
- document.getElementById(pDiv).innerHTML = htmlDocument;
- }
- } catch(e) {
- status = "Trouble accessing it";
- document.getElementById(pDiv).innerHTML = e.message;
- }