View Full Version : Javascript onclick link concatination
Trashed20
02-06-2003, 22:14
hehe, as if the title wan't complicated enought. Ok, i know you can do javascript actions via the onClick command. Is it possible to have the hyperlink (ex http://lightningrobotics.com/?do=news) dynamically change to the same url plus a "&time=(timestamp)" and then transport you to that page. the timestamp would have to be made via php in the javascript at the execution of the javascript. is it possible?
jonathan lall
02-06-2003, 22:33
If I follow correctly, yes certainly, but I wouldn't know how. :rolleyes:
Brrrrrrrrandon!!
yes.. if i get what you're saying correctly...
Here's what you'd need: (be sure the file gets parsed by php ;))
href="<?php echo "http://lightningrobotics.com/?do=news&".time(); ?>"
that should do it :)
Trashed20
02-06-2003, 23:12
Originally posted by Jack
yes.. if i get what you're saying correctly...
Here's what you'd need: (be sure the file gets parsed by php ;))
href="<?php echo "http://lightningrobotics.com/?do=news&".time(); ?>"
that should do it :)
sorta....
that would create all the links with the timestamp of initial parsing. So you could wait 5 minutes and have the timestamp be 5 minutes old. I need the timestamp right when you click the link. so you click the link (your mouse is still down) the timestamp is thrown in (you let go) and you go to the new url.
Brandon Martus
03-06-2003, 00:09
here we go:
(note: this is not tested, this is pseudo-code to get you going)
<!-- somewhere up top in 'head' -->
<script language="JavaScript">
<!-- //
function sendWithTimestamp(url) {
timestamp = Get.The.Time.Stamp; // <-- said pesudo-code. :)
url = url + "&time=" + timestamp;
window.location(url); // <-- this may not be right, I forget exactly.
}
-->
</script>
...
<!-- then down in the body -->
<a href="javaScript:sendWithTimestamp('http://lightningrobotics.com/?do=news');">news</a>
Hope my near-sleep jabber makes any sense & gets you going in the right direction. You may need to make a function to get the timestamp in JavaScript... my book's at work and I'm too lazy to go search for the JS Time functions. I have to leave something for you to do, eh? :)
Trashed20
03-06-2003, 06:08
that should work. I actually had a dream about this last night in which i just had the date ammended at the top of the standard page that had something like this:
if (!&GET[time]){
$timestamp=time;
put url thingie here and add time
do a http redirrect thingie to new url with timestamp
}
i think yours will work better if i can program figure out the rest. more efficient anyway. i'll work on it later tonight but right now i have to ge some skoolin for the last 2 days of my senioryear, yay...
seanwitte
03-06-2003, 09:08
This'll work. You can modify the currentTimestamp() function to fit whatever format you want to use.
script block for the header:
<SCRIPT LANGUAGE="Javascript">
<!--
// return the current timestamp as a string
function currentTimestamp()
{
var now = new Date();
var ts = new Array(
now.getFullYear().toString(),
now.getMonth().toString(),
now.getDay().toString(),
now.getHours().toString(),
now.getMinutes().toString(),
now.getSeconds().toString())
var i;
var retval = '';
//pad one-digit values to two and concatenate
//the order is yyyymmddhhnnss
for (i=0; i<ts.length; i++)
{
if (ts[i].length < 2) ts[i] = '0' + ts[i];
retval += ts[i];
}
return (retval);
}
//append the current timestamp to a url and redirect
function sendWithTimestamp(url)
{
// check url for a ?
if (url.indexOf('?') >= 0) url += '&';
else url += '?';
//build the url
url += 'time=' + escape(currentTimestamp());
document.location.href = url;
}
//-->
</SCRIPT>
Usage (the bulletin board software injects a space in "JavaScript", so you'll need to change it in the anchor tag):
<A HREF="Javascript:sendWithTimestamp('mypage.php');">MyPage</A>
An observation that may change things: when javascript returns a timestamp, is it not based on the clock of the user's computer, which may or may not be the same as the clock on the server to which the link is directing? It is entirely possible that this is your goal, but I just thought I would point that out.
Brandon Martus
03-06-2003, 10:13
True.
You may want to send a flag to the link you are going to, and let the destination page make the timestamp.
<a href="mypage.php?timestamp=1">My Page</a><?php
if ($timestamp==1) {
$time = time();
}
// continue as planned..
?>
I'd peresonally do it a different way... the link anyway...
<A
HREF="mypage.php"
onClick="
window.location = this.href + sendWithTimestamp('mypage.php');
return false;
">MyPage</A>
it makes opening in a new window/tab not break...
(spaced to be more readable. remove spaces as needed. :-D)
seanwitte
04-06-2003, 11:18
I didn't even think of that, you're completely right. If you have links targeting a frame or another window you don't want to lose that functionality. You can use a generalized function that sets a querystring value in a URL to do it and use it anywhere. Something like this:
<HTML>
<HEAD>
<SCRIPT LANGUAGE="Javascript">
<!--
// return the current timestamp as a string
function currentTimestamp()
{
var now = new Date();
var ts = new Array(
now.getFullYear().toString(),
now.getMonth().toString(),
now.getDay().toString(),
now.getHours().toString(),
now.getMinutes().toString(),
now.getSeconds().toString())
var i;
var retval = '';
//pad one-digit values to two and concatenate
//the order is yyyymmddhhnnss
for (i=0; i<ts.length; i++)
{
if (ts[i].length < 2) ts[i] = '0' + ts[i];
retval += ts[i];
}
return (retval);
}
//update a querystring value in a URL or append
//it to the set if it does not already exist.
function setQuerystringElement(url, element, value)
{
var newUrl = url;
var params = url.split('?');
var param;
var i;
var found = false;
//params will have either one or two entries, where
//entry 0 if the base URL and entry 1 is the querystring
if (params.length == 1)
{
//url has no querystring, so just append element
return newUrl += '?' + element + '=' + escape(value);
}
else
{
//save the base url from up above
newUrl = params[0];
//divide the querystring into name/value pairs
params = params[1].split('&');
for (i=0; i<params.length; i++)
{
//divide each pair into a name and value
param = params[i].split('=');
if (param[0].toLowerCase() == element.toLowerCase())
{
//update the value of the element
param[1] = value;
found = true;
}
//append the parameter to the URL
newUrl += (i == 0) ? '?' : '&';
newUrl += param[0] + '=' + escape(param[1]);
}
//if the element was not already in the URL, add it
if (!found) newUrl += '&' + element + '=' + escape(value);
}
return (newUrl);
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<A HREF="temp.html" target="_blank" onclick="JavaScript:this.href=setQuerystringElement(this.hr ef, 'time', currentTimestamp()); return true;">Link</A>
</BODY>
</HTML>
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.