I am terrible with Javascript but learning from my bruises.
On the site I am working on - I currently have a page setup for everyday of the year (i.e. January 1st has its own page '01-jan-01.html' January 2nd is '01-jan-02.html') - I want to make a button that will look up the current date then send the user to the corresponding page (Think 'This day in history').
I already have a button that will pick one of these pages at random and the code works see below:
<script type="text/javascript">
var urls = [
'01-jan-01.html'
'01-jan-02.html'
....
];
function goSomewhere() {
var url = urls[Math.floor(Math.random()*urls.length)];
window.location = url; // redirect
}
Then on my button I simply call goSomewhere() on the onClick() and as i said earlier, this works fine. However I can't figure out how to select based on the CURRENT DATE. any help would be greatly appreciated. thank you!
EDIT: No answers but here is the progress so far:
<script>
var day = date.getDate();
var month = date.getMonth();
function goSomewhere() {
var url = month + "-" + day];
window.location = url; // redirect
}
</script>
This wouldn't even solve my problem entirely but its how I'm trying to wrap my head around the logic. In the above code I would still need to find a way to incorporate the 'jan' (month) varible in each webpage link since the formatting is 'MM-mmmmm-DD' - would it be easier if I change the naming mechanic to simply "MM-DD"? I'd prefer not to since it would involve a lot of manual work. Thanks
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
var yy = yyyy.toString().substr(2,2); //get two digit year
if(dd<10){ dd='0'+dd }
if(mm<10){ mm='0'+mm }
location.href = yy+'-'+mm+'-'+dd+'.html';
Related
Okay the questions are. I need help on making it so when I click the button it runs the free_cash variable in the function which I have set up. But I want it to add the givin amount to the total money the person has.
I also want to know how I can make it so when you click the button to get your free daily cash it only allows you to do it once a day and if you click it more the message switches to Something like you already been givin a handout today.
<!DOCTYPE html>
<html>
<head>
<title>
Character
</title>
<style>
</style>
</head>
<body>
<p style="margin-right: 20px; margin-left: 20px; margin-top: 5px; color: darkblue; font-size: 35px;">Money:</p>
<button onclick="FreeCash()">Free Daily Cash</button><br>
<p id="freecash"></p>
<script>
function FreeCash(){
var free_cash = "Someone handed you \"$100\" You must be a lucky man";
var nofree_cash = "Sorry you already got your handout for the day. Come back tomorrow ight.";
document.getElementById("freecash").innerHTML = free_cash;
}
</script>
<br><br><br><br>
<noscript>
<h3>This site requires JavaScript</h3>
</noscript>
</body>
</html>
You should do something like this:
function FreeCash() {
//Checks whether lastDate has been set.
if (!!localStorage.getItem("lastDate")) {
//If so and a day did not pass since then, then end the function
if ((new Date() - new Date(localStorage.getItem("lastDate"))) / (1000 * 3600 * 24) < 1) {
return;
}
}
//Set last date to current moment. You can set it to 00:00:00 of the day as well, if that fulfills your requirements better.
localStorage.setItem("lastDate", new Date());
var free_cash = "Someone handed you \"$100\" You must be a lucky man";
var nofree_cash = "Sorry you already got your handout for the day. Come back tomorrow ight.";
document.getElementById("freecash").innerHTML = free_cash;
}
This should solve the problem on client-side even if the user navigates off the page, however, you need a validation at server-side as well, because I could modify your client-side functions in my browser console whenever I want to do so.
You can use a variable to check if the button was clicked :
var clicked = false;
function FreeCash(){
var free_cash = "Someone handed you \"$100\" You must be a lucky man";
var nofree_cash = "Sorry you already got your handout for the day. Come back tomorrow ight.";
if(!clicked) {
document.getElementById("freecash").innerHTML = free_cash;
clicked = true;
} else {
document.getElementById("freecash").innerHTML = nofree_cash;
}
}
You can see the DEMO here.
EDIT:
And to make it available once a day you could use a setInterval() to reset the value of clicked to false after a day.
setInterval(function(){
clicked = false;
}, 1000*60*60*24); // after a day
It's a dirty way but it should do the trick.
You can see the updated fiddle wich executes every 10 seconds.
try this
var givenDateTime = new Date();
var isGiven = false;
function FreeCash(){
var cuurDateTime = new Date();
var message = '';
var free_cash = "Someone handed you \"$100\" You must be a lucky man";
var nofree_cash = "Sorry you already got your handout for the day. Come back tomorrow ight.";
alert(isGiven);
alert((cuurDateTime.getDate() - 1) < givenDateTime);
if(isGiven && (cuurDateTime.getDate() - 1) < givenDateTime){
message=nofree_cash;
document.getElementById("freecash").innerHTML = message;
}else{
message=free_cash;
isGiven = true;
givenDateTime = new Date();
document.getElementById("freecash").innerHTML = message;
}
}
You cannot do this with only javascript.... you need a database to store the time when your user clicked, tough a web side language (PHP, Java EE....ect).
{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")}
var c = new sforce.SObject("service_request__c");
c.id = "{!Service_Request__c.Id}";
c.status__c = "sent to customer";
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
today =yyyy+'-'+mm+'-'+ dd;
c.sentdate__c=today;
var a="{!Service_Request__c.Nicomatic_request_Id__c}".replace(",","");
result = sforce.connection.update([c]);
location.reload();
window.open("http://www.nicomatic.com/_1new_website/index.php?action=ProcessReq3&idd="+a)
I want to trigger click on the url http://www.nicomatic.com/_1new_website/index.php?action=ProcessReq3&idd="+a .I dont want to open in new window .I am working salesforce javascript button we can use only javascript. help me how can io trigger click on it url its should open a new window/tab.
they are two url are there one is my current page url and other is "http://www.nicomatic.com/_1new_website/index.php?action=ProcessReq3&idd="+a" i want to click this url and stay reload on my first page.i just want the click action is done on that url .i dont want to open that link in same window or other window.
I am currently trying to fill a html table with several counters, one underneath the other, to show days past since an incident.
Thanks to the internet, i ended up with this script:
<script language="JavaScript1.2" type="text/javascript">function
setcountup(theyear,themonth,theday){
yr=theyear;mo=themonth;da=theday
}
//////////CONFIGURE THE countup SCRIPT HERE//////////////////
//STEP 1: Configure the date to count up from, in the format year, month, day:
//This date should be less than today
setcountup(2012,9,19)
//STEP 2: Configure text to be attached to count up
var displaymessage=""
//STEP 3: Configure the below 5 variables to set the width, height, background color,
and text style of the countup area
var countupwidth='90%'
var countupheight='40px' //applicable only in NS4
var countupbgcolor=''
var opentags='<font face="Verdana"><large>'
var closetags='</large></font>'
//////////DO NOT EDIT PASS THIS LINE//////////////////
var montharray=new
Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
var crosscount=''
function start_countup(){
if (document.layers)
document.countupnsmain.visibility="show"
else if (document.all||document.getElementById)
crosscount=document.getElementById&&!document.all?
document.getElementById("countupie") : countupie
countup()
}
if (document.all||document.getElementById)
document.write('<span id="countupie" style="width:'+countupwidth+'; background-
color:'+countupbgcolor+'"></span>')
window.onload=start_countup
function countup(){
var today=new Date()
var todayy=today.getYear()
if (todayy < 1000)
todayy+=1900
var todaym=today.getMonth()
var todayd=today.getDate()
var todayh=today.getHours()
var todaymin=today.getMinutes()
var todaysec=today.getSeconds()
var todaystring=montharray[todaym]+" "+todayd+", "+todayy+"
"+todayh+":"+todaymin+":"+todaysec
paststring=montharray[mo-1]+" "+da+", "+yr
paststring="10:00"+montharray[mo-1]+" "+da+", "+yr
dd=Date.parse(todaystring)-Date.parse(paststring)
dday=Math.floor(dd/(60*60*1000*24)*1)
dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)
dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)
if (document.layers){
document.countupnsmain.document.countupnssub.document.write(opentags+dday+ " days
"+displaymessage+closetags)//to get more detail, enter one of the following in the
write line(also in the else): +dhour+" hours, "+dmin+" minutes, and "+dsec+" seconds
"
document.countupnsmain.document.countupnssub.document.close()
}
else if (document.all||document.getElementById)
crosscount.innerHTML=opentags+dday+ " days "+displaymessage+closetags//+dhour+"
hours, "+dmin+" minutes, and "+dsec+" seconds "
setTimeout("countup()",1000)
}
</script>
Now, each cell row has a counter for a different event.
I seem to be incapable of just putting this code in each , as it creates a conflict (i think)
I am completely new at this, and i have to get this sorted out.
Can anybody help me out, or point me in the right direction please?
Thank you in advance
I spent a good amount of time trying to get the code you posted to work, and I have to say, that's some really bad code. There's a lot of unneeded code like:
paststring=montharray[mo-1]+" "+da+", "+yr
paststring="10:00"+montharray[mo-1]+" "+da+", "+yr
and a lot of just general formatting craziness. I believe I'd made it work and it's significantly easier to read than it was. This fiddle should help you make yours work.
http://jsfiddle.net/9DNaD/
I'm implementing a script with Colorbox which shows a nice popup the first time a user visits a website. This script that I found is setting the cookie to last for 15 days - what I want is to manually be able to manage the "cookie-update" by my own. In my case I want to use this script to show the user that my website has been updated and using the Colorbox to show the changelog; just on the first visit. Something like this:
Update #1 -> New cookie tells there's a new update -> Show Colorbox 1 time -> Then no more, until...
Update #2 -> New cookie tells there's a new update -> Show Colorbox -> ...and so on...
Short version of the question: How do I change the script from a 15 day timer to manually be able to change it so I can decide when I want to show my Colorbox with a new changelog?
The original script is located here:
http://papermashup.com/automatic-jquery-site-subscription-lightbox/
You need to put a date in the site - Note JS months start at zero
DEMO
var latestUpdate=new Date(2012,11,10).getTime(); // 10th of December
var whatVersionSeen = parseInt($.cookie("visited"));
if (isNaN(whatVersionSeen)) whatVersionSeen = 0; // first time
if (whatVersionSeen < latestUpdate) { // whatever in the cookie < latest update
$.cookie('visited', latestUpdate, { expires: 365 });
$.colorbox({width:"580px", inline:true, href:"#subscribe_popup"});
}
the above code replaces
if (document.cookie.indexOf('visited=true') == -1) {
var fifteenDays = 1000*60*60*24*15;
var expires = new Date((new Date()).valueOf() + fifteenDays);
document.cookie = "visited=true;expires=" + expires.toUTCString();
$.colorbox({width:"580px", inline:true, href:"#subscribe_popup"});
}
completely and expects to find $.cookie
There is Jquery Code in that Script.
$("document").ready(function (){
// load the overlay
if (document.cookie.indexOf('visited=true') == -1) {
var fifteenDays = 1000*60*60*24*15;
var expires = new Date((new Date()).valueOf() + fifteenDays);
document.cookie = "visited=true;expires=" + expires.toUTCString();
$.colorbox({width:"580px", inline:true, href:"#subscribe_popup"});
}
$(".open_popup").colorbox({width:"580px", inline:true, href:"#subscribe_popup"});
});
if you want to change the days then change it here.
var fifteenDays = 1000*60*60*24*15;
I have been using this script ( http://www.dynamicdrive.com/dynamicindex6/dhtmlcount.htm ) for countdown to vacations. But this time i need to have 2 countdowns on ONE page. I tried having 2 different pages with one script in each and include them to the main page, but that did not work.
Anyone know how to edit it or have a script that works for this purpose? I tried editing some of the variables but I were unable to get it to work.
Thanks.
I have cooked up a simple countdown Constructor which may help you further.
function countDown(startTime, divid, the_event){
var tdiv = document.getElementById(divid)
,start = parseInt(startTime.getTime(),10)
,the_event = the_event || startTime.toLocaleString()
,to;
this.rewriteCounter = function(){
var now = new Date().getTime()
,diff = Math.round((start - now)/1000);
if (startTime > now)
{
tdiv.innerHTML = diff +' seconds untill ' + the_event;
}
else {clearInterval(to);}
};
this.rewriteCounter();
to = setInterval(this.rewriteCounter,1000);
}
//usage
var count1 = new countDown(new Date('2010/12/11 10:44:59')
,'counter1'
,'tomorrow 10:45');
var count2 = new countDown(new Date('2010/12/25')
,'counter2'
,'first christmas day');
Check it out #jsfiddle