PDA

View Full Version : check file exists before load


pinktako
10-27-2008, 08:09 AM
I found this JavaScript code on another forum that will check if file exists before it is loaded. I am using ExternalInterface.call to call this JavaScript function.

<script language="JavaScript">
function fileExists(file_name) {
var answer = <?php return file_exists(file_name); ?>;
return answer;
}
</script>

However, this JavaScript only works in FireFox, IE and Safari both errors out. Does anyone see what the problem is?

jasonJ
10-27-2008, 05:34 PM
The code doesn't even work in FF for me. What is "return file_exists(file_name)" supposed to do? Why "return" statement? As far as I know, PHP code is generated on the server. JavaScript functions, on the other hand, run from the browser (and thus PHP code is generated first). So when you open your web page first time, and call the fileExists from Flash without reloading the web page, the file_name variable will remain the same. But that's just a hypothetical since the code doesn't run in the first place.

pinktako
10-28-2008, 05:58 AM
You are right jasonJ, the code did not work. IE and Safari wouldn't even load the page with the erroreous javascript. That's why I thought it was working in FF. I updated the code to use IOErrorEvent on load instead to check if the file exists. Thanks.