PDA

View Full Version : Insert Javascript variable into Meta tags?


Crlaozwyn
04-09-2007, 01:35 PM
I'm trying to create a page that refreshes a frame at a random interval - I've used Javascript to come up with that random interval (minimum of 5, max of 10), but the page refreshes using a meta tag in the head. I'm obviously not getting any results - help anyone? Following is the code:

<html>

<head>
<script language="JavaScript">
var now = new Date();
var secs = now.getSeconds();
var raw_random_number = Math.random(secs);
var random_number = Math.round(raw_random_number * (5)+5);

<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta http-equiv="refresh" content="5">
</script>
<title>New Page 1</title>
</head>
<FRAMESET COLS="*,1">
<FRAME SRC="http://htmlcodetutorial.com">
<FRAME>
</FRAMESET>
</HTML>

Jon Hanlon
04-09-2007, 06:59 PM
1) You should put the refresh in the frames themselves, not the frameset page.
2) You can't really dive into Meta tags programatically.


<script language="JavaScript">

function makeRand() {
var now = new Date();
var secs = now.getSeconds();
var raw_random_number = Math.random(secs);
var random_number = Math.round(raw_random_number * (5)+5);
return random_number;
}
</script>

<body onload="setTimeout('location.reload(true)',makeRand())">

Jon Hanlon
04-09-2007, 07:14 PM
Actually, you might try:
<FRAME SRC="http://htmlcodetutorial.com" onload="setTimeout('self.location.reload(true)',makeRand())">

For each frame. It might work.

Crlaozwyn
04-12-2007, 10:15 PM
Haven't had much luck toying with this, but I appreciate the help. Thank you.

ASMBlah
04-16-2007, 12:59 PM
Hi,

Both of Jon's solutions are correct, here is the full code for one of them, with reporting commands included. It's up to you which one to use.


<html>

<head>
<script language="JavaScript">

window.onload = funcInit;

function funcInit()
{
var now = new Date();
var secs = now.getSeconds();
var raw_random_number = Math.random(secs);
var random_number = Math.round(raw_random_number * (5)+5);

window.setTimeout("funcTimeoutCallback(" + random_number + ");", random_number);
}

//callback function for timeout
function funcTimeoutCallback(intInterval)
{
alert("Reloaded frame after " + intInterval + " seconds");
//the command [document.frames["fmMain"].location.reload()] could have been used instead here,
//... => but because the frame's src is out of this document's hostname, this method must be used
document.frames["fmMain"].location.href = "#";
}
</script>

<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<title>New Page 1</title>
</head>
<frameset cols="*,1">
<frame src="http://htmlcodetutorial.com" name="fmMain">
<frame>
</FRAMESET>
</html>


Dan

johnz
04-17-2007, 01:15 AM
You can use PHP to achieve the same thing while still using the meta refresh.


<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<?php $num = rand(5, 10); ?>
<meta http-equiv="refresh" content="<?php echo $num; ?>">
<title>New Page 1</title>
</head>
<FRAMESET COLS="*,1">
<FRAME SRC="http://htmlcodetutorial.com">
<FRAME>
</FRAMESET>
</HTML>


PHP uses the rand() function to generate a random number between 5 and 10.
It then stores it to the variable $num.
You then can display the generated number in the meta tag by using echo.