Log in

View Full Version : such a code available?



joycie
08-24-2006, 03:09 PM
I would like my index page to automatically go to another page after a specified amount of time but the URL remain the same as index.htm. May I know is there such a code available?

mburt
08-24-2006, 03:11 PM
The first part could easily be done, but this?

but the URL remain the same as index.htm

I don't really understand why you would want to do that.

joycie
08-24-2006, 03:19 PM
To prevent the easy access of the source code.:D

mburt
08-24-2006, 03:23 PM
Well, that can't be done, sorry to say. The url is there whether you like it or not. Hiding it doesn't change the fact that it is there, and most anyone could find it. Using frames is the closest thing to this, but HTML 4.01 Strict (most popular validator) doesn't allow it.

mburt
08-24-2006, 03:26 PM
As for the timer, check this out:


<script type="text/css">
var i = 0
//couting variable
var sec = 10
//number of seconds to wait
var target = "http://www.yourpage.com/"
//the page that will be loaded
onload=function timer() {
i=i+1
if (i>sec) {window.location.href=target}
setTimeout("timer()",1000)
}
</script>

joycie
08-24-2006, 03:31 PM
Many thanks for the code, mburt.

Twey
08-24-2006, 03:47 PM
This should be done with a meta refresh:
<meta http-equiv="refresh" content="10;url=http://www.yourpage.com/">Perhaps with some Javascript as backup:
<script type="text/javascript">
window.onload = function() {
setTimeout(function() {
window.location.href = "http://www.yourpage.com/";
}, 10 * 1000);
};And always with a normal link just in case:
Sorry, you should have been redirected <a href="http://www.yourpage.com/">here</a>

joycie
08-24-2006, 03:55 PM
Many thanks to Twey for your kind input.

Twey
08-24-2006, 04:12 PM
mburt: Does that code even work? Functions declared with function identifier(arguments) { shouldn't return a value.

mwinter
08-24-2006, 07:04 PM
mburt: Does that code even work?

It will work in many browsers. It does violate the specification, though.



Functions declared with function identifier(arguments) { shouldn't return a value.

I don't think you meant to phrase that statement the way you did. The problem with something like:



var funcRef = function funcName() {
setTimeout('funcName();');
};

is that setTimeout evaluates strings in global scope. That is, all identifiers that appear in the string must be available through properties of the global object. The identifier in function expressions (funcName, above) should not be exposed by the interpreter outside the function body.



var funcRef = function funcName() {
alert(typeof funcName);
};

alert(typeof funcName);

should display 'undefined', whereas calling the function [funcRef()] would display 'function'.

If one were to follow mburt's suggestion (there's no need though; Twey's is fine), the function (and assignment) should be rewritten:



function timer() {
if (++i > sec) window.location.href = target;
setTimeout('timer();', 1000);
}
this.onload = timer;

Mike

Twey
08-24-2006, 07:45 PM
Functions declared with function identifier(arguments) { shouldn't return a value.I don't think you meant to phrase that statement the way you did.No, I didn't. Let me try again:

The function() syntax should return a pointer to the function it creates, but the function identifier() syntax shouldn't.

Good (relatively):
alert(
(window.myFunc = function() {
return "A string";
})()
);Bad (shouldn't work):
alert(
(function myFunc() {
return "A string";
})()
);

mwinter
08-24-2006, 08:57 PM
The function() syntax should return a pointer to the function it creates,

Evaluate to a Function object reference, would be a more accurate way of stating that. Expressions don't return anything; functions do. There are no pointers, but there are references.



but the function identifier() syntax shouldn't.

When it is part of an expression, it should. Remember that there are three forms (excluding the Function constructor function) of token sequence that can create a function object, two of which feature an identifier after the "function" token. One of those is a function declaration (a statement), the other is one of two forms of function expression. Both function expressions evaluate to a Function object reference, and neither creates a property in the enclosing scope. A function declaration does create a property in the enclosing scope, but as a statement, it cannot evaluate to anything by definition.

Mike