I'm trying to use a bit of JavaScript to get the date and time and then use some jQuery to enable/disable a link.
So I have something like this,
<a href="link.html" title="Link">
<img src="link.gif" alt="link" />
</a>
And between Friday at 4PM and Sunday at 7:00AM, I'd like to have it just say this,
<img src="link.gif" alt="link">
How would I be able to do that?
P.S. I would prefer to have the link hardcoded rather than to have it propped with the JavaScript...
Instead of removing the anchor tags () just disable it using code like this:
with HTML like this:
here's a working version http://jsbin.com/ahitiv/9/edit#source
something like:
A javascript solution that moves the image out of the link and removes the link, when the date is within the specified range:
Put this into a jQuery-DOM-Ready-Loader. Here's the jsfiddle.
As @Origin pointed out, this does not prevent the user from accessing the linked url, it only hides the link in javascript-enabled browsers.
first of all, I have to say, this sounds more like a job for server side.
something like (pseudo code)
but if JS solution is required, you can do the following the jquery. Create 2 divs. lets call one "with link" and the other "without link". Check if the time is before or after, and then invoke "hide" on the one you don't want to display. it will look something like
let me know if there's something I should add/modify.
The problem with JavaScript will be that it will be disabled/removed between those times on the client machine and not on your machine. SO say if you want to disable the link in that period in your time (say US) it will not be disabled at the same time in say Europe.
So the best way to do this is to disable the link from server side.
Alternate way to do in JavaScript can be to get the server time or time from some standard location, which will not change according to user's location and depending on that enable or disable the link. I found this code to get the time.
Now you can use this code to show/hide the link by creating 2 divs/spans and displaying one based on your time.
Update:
Here is an working example of the method I just mentioned. http://jsfiddle.net/pkDNX/
You will have to adjust the code to your timezone and the if conditions.
Update: Here is the working example for your requirements. http://jsfiddle.net/pkDNX/1/