javascript - Checking if a URL exists or not in Client Side Code -


my objective check whether url valid or not client side. tried following things: 1. tried using ajax request using datatype json. - got cross-origin request blocked error. 2. tried using jsonp datatype. - worked fine websites google.com cribed others facebook.com

got error "refused execute script facebook

callback=jquery32107833494968122849_1505110738710&_=1505110738711'  

because mime type

('text/html') not executable, , strict mime type checking enabled."

is there workaround this. want make sure url valid irrespective of content in response.

following code wrote:

<html>   <body>      <script        src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">     </script> <script>     function callpagemethod() {             $.ajax({                 type: "get",                 url: "https://www.google.com/",                 datatype: "jsonp",                 success: function (data, textstatus, xhr) {                     alert("success");                 },                 error: function (data, textstatus, xhr) {                     if (data.status === 200) {                         alert("finally done")                     } else {                         alert("error");                     }                 },             });     } </script> <button onclick="callpagemethod()">test url</button> </body>  </html> 

any suggestions or alternative approach should follow resolve issue?

not properly, sites have favicon.ico either site directly or provided hosting company site if 404 image.

<img src="https://www.google.com/favicon.ico"      onload="alert('icon loaded')"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"      onload="alert('ajax loaded')"></script> 

although iframe , object have onload events, invalid pages trigger event.

this fastest site test can think of ...

var img = new image(); img.onload = function () {    alert("image width " + img.naturalwidth + " not 0 site valid"); } img.src = "https://www.google.com/favicon.ico"; 

as facebook, each page uses resources url, iframes blocked scripts. need make request server test if page existed.


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -