/    Sign up×
Community /Pin to ProfileBookmark

client-side – check if server response is redirect

I have a form where someone enters a URL, I wish to find out if a website that is entered is the main website and not a site that the entered URL is redirected to.

say someone enters [url]http://www.mysite.com[/url]
but this site is the old URL and I have a new site where the old site is redirect to being… [url]http://www.mynewsite.com[/url] this URL is now in the address bar.

I wish to get the server response and get the new site that they are redirected to.

How would I do this so I can place an message on the screen that the url they entered is being redirected and to enter the correct url being the site site?

I have been playing around with code for a bit and come up with this…

[code]
var Response = ”;
function loadXMLDoc(url) {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest(); // code for IE7+, Firefox, Chrome, Opera, Safari
} else {
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”); // code for IE6, IE5
}
//xmlhttp.onreadystatechange=function() {
//if (xmlhttp.readyState==4 && xmlhttp.status==200) {
//Response = xmlhttp.responseText;
Response = xmlhttp.status;
//}
//}
xmlhttp.open(“GET”,url,true);
xmlhttp.send();
}
function update() {
document.getElementById(‘q’).innerHTML=Response;
alert(‘.’ + Response + ‘.’);
}
[/code]

[code]
<br><br>URL: must include the http://<br><input type=”text” style=”width: 250px;” name=”aUrl” value=”<?php echo ((isset($aUrl) && $aUrl != “”) ? $aUrl : “”); ?>” onKeyUp=”loadXMLDoc(this.value); update();” />
<span id=”q”></span>
[/code]

All I get is ‘0’ how can I get the server response like 200 or 404 and the like ?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinApr 13.2016 — <!doctype html>

<head>

<title>Title of the document</title>

</head>

<body>

<button onclick=loadXMLDoc('http://www.msn.com')>getStatus of msn.com</button>

<div id=dsply></div>

<script>

var Response = '';

function loadXMLDoc(url) {

var xmlhttp;

xmlhttp=new XMLHttpRequest(); // code for all modern browsers

xmlhttp.onreadystatechange=function() {

if (xmlhttp.readyState==4 && xmlhttp.status==200) {

//Response = xmlhttp.responseText;

//Response = xmlhttp.status;

update(xmlhttp.status)

}

}

xmlhttp.open("GET",url,true);

xmlhttp.send();

}

function update(Response) {

dsply.innerHTML=Response;

alert('.' + Response + '.');

}

</script>

</body>

</html>
Copy linkTweet thisAlerts:
@jasonc310771authorApr 13.2016 — ah, thats it...

Thank you.

Just one thing, how do I get the code to say that the page was redirected to another URL ? and what that URL is.

<!doctype html>

<head>

<title>Title of the document</title>

</head>

<body>

<button onclick=loadXMLDoc('http://www.msn.com')>getStatus of msn.com</button>

<div id=dsply></div>

<script>

var Response = '';

function loadXMLDoc(url) {

var xmlhttp;

xmlhttp=new XMLHttpRequest(); // code for all modern browsers

xmlhttp.onreadystatechange=function() {

if (xmlhttp.readyState==4 && xmlhttp.status==200) {

//Response = xmlhttp.responseText;

//Response = xmlhttp.status;

update(xmlhttp.status)

}

}

xmlhttp.open("GET",url,true);

xmlhttp.send();

}

function update(Response) {

dsply.innerHTML=Response;

alert('.' + Response + '.');

}

</script>

</body>

</html>[/QUOTE]
×

Success!

Help @jasonc310771 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...