PDA

View Full Version : Newbie ... got me stumped !!



odintheterrible
Jan 15th, 2010, 12:40 AM
Greetings,

Perhaps someone with the knowledge could assist me with this problem ...

Basically, I need to put some code together to automatically redirect a page back to the referring page after a short delay ... and frankly I'm banging my head against a brick wall LOL

OK ... got this in HTML to auto redirect to a specific web page after a defined delay :


<meta HTTP-EQUIV="REFRESH" content="10; url=http://www.****.co.uk/index.htm">


... and I have this javascript for redirecting back to the referring page using a button :


<form method="POST">
input type="button" name="B1" value="Try it now" onclick="history.back();">
</form>


So the question is how to combine the two so that I can get the page to return to the referring page after a delay automatically (that is, without having to press a button) ????

Any assistance would be really appreciated. Many thanks.

O T T

Dean440
Jan 15th, 2010, 01:34 AM
Javascript has a "pseudo protocol" that works kind of like using "http://"


<meta http-equiv="Refresh" content="10;url=javascript:history.back()" />

By using the "javascript:" protocol, you can specify JavaScript code directly in your URL.

odintheterrible
Jan 15th, 2010, 11:54 AM
Javascript has a "pseudo protocol" that works kind of like using "http://"


<meta http-equiv="Refresh" content="10;url=javascript:history.back()" />

By using the "javascript:" protocol, you can specify JavaScript code directly in your URL.

Many thanks ... doesn't seem to work for now, but I'll have a fiddle and see if I can get it to function.

Thanks again

Philip M
Jan 15th, 2010, 12:09 PM
I would advise avoiding using META refresh as by default IE has it disabled under the security settings. In any case, using META refresh is considered a poor implementation of redirection against using .htaccess or scripting. The tag is deprecated and ought to be avoided.

In IE7 with META REFRESH enabled, the page refresh works successfully when a HTTP: style URL is defined, but not a javascript: style URL as suggested by Dean440.
<meta http-equiv="Refresh" content="3;url=http://mytest.local"> -- Works fine
<meta http-equiv="Refresh" content="3;url=javascript:refreshPage();"> -- Doesn't work

You can use the following Javascript (although naturally the user must have Javascript enabled).


<script type = "text/javascript">
var timer = null;
function redirect() {
window.location = 'http://www.yourdomain.com/index.htm'
}
</script>

<body onload="timer=setTimeout('redirect()',10000)"> // 10 seconds

Refreshing/redirecting the current page without any explanation can confuse people. If they didn't request the reload, some people can get concerned about security. In short, think through what it is you are trying to do.

odintheterrible
Jan 15th, 2010, 12:16 PM
I would advise avoiding using META refresh as by default IE has it disabled under the security settings. In any case, using META refresh is a very nasty tag to use and is considered a poor implementation of redirection against using .htaccess or scripting.



<script type = "text/javascript">
var timer = null;
function redirect() {
window.location = 'http://www.yourdomain.com/index.htm'
}
</script>

<body onload="timer=setTimeout('redirect()',10000)"> // 10 seconds

Kewl ... MANY THANKS !!

odintheterrible
Jan 15th, 2010, 12:20 PM
I would advise avoiding using META refresh as by default IE has it disabled under the security settings. In any case, using META refresh is a very nasty tag to use and is considered a poor implementation of redirection against using .htaccess or scripting.



<script type = "text/javascript">
var timer = null;
function redirect() {
window.location = 'http://www.yourdomain.com/index.htm'
}
</script>

<body onload="timer=setTimeout('redirect()',10000)"> // 10 seconds

Hi .. just had another look at this and to my uninformed eye it appears that this code will redirect back to the site's home (index) page ... or am I wrong ?

If that is correct, then it really doesn't do what I was looking for as I want to redirect back to the referring page (which ever that may be) rather than to direct back to the index page.

Anyway ... thanks for the input

Dean440
Jan 15th, 2010, 03:08 PM
Hi .. just had another look at this and to my uninformed eye it appears that this code will redirect back to the site's home (index) page ... or am I wrong ?

If that is correct, then it really doesn't do what I was looking for as I want to redirect back to the referring page (which ever that may be) rather than to direct back to the index page.

Anyway ... thanks for the input

<body onload="timer=setTimeout('history.back()',10000);"> // 10 seconds

Philip M
Jan 15th, 2010, 03:39 PM
If that is correct, then it really doesn't do what I was looking for as I want to redirect back to the referring page (which ever that may be) rather than to direct back to the index page.




Why do you want to do that?

odintheterrible
Jan 15th, 2010, 11:14 PM
Why do you want to do that?

The referring page is a form with certain required fields. If the form is submitted with any of the required fields left blank, then it seems reasonable to redirect the sender back to the page with the form on it, to have the chance to fill in the missing required information and then the opportunity to resubmit the page.

I hope that clears things a little ??

Dean440
Jan 16th, 2010, 02:34 AM
The referring page is a form with certain required fields. If the form is submitted with any of the required fields left blank, then it seems reasonable to redirect the sender back to the page with the form on it, to have the chance to fill in the missing required information and then the opportunity to resubmit the page.

I hope that clears things a little ??

The proper solution is to prevent the page from even submitting the form if the form fields are invalid. You do this using the onsubmit handler in the form tag, and calling a function. The function you call should return true or false: true if everything is okay, and false if any fields are left blank.

If the function returns false, the form won't submit. This will save your server some hits.




function checkMyForm(formName){
if(formName.firstName.value == "Ted"){
alert("Sorry, Ted. Can't let you through.");
return false;
}
return true;
}
.....
<form action="lol.php" onsubmit="return checkMyForm(this)">
<input type="text" name="firstName" />
....blah blah
</form>

Philip M
Jan 16th, 2010, 08:24 AM
The proper solution is to prevent the page from even submitting the form if the form fields are invalid. You do this using the onsubmit handler in the form tag, and calling a function. The function you call should return true or false: true if everything is okay, and false if any fields are left blank.

If the function returns false, the form won't submit. This will save your server some hits.




function checkMyForm(formName){
if(formName.firstName.value == "Ted"){
alert("Sorry, Ted. Can't let you through.");
return false;
}
return true;
}
.....
<form action="lol.php" onsubmit="return checkMyForm(this)">
<input type="text" name="firstName" />
....blah blah
</form>


Dean440 is absolutely correct, and it should not be possible to submit an incomplete/invalid form. But bear in mind that form validation needs to be quite rigourous.

if(formName.firstName.value == "Ted"){

will only reject "Ted" and not "ted", " ted", "ted ", "teddy" and so on.

Form validation has been covered a zillion times in this forum. Remember that validation of the kind if(formName.firstName.value == ""){ is barely worthy of the name, and virtually useless, as even a single space or a ? will return false, that is pass the validation.

odintheterrible
Jan 16th, 2010, 09:32 AM
Dean440 is absolutely correct, and it should not be possible to submit an incomplete/invalid form. But bear in mind that form validation needs to be quite rigourous.

if(formName.firstName.value == "Ted"){

will only reject "Ted" and not "ted", " ted", "ted ", "teddy" and so on.

Form validation has been covered a zillion times in this forum. Remember that validation of the kind if(formName.firstName.value == ""){ is barely worthy of the name, and virtually useless, as even a single space or a ? will return false, that is pass the validation.

Thanks for the comment ... however this seems to be moving off topic and I am well aware of the issues surrounding form validation and that was NOT the basis for the original inquiry.

Whether or not you agree with the requirement to have the return directed to the referring page or not is, with due regard, not relevant. That is the requirement and it was that which I was having trouble with.

I again thank you all for your input, but the requirement is clear AND is well thought out and conceived along with the field requirements for validation. The validation requirements and processes are NOT the issue.

The issue is the requirement to return to the referring page.

Old Pedant
Jan 17th, 2010, 01:48 AM
Well, one easy way would be to put the URL of the referring page into a hidden <FORM> field to that you can then pick it up on the referred-to page. Or append it to the ACTION= url, of course. But if you are sending/validating a <form> anyway, I'd think the hidden form field would work just fine.

You could do that either via document.write as you create the <form> or by plunking the value into the field at, say, submit time.



<form ... onsubmit="this.pageFrom.value=location.href; return otherStuffMaybe();">
<input type="hidden" name="pageFrom" />
...


That seems simple enough, to me. Granted, it means that JS has to be enabled for that page, but then JS has to be enabled on the referred-to page in order to make the jump back here, so that doesn't seem onerous.