3

Is there any solution in javascript which can be used to determine if pdf file exists on a given external url? object tag supports onerror event only in firefox but not in chrome/safari/edge.

I've tried with attaching href to link tag but that won't work if test from https to http

The idea is to display pdf in object tag if url is valid (file exists), otherwise a custom error should be displayed

PS Ajax call doesn't work because of cors

EDIT

<object onerror="alert('error')" id="myPdf" type="application/pdf" data="https://tech.ebu.ch/docs/tech/tech3285X.pdf">
    Unable to open the PDF file.
</object>

onerror will be triggered in Firefox but not in Chrome

ilija veselica
  • 9,414
  • 39
  • 93
  • 147
  • The onerror event works, show us some code. Most likely you have a syntax error. – Scriptonomy Oct 24 '17 at 17:39
  • Ajax will work if you make the call through a CORS proxy. You can either set up your own proxy, or you can use a public CORS proxy — just by prefixing your current request URL with `https://cors-anywhere.herokuapp.com/`. For code samples and a longer explanation, see the *How to use a CORS proxy to get around “No Access-Control-Allow-Origin header” problems* section of the answer at https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested-resource-whe/43881141#43881141. That’ll also work for a test for an `http` resource. – sideshowbarker Oct 24 '17 at 22:27
  • @Scriptonomy I've added an example which works in Firefox but not in Chrome – ilija veselica Oct 25 '17 at 08:39
  • @sideshowbarker `https://cors-anywhere.herokuapp.com/` is sometimes down so it's not reliable – ilija veselica Oct 25 '17 at 09:15
  • @ilijaveselica it’s a pretty trivial to set up a similar CORS proxy instance of your own — using the code from https://github.com/Rob--W/cors-anywhere/ or such, or else just rolling your own in whatever your preferred server-side programming runtime is – sideshowbarker Oct 25 '17 at 09:59
  • that is a potential workaround but it sounds like a very clumsy solution to have another server running just for that purpose – ilija veselica Oct 25 '17 at 11:02

0 Answers0