Like to load https version of a page if its available. My code is 'blocked by CORS policy'. Seems stupid that a secure connection should be blocked. Can I fix this with javascript only without having to add further serverside code? (and without using jquery).
// check if ssl version of the page is available
if (window.location.protocol=="http:") {
newurl = "https:"+window.location.href.substr(5);
// get an ajax object
if (window.XMLHttpRequest) req=new XMLHttpRequest();
else if (window.ActiveXObject) req=new ActiveXObject("Microsoft.XMLHTTP");
else req=false;
// run ajax
if (req) {
// setup return function to load page if it was successful
req.onreadystatechange=function() {
if (req.readyState!=4) return;
if (req.status==200) location.href=newurl;
}
// attempt to open url
req.open("GET",newurl,true);
req.send("");
}
}