代码我想从"php"Javascript"“帮助”


codes i want to change from "php" to "Javascript"

这是PHP代码

<?php
if (file_get_contents('http://www.anysite.com/anypath/anyfile.php')=="anydata") {
echo "OK";}
ELSE {
ECHO "Not OK";}
?>

我想改变这从"php"到"javascript"我试图这样做,但没有结果,因为"file_get_contents"函数,请帮忙吧?!

使用jquery可以使用这个函数

function urlExists(url, callback){
  $.ajax({
    type: 'HEAD',
    url: url,
    success: function(){
      callback(true);
    },
    error: function() {
      callback(false);
    }
  });
}

,在js中使用:

var exists = urlExists(url);
if (exists) { 
   // do your staff here 
}

FILE_GET_CONTENTS在JS中并没有真正的类比,因为它是一种客户端脚本语言。您可以使用location.href='http://www.anysite.com/anypath/anyfile.php';强制一个下载文件对话框,但这可能不是你想要完成的。为什么要用JavaScript呢?