0

There is a performance time delay in rendering window.navigate(strURL);. Until then I want display a wait message using mainContainer.innerHTML = HTMLwait; but it is not working. Any help is appreciated. Thanks!

var HTMLwait = "<div style='width:300px;height:300px;top:40%;left:45%;position:absolute;overflow:auto;display:inline;'><div style='width:50px;height:50px;top:20%;left:4%;position:absolute;overflow:auto;display:inline;'><img src='../../images/Developer/ajax-loader.gif'></div><div style='width:50px;height:50px;top:35%;left:0%;position:absolute;font-family:arial;font-size:12px;color:#000000;'>Loading...</div></div>";

var strURL = window.location.href;

var mainContainer = $(".mainContainer");
if (mainContainer != null) {
  mainContainer.innerHTML = HTMLwait;
}

window.navigate(strURL);

2 Answers 2

3

mainContainer is a jQuery object and therefore does not have a innerHTML property. To set the HTML of an element selected via jQuery, use html().

Note that a jQuery object will never be null. To check if it exists, use the length property. You should note though that checking that the element exists is redundant, as jQuery will not throw an error when calling a method on a jQuery object that holds no Elements.

Also you appear to be using window.location.href to reload the page. A much simpler method would be to just use window.location.reload().

With all that said, try this:

var HTMLwait = "<div style='width:300px;height:300px;top:40%;left:45%;position:absolute;overflow:auto;display:inline;'><div style='width:50px;height:50px;top:20%;left:4%;position:absolute;overflow:auto;display:inline;'><img src='../../images/Developer/ajax-loader.gif'></div><div style='width:50px;height:50px;top:35%;left:0%;position:absolute;font-family:arial;font-size:12px;color:#000000;'>Loading...</div></div>";
var mainContainer = $(".mainContainer").html(HTMLwait);
window.location.reload();

Finally, there is a lot of inline styling in the HTML string you're appending to the DOM. You should really avoid inline styles. Instead place a class on the elements you append, then style them via an external stylesheet.

Sign up to request clarification or add additional context in comments.

1 Comment

var strURL = window.location.href; is used for getting the URL to redirect( not for reload) and this code does not work.Not showing the HTML wait message
1

For jQuery objects use .html(),

$('.mainContainer').html(HTMLwait);

If you would like to use .innerHTML property:

document.querySelector('.mainContainer').innerHTML = HTMLwait;

var HTMLwait = "<div style='width:300px;height:300px;top:40%;left:45%;position:absolute;overflow:auto;display:inline;'><div style='width:50px;height:50px;top:20%;left:4%;position:absolute;overflow:auto;display:inline;'><img src='../../images/Developer/ajax-loader.gif'></div><div style='width:50px;height:50px;top:35%;left:0%;position:absolute;font-family:arial;font-size:12px;color:#000000;'>Loading...</div></div>";


showMessage(HTMLwait);

function showMessage(message) {

  $('.mainContainer').html(HTMLwait);

  setTimeout(function() {
    console.log('time to reload the page');
    // uncomment line below
    // window.location.reload();
  }, 1000); // executed after one second
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="mainContainer"></div>

1 Comment

@user2082630 If you would like to delay reload - to let users see the message first, use setTimeout function. I've updated my answer, please see example how to implement reload. If it helped you, please accept my answer.

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.