0

I'm having an issue trying to insert java scripts into an existing php page (which I didn't create).

What I'm trying to do is use a script that hides DIV's based on Dates. The div's have a lot of calls to PayPal for items that are time sensitive, and I'm trying to have the web page only display the currently available options.

Currently, I have this in the head: script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1 jquery.min.js"

/script

(I do have < and > before and after the code lines, but for some reason it won't let the line display on this post if I include them!)

I have this before the end of the body:

var now = new Date();
var futuretime1 = new Date("April 23, 2015 07:57:00");
var futuretime2 = new Date("April 23, 2015 07:58:00");
if(now > futuretime1)
 {
 $("#div1").hide();
if(now > futuretime2)
 {
 $("#div2").hide();
 }
</script>

This doesn't seem to even be recognized when the page runs in Firefox.

I've tried several different javascripts that are suppose to do the same thing, and none seem to be recognized. I'm pretty new to coding, so I don't know where to go from here!

Could really use some ideas!

5
  • Are you sure you have elements with id "div1" and "div2", and just one of each? If you re-use "id" values things won't work very well. Commented Apr 23, 2015 at 13:41
  • Could we see the html that you are trying to hide? Commented Apr 23, 2015 at 13:41
  • 5
    Your first if is missing a closing bracket }. Commented Apr 23, 2015 at 13:43
  • <div1> <td colspan=2><center> <form target="paypal" action="paypal.com/cgi-bin/webscr" method="post"> <table><tr><td align=center><i>Sign up for</i></td></tr><tr><td><input type="image" src="paypalobjects.com/en_US/i/btn/btn_installment_plan_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"></td></tr></table> <img alt="" border="0" src="paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1"> </form> </td> </tr> </div1> Commented Apr 23, 2015 at 14:04
  • Code is too long to post it says. Suffice it to say that the code between the div and /div tags ( of which there are only one set of div1 and div2 Pointy) sets up table rows. I did add the brackets as Karl-Andre suggested. I'm not sure how to manually check the output FoX. Commented Apr 23, 2015 at 14:09

5 Answers 5

1

You missed the closing bracket } of the if statement

if (now > futuretime1) {
    $("#div1").hide();
}
if (now > futuretime2) {
    $("#div2").hide();
}
Sign up to request clarification or add additional context in comments.

Comments

0

Check with new Date().getTime()

Using Timestamp for check time. I am not sure now time is equal to future time in your point and you missed one close bracket for if

var now = new Date().getTime();
var futuretime1 = new Date("April 23, 2015 07:57:00").getTime();
var futuretime2 = new Date("April 23, 2015 07:58:00").getTime();

if(now > futuretime1)
 {
 $("#div1").hide();
} 
if(now > futuretime2)
 {
 $("#div2").hide();
 }

alert(new Date().getTime()); // timestamp

1 Comment

Nothing I do seems to work. Is there a very simple javascript I can try on a new page to see if it works at all? I can create a blank php page in the website.
0

Missing bracket and try to use Date.parse it will give you correct result.

var now = new Date();
var futuretime1 = new Date("April 23, 2015 07:57:00");
var futuretime2 = new Date("April 23, 2015 07:58:00");


if( Date.parse(now) > Date.parse(futuretime1) )
{
    $("#div1").hide();
}
if(Date.parse(now) > Date.parse(futuretime2))
{
    $("#div2").hide();
}

Comments

0

I setup a codepen here http://codepen.io/michael-hamilton/pen/OVJZaz for you to check out. Click "Test" to run the function that checks the dates and hides the divs. I am having no issues with this working. Make sure that your Javascript is targeting the correct HTML elements. Make sure that the div ID that you want to hide is the same in the JS as in the HTML.

$("#div-1").hide();

You can verify this works in codepen by changing the future dates and and running the code again.

Also for the other commenters - I checked the values of futuretime1 and futuretime2 against now and they are all in the same format

7 Comments

Michael - does the div ID need the # as part of it as well?
no, the id attribute of the div does not need the the #. The # is a selector that is used in CSS that signifies id. jQuery uses these same selectors as well
your div element should look something like this: <div id="div-1"></div>
I can get your codepen to work, but copying it to a new php page and running it in Firefox gets me nothing.
Run this in Firefox, get nothing. : <html> <head> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1 jquery.min.js"> <script> function test(){ var now = new Date(); var futuretime1 = new Date("April 23, 2015 11:53:00"); var futuretime2 = new Date("April 23, 2015 11:53:30"); if(now > futuretime1){ $("#div-1").hide(); } if(now > futuretime2){ $("#div-2").hide(); } } $('#test').click(test); </script> </head> <body> <div id="div-1"> div 1 </div> <div id="div-2"> div 2 </div> <a href="javascript:;" id="test">Test</a> </body> </html>
|
0
You can do this way

// this will wrk perfectly

<?php
$today_date_time = date('Y-m-d H:i:s');
var $futuretime1 = new Date("April 23, 2015 07:57:00");
var $futuretime2 = new Date("April 23, 2015 07:58:00");
?>
<script>
var now = '<?php echo strtotime($today_date_time)?>';
var futuretime1 = '<?php echo strtotime($futuretime1)?>';
var futuretime2 = '<?php echo strtotime($futuretime2)?>';
if(now>futuretime1)
{
$("#div1").hide();
}
if(now>futuretime2)
{
$("#div2").hide();
}
</script>

2 Comments

Neelesh - where should these be inserted? My "script" is located just before the end of the body... should the <?php go in the head?
It can be added any where but should be at top of the script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.