I'm looking for some code which can check whether 30 days have passed, and if so the script will stop working; if not, it calls the start()
function.
I only need to do it in Javascript.
var date = 11;
var month = 10;
var year = 2012;
function Checkday() {
if ( // check 30 days ) {
start();
} else {
stop();
};
};
I'm also wondering whether there is a way to add PHP code in a JS file. I know this is a stupid question but I'm just curious.
Thanks in advance.
Edited:
We will be selling the JS file so we will be setting the date in the JS file. So we want it to check the expiry time and stop functioning.
I'm looking for some code which can check whether 30 days have passed, and if so the script will stop working; if not, it calls the start()
function.
I only need to do it in Javascript.
var date = 11;
var month = 10;
var year = 2012;
function Checkday() {
if ( // check 30 days ) {
start();
} else {
stop();
};
};
I'm also wondering whether there is a way to add PHP code in a JS file. I know this is a stupid question but I'm just curious.
Thanks in advance.
Edited:
We will be selling the JS file so we will be setting the date in the JS file. So we want it to check the expiry time and stop functioning.
Share Improve this question edited Oct 12, 2012 at 12:53 Layke 53.3k11 gold badges87 silver badges111 bronze badges asked Oct 12, 2012 at 4:07 deeroxdeerox 1,0553 gold badges14 silver badges28 bronze badges 10- developer.mozilla/en-US/docs/JavaScript/Reference/… – Marc B Commented Oct 12, 2012 at 4:08
- @MarcB. Expire javascript, Ohh, people have imagination now days... – gdoron Commented Oct 12, 2012 at 4:16
- 2 @gdoron: they'll probably sell one copy... – Marc B Commented Oct 12, 2012 at 4:18
- duplicate: stackoverflow./questions/1210701/… – yodog Commented Oct 12, 2012 at 4:18
- 1 @MarcB. Doesn't know js but want to sell js. Funny how things work... – gdoron Commented Oct 12, 2012 at 4:30
3 Answers
Reset to default 4I've made a fiddle for you.
var date = 10;
var month = 10;
var year = 2012;
var expires = new Date(year, month, date);
function checkDate() {
var d = new Date();
return(d>expires);
}
Give your js file the ending .php and put header('content-type: application/x-javascript');
at the beginning of it. Now you can <?php echo $variables; ?>
inside of it. This workouround must not work in ie*.
Nevertheless: if you're selling the js and want to stop this function when a given timepoint is reached: This is not solvable via javascript! Anybody could edit a javascript file - like the date, month, year params. Except you hava a js packer, which - IMHO - is not safe enough.
This might help you..
var date = 11;
var month = 10;
var year = 2012
function Checkday() {
if ( !isExpired() ) {
start();
} else {
stop();
};
};
function isExpired() {
var current = new Date();
current = current.getTime();
var dateToCheck = new Date();
dateToCheck.setFullYear(year);
dateToCheck.setMonth(month);
dateToCheck.setDate(date + 30)
var checkdate = dateToCheck.getTime();
return checkdate < current
}
Silly question, but what is to stop the people you are selling this JavaScript file to from opening it up themselves and changing the date? Are you selling this file to them so they can use it in their website? I have to imagine that they will be smart enough to think of that.
Also, php is a server-side language while JavaScript is a client-side language, so putting php in the JS file directly wouldn't do you much good. Really your best bet is to save the dateinfo in a cookie and check it before calling the function.