I have a iframe in my page. If the iframe doesn’t load, want it to alert the message “pdf not found” and if the iframe does load, it should alert “pdf opened“.
Does anyone know how to achieve that?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
So, the idea is to use an Ajax-request to “test” the URL. Ajax-requests enable you to bind “success” and “error” handlers – unlike
<iframe>elements which only provide a “load” handler.Of course, Ajax-requests are restricted by the Same Origin Policy (unless the web-server enables CORS), but you stated that the PDF is on the same domain, so there shouldn’t be any issues.
Also, you stated that you use the Mootools library – I use jQuery, so I can only provide you with a jQuery solution, but since we’re making a simple Ajax-request with “success” and “error” handlers, you should be able to recreate a Mootools solution based on my jQuery solution easily.
So, given an iframe and an URL:
The Ajax-request:
Success-demo: http://jsfiddle.net/CZWdL/1/show/
Error-demo: http://jsfiddle.net/CZWdL/2/show/
So, if the Ajax-request triggers an “error” event, we simply alert the “Not found” message immediately. If, however, the Ajax-request triggers a “success” event, we assign a “load” handler to our IFRAME element (this “load” handler will eventually alert the “Loaded” message), and set the URL to its
srcproperty manually.