I am using onclick show function to accomplish following sequence..
- Onclick, a div with content will appear for 5 seconds
- After 5 secs, old div hides and new div with content appears
function show() {
document.getElementById("myDiv").style.display="block";
setTimeout("hide()", 5000); // 5 seconds
}
function hide() {
document.getElementById("myDiv").style.display="none";
show2();
}
function show2() {
document.getElementById("Div2").style.display="block";
}
I want to add one more step here…
- Onclick, check if a text file exists on a website and at the same time display a div for 5 secs
- Display specific div if file does not exists
- Display another div if file exists
2
Answers
To check if a file exists or not can be done using the
success
andfail
callbacks of the jQuery$.get
method, see the snippet below.Note that in the snippet, no URL will ever load anything because (a) the URLs are invalid, and (b) even with valid URLs, the configured CORS policies of the domain
stacksnippets.com
make any kind of Ajax requests impossible to succeed. Meaning that when you run it, you will only see thefail
scenario happen.CORS may also be an important factor to consider for your own domain.
Also note that you will have to put your
show/hide a certain DIV
logic inside thesuccess
andfail
callback functions, or at least you’ll need to call the relevant code from there.@Peter said correct. CORS will prevent you accomplishing what you want. This can be done easily if you want to do this on your own down.
You will need to add one more div and a function.
and then….