Changing page with AJAX using JS
I have the following problem. I want to move the user to another page when a certian condition is met. For example, I have a text input field where a user enters a URL. I want to move the user to this url, without him pressing any kind of submit form. I'm using AJAX to achive this. I have a function that checks for the pattern the user entered, if it is found to be a valid url, the page will change to this url. and if it's not a valid url, it will write error.
I have a <div id='url'></div>
that the AJAX updates with error message (on 1st load, before user entered anything) and this message continues until it a valid url was entered. when a valid url was entered, the following will be written to this DIV:
<script type="text/javascript">
window.location.href="http://google.com"; // google.com is just an example
</script>
this doesn't seem to work. but if I put the page change code instead of the error message, it is working.
Why doesn't it work after a valid URL was entered?
I don't see any need for AJAX, just normal javascript. When you find that the entered text is a valid website and you want to load it, just use window.location = "http://google.com"; // google.com is just an example
directly - no need to insert it into the document.
Why do you write JS to DIV instead of just calling window.location.href="http://google.com"; ?
For example
$.ajax({
url: 'answercheck.php',
success: function( data ) {
if (data=="correct"){
window.location.href="http://google.com";
}
else
{
//show error message
}
}
});
精彩评论