validating a webpage with javascript

I need to find out if a page is returning a 404 or 301 status code. I know I can write a php script to use cURL to return a value for javascript to read, but I am trying to simplify the process and kinda new. Right now I have an `onBlur` function that makes sure the webpage is at least in the correct format before they leave the field. But I would like it to also check the status of the page and I can't seem to find a solution for using cUrl directly with javascript or any examples of how this would be done. Anybody care to help me out please? Here is my validate.js that I am calling on the page... function loadXMLDoc() { var url = document.getElementById("webpage_url").value; var xmlhttp; if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("valid").innerHTML = xmlhttp.responseText; } }"GET", "custom/modules/CT221_SEP_test/js/curltest.php/" + url, true); xmlhttp.send(); } function isURL() { var url = document.getElementById("webpage_url"); var urlerr = document.getElementById("url_error"); var reg = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/; if (!reg.test(url.value)) { urlerr.innerHTML = "Invalid WebPage Address"; url.focus(); } else { loadXMLDoc(); urlerr.innerHTML = //calls success image"; } } YAHOO.util.Event.on("webpage_url", "blur", isURL); then my curltest.php file looks like this... $url = $_GET['url']; function validateurl($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($ch); curl_close($ch); preg_match("/HTTP\/1\.[1|0]\s(\d{3})/", $data, $matches); return $matches[1]; } $code = validateurl($url); if ($code != "404") { echo "Webpage is valid"; } else[ echo "Webpage is not live"; }
possible duplicate of cUrl Converting into Javascript possible?

以上就是validating a webpage with javascript的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准