The following script works well on a page by itself. However, when embedding it, the document.write takes over, thus rendering the rest of the page into oblivion. I would like an alternative solution to building the required links via variables. I have little to no experience with anything beyond document.write for these kinds of things. Any help/guidance is greatly appreciated.
<script language="javascript" type="text/javascript">
Today = new Date();
TodayDay = Today.getDate();
TodayMon = Today.getMonth();
TodayYear = Today.getYear();
if (TodayYear < 2000) TodayYear += 1900;
escNextYear = TodayYear;
escTwoYears = TodayYear;
if (TodayMon == 0) { TodayMonth = "January"; escNextMonth = "February"; escTwoMonths = "March"; }
else if (TodayMon == 1) { TodayMonth = "February"; escNextMonth = "March"; escTwoMonths = "April"; }
else if (TodayMon == 2) { TodayMonth = "March"; escNextMonth = "April"; escTwoMonths = "May"; }
else if (TodayMon == 3) { TodayMonth = "April"; escNextMonth = "May"; escTwoMonths = "June"; }
else if (TodayMon == 4) { TodayMonth = "May"; escNextMonth = "June"; escTwoMonths = "July"; }
else if (TodayMon == 5) { TodayMonth = "June"; escNextMonth = "July"; escTwoMonths = "August"; }
else if (TodayMon == 6) { TodayMonth = "July"; escNextMonth = "August"; escTwoMonths = "September"; }
else if (TodayMon == 7) { TodayMonth = "August"; escNextMonth = "September"; escTwoMonths = "October"; }
else if (TodayMon == 8) { TodayMonth = "September"; escNextMonth = "October"; escTwoMonths = "November"; }
else if (TodayMon == 9) { TodayMonth = "October"; escNextMonth = "November"; escTwoMonths = "December"; }
else if (TodayMon == 10) { TodayMonth = "November"; escNextMonth = "December"; escTwoMonths = "January"; }
else if (TodayMon == 11) { TodayMonth = "December"; escNextMonth = "January"; escTwoMonths = "February"; }
else { TodayMonth = TodayMon; }
if (TodayMon == 11) {escTwoYears = TodayYear+1};
if (TodayMon == 11) {escNextYear = TodayYear+1};
if (TodayMon == 10) {escTwoYears = TodayYear+1};
escdate = TodayMon+1 + "/01/" + TodayYear;
escNextMon = TodayMon+2 + "/01/" + TodayYear; //Vinnie
if (escNextMon > 12) {escNextMon = escNextMon - 12};
escTwoMon = TodayMon+3;
if (escTwoMon > 12) {escTwoMon = escTwoMon - 12};
escdatenext = escNextMon + "/01/" + escNextYear;
escdatetwo = escTwoMon + "/01/" + escTwoYears;
// document.write(escdate);
// document.write(TodayMonth + " " + TodayDay + ", " + TodayYear);
html = '<center>Please click on the month to view<br />the Professional Development Calendar for<br />' +
'<strong>' + TodayMonth + '</strong>, ' +
'<strong>' + escNextMonth + '</strong>, and ' +
'<strong>' + escTwoMonths + '</strong>' +
'<p><div style="border: 3px solid rgb(201, 0, 1); overflow: hidden; margin: 15px auto; max-width: 736px;">' +
'<iframe scrolling="yes" src="https://www.escweb.net/ar_esc/catalog/coopcalendar.aspx?location=2464&mode=weekly" style="border: 0px none; height: 600px; width: 100%;"></iframe>';
document.write(html);
</script>
It looks like the reason your page is being rendered "into oblivion" is because there are HTML tags that aren't being closed. Try this:
html = '<center>Please click on the month to view<br />the Professional Development Calendar for<br />' +
'<strong>' + TodayMonth + '</strong>, ' +
'<strong>' + escNextMonth + '</strong>, and ' +
'<strong>' + escTwoMonths + '</strong>' +
'<p><div style="border: 3px solid rgb(201, 0, 1); overflow: hidden; margin: 15px auto; max-width: 736px;">' +
'<iframe scrolling="yes" src="https://www.escweb.net/ar_esc/catalog/coopcalendar.aspx?location=2464&mode=weekly" style="border: 0px none; height: 600px; width: 100%;"></iframe>' +
'</div></p></center>';
Related
how can I differentiate when I have to use HEX code and color name? and also how can I change my background color? I mean, I think I have changed my background color to black, but when I embed it to my Notion page, it still comes up with aliceblue color, not black. anyone can help me to add time?
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var today = new Date();
var hrs = today.getHours();
var dayOfWeek = weekday[today.getDay()];
var date = dayOfWeek + " " + today.getDate() + "/" + (today.getMonth() + 1) + '/' + today.getFullYear();
var greet;
if (hrs < 12)
greet = 'Good Morning ';
else if (hrs >= 12 && hrs <= 17)
greet = 'Good Afternoon ';
else if (hrs >= 17 && hrs <= 18)
greet = ' Good Evening ';
else if (hrs >= 18 && hrs <= 24)
greet = 'Good Night ';
document.getElementById('lbl').innerHTML =
greet += "Ivan" + `<div id="date"> It's ${date}</div>`;
#lbl {
font-family: monospace;
font-size: xx-large;
color: #F0F8FF;
padding: 5px;
text-align: center;
line-height: 1.5;
height: 85%
}
#date {
font-size: large;
}
body {
background-color: black;
}
<div id="lbl"></div>
I'm trying to set up a countdown timer in WordPress, it works fine on Windows browsers but it shows NaN on iOS browsers eg: safari and chrome.
stackflow is asking me to add some details but I don't have more details to post or at least I can't think of anything so here more details.
function calcage(secs, num1, num2) {
s = ((Math.floor(secs/num1))%num2).toString();
if (LeadingZero && s.length < 2)
s = "0" + s;
return "<b>" + s + "</b>";
}
function CountBack(secs) {
if (secs < 0) {
document.getElementById("cntdwn").innerHTML = FinishMessage;
return;
}
DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000));
DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));
document.getElementById("cntdwn").innerHTML = DisplayStr;
if (CountActive)
setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
}
function putspan(backcolor, forecolor) {
document.write("<span id='cntdwn' style='background-color:" + backcolor +
"; color:" + forecolor + "'></span>");
}
if (typeof(BackColor)=="undefined")
BackColor = "white";
if (typeof(ForeColor)=="undefined")
ForeColor= "black";
if (typeof(TargetDate)=="undefined")
TargetDate = "12/31/2020 5:00 AM";
if (typeof(DisplayFormat)=="undefined")
DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
if (typeof(CountActive)=="undefined")
CountActive = true;
if (typeof(FinishMessage)=="undefined")
FinishMessage = "";
if (typeof(CountStepper)!="number")
CountStepper = -1;
if (typeof(LeadingZero)=="undefined")
LeadingZero = true;
CountStepper = Math.ceil(CountStepper);
if (CountStepper == 0)
CountActive = false;
var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
putspan(BackColor, ForeColor);
var dthen = new Date(TargetDate);
var dnow = new Date();
if(CountStepper>0)
ddiff = new Date(dnow-dthen);
else
ddiff = new Date(dthen-dnow);
gsecs = Math.floor(ddiff.valueOf()/1000);
CountBack(gsecs);
I have a web page where I am reading Google Blogger blog category from his feed using JSON. I have two functions. First on is getting all the Blog Categories List and second one is taking Blog Categories from that and then hitting again Blog to get latest posts from that This is the text test to see that web page data is here or not.
<div id="blogCategoriesList">
<script type="text/javascript">
var blogurl = "https://googleblog.blogspot.com/";
function blogCatList(json) {
document.write('<select onchange="showThisCatPosts(this.value)">');
document.write('<option>CHOOSE A CATEGORY</option>');
for (var i = 0; i < json.feed.category.length; i++)
{
var item = "<option value='" + json.feed.category[i].term + "'>" + json.feed.category[i].term + "</option>";
document.write(item);
}
document.write('</select>');
}
document.write('<script type=\"text/javascript\" src=\"' + blogurl + '/feeds/posts/default?redirect=false&orderby=published&alt=json-in-script&callback=blogCatList&max-results=500\"><\/script>');
document.write('<br/><br/><a href=\"' + blogurl + '\" target=\"_blank\" class=\"footerLINK\">Read The Blog Online Now<\/a>');
</script>
</div>
<div id="blogCategoriesPost">
<script style='text/javascript'>
var blogurl = "https://googleblog.blogspot.com/";
var numposts = 10; // Out Of 500
var displaymore = true;
var showcommentnum = true;
var showpostdate = true;
var showpostsummary = true;
var numchars = 100;
function blogCategoriesPost(json) {
if(json.feed.entry.length < numposts ){
numposts = json.feed.entry.length;
}
for (var i = 0; i < numposts; i++) {
var entry = json.feed.entry[i];
var posttitle = entry.title.$t;
var posturl;
if (i == json.feed.entry.length) break;
for (var k = 0; k < entry.link.length; k++) {
if (entry.link[k].rel == 'replies' && entry.link[k].type == 'text/html') {
var commenttext = entry.link[k].title;
var commenturl = entry.link[k].href;
}
if (entry.link[k].rel == 'alternate') {
posturl = entry.link[k].href;
break;
}
}
var postdate = entry.published.$t;
var cdyear = postdate.substring(0, 4);
var cdmonth = postdate.substring(5, 7);
var cdday = postdate.substring(8, 10);
var monthnames = new Array();
monthnames[1] = "Jan";
monthnames[2] = "Feb";
monthnames[3] = "Mar";
monthnames[4] = "Apr";
monthnames[5] = "May";
monthnames[6] = "Jun";
monthnames[7] = "Jul";
monthnames[8] = "Aug";
monthnames[9] = "Sep";
monthnames[10] = "Oct";
monthnames[11] = "Nov";
monthnames[12] = "Dec";
document.write('<div id="mainDIV">');
document.write('<h2 class="post_heading">' + posttitle + '</h2>');
if ("content" in entry) {
var postcontent = entry.content.$t;
} else
if ("summary" in entry) {
var postcontent = entry.summary.$t;
} else var postcontent = "";
var re = /<\S[^>]*>/g;
postcontent = postcontent.replace(re, ""); // Will Show Only Text Instead Of HTML
if (showpostsummary == true) {
if (postcontent.length < numchars) {
document.write('<span class="post_summary">');
document.write(postcontent);
document.write('</span>');
} else {
//document.getElementById("catPosts").innerHTML += '<span class="post_summary">';
document.write('<span class="post_summary">');
postcontent = postcontent.substring(0, numchars);
var quoteEnd = postcontent.lastIndexOf(" ");
postcontent = postcontent.substring(0, quoteEnd);
document.write(postcontent + '...');
document.write('</span>');
}
}
var towrite = '';
document.write('<strong class="post_footer">');
if (showpostdate == true) {
towrite = 'Published On: ' + towrite + monthnames[parseInt(cdmonth, 10)] + '-' + cdday + '-' + cdyear;
}
if (showcommentnum == true) {
if (commenttext == '1 Comments') commenttext = '1 Comment';
if (commenttext == '0 Comments') commenttext = 'No Comments';
commenttext = '<br/>' + commenttext + '';
towrite = towrite + commenttext;
}
if (displaymore == true) {
towrite = towrite + '<br/>Read Full Article -->';
}
document.write(towrite);
document.write('</strong></div>');
}
}
function showThisCatPosts(BLOGCAT){
document.write('<script type=\"text/javascript\" src=\"' + blogurl + '/feeds/posts/default/-/' + BLOGCAT + '?redirect=false&orderby=published&alt=json-in-script&callback=blogCategoriesPost&max-results=500\"><\/script>');
document.write('<a href=\"' + blogurl + '\" target=\"_blank\" class=\"footerLINK\">Read The Blog Online Now<\/a>');
}
</script>
You can see a working DEMO at JSBIN. My problem is that when page load, its works perfectly showing all page data and the blog categories lists too but when I select any category then my all page data remove and only that label posts are visible. Why this is happening...??? I want to just change the posts as per label not to remove all page data...
That's the normal behaviour of document.write(). You might need to use:
document.getElementById("element_id").innerHTML = 'Stuff to Write.';
Finally I got it solved. The reason is that document.write() is bad as it write the text on page load. You can use it if you want to write some text on page load not later.
If you want to write later then have to use document.getElementById("element_id").innerHTML to write your text but if you want to write <script> tags then document.getElementById("element_id").innerHTML is not good as it will write but dont hit the SRC so use document.createElement("script") to write scripts after page load that will be runable too.
See the working DEMO of my code at JSBIN... :)
Special Thanks To: #Praveen Kumar :-)
I have a script which lists Google Calendar events with the start time, name and description but I would like to have the end time too so it reads;
5:00pm - 6:00pm - Event title - event description
Rather than just what it has at the moment which is just the start time and not the end time.
This is the script I have:
$(document).ready(GCalEvents());
function GCalEvents() {
var datenow = new Date();
var curyear = datenow.getFullYear();
var curmonth = datenow.getMonth() + 1;
var curdate = datenow.getDate();
var curhour = datenow.getHours();
var curmin = datenow.getMinutes();
var curtz = datenow.getTimezoneOffset();
//curtz = -480;
var plusmin = "-";
if (curtz <= 0) {
plusmin = "%2B";
}
curtz = Math.abs(curtz);
var curtzmin = curtz % 60;
curtz = parseInt(curtz / 60);
if (curtzmin < 10) { curtzmin = "0" + curtzmin; }
if (curtz < 10) { curtz = "0" + curtz; }
datenow = curyear + "-" + curmonth + "-" + curdate + "T" + curhour + "%3A" + curmin + "%3A00" + plusmin + curtz + "%3A" + curtzmin;
var url = "https://www.googleapis.com/calendar/v3/calendars/{calendarid}/events?alwaysIncludeEmail=false&maxResults=2&orderBy=startTime&showDeleted=false&showHiddenInvitations=false&singleEvents=true&timeMin=" + datenow + "&fields=items(start%2Csummary%2Cdescription)%2CtimeZone&key={key}";
// var url = "https://www.googleapis.com/calendar/v3/calendars/{calendarid}/events?alwaysIncludeEmail=false&maxResults=2&orderBy=startTime&showDeleted=false&showHiddenInvitations=false&singleEvents=true&timeMin=" + datenow + "&fields=items(start%2Csummary%2Cdescription)%2CtimeZone&key={key}";
$.getJSON(url, function(data) {
var event_start_date_new = new Date();
event_start_date_new.setDate(event_start_date_new.getDate() - 1);
for(i in data['items']) {
item = data['items'][i];
// event title
var event_title = item.summary;
var event_description = item.description;
// event start date/time
var event_start_date = new Date(item.start.dateTime);
// format the date and time
var month = event_start_date.getMonth();
var day = event_start_date.getDay();
var date = event_start_date.getDate();
var daysArr = new Array();
daysArr[0] = "Sunday";
daysArr[1] = "Monday";
daysArr[2] = "Tuesday";
daysArr[3] = "Wednesday";
daysArr[4] = "Thursday";
daysArr[5] = "Friday";
daysArr[6] = "Saturday";
var monthsArr = new Array();
monthsArr[0] = "January";
monthsArr[1] = "February";
monthsArr[2] = "March";
monthsArr[3] = "April";
monthsArr[4] = "May";
monthsArr[5] = "June";
monthsArr[6] = "July";
monthsArr[7] = "August";
monthsArr[8] = "September";
monthsArr[9] = "October";
monthsArr[10] = "November";
monthsArr[11] = "December";
var hour = event_start_date.getHours();
var min = event_start_date.getMinutes();
if (min < 10) { min = "0" + min; }
if (hour < 11) {
var ampm = "am";
if (hour == 0) { hour = 12; }
} else {
var ampm = "pm";
if (hour > 12) { hour = hour - 12; }
}
if (hour < 10) { hour = " " + hour; }
var event_start_str = daysArr[day] + ", " + monthsArr[month] + " " + date;
var event_time_str = hour + ":" + min + " " + ampm;
if (event_start_date.getDate() != event_start_date_new.getDate()) {
event_start_str = "<div class=\"calendar_date\"><strong>" + event_start_str + "</strong></div>";
event_start_date_new = event_start_date;
} else {
event_start_str = "";
};
// Render the event
$("#gcal-events").last().before("<div class=\"calendar_item\"><li>" + event_time_str + " - " + event_title + "<p><small>" + event_description + "</small></p></ul>" + "</li></div>");
}
});
};
If anyone is able to help with that to reversion the current script, that would be great.
The Events resource returns start and end dates as well (JSON format that has date, dateTime, timeZone attributes).
Based on your code, you're not using the end attribute which is why you're not having any values for the end date displayed. Your code for the start date is alright, so you can just re-implement it for the end attribute.
Hope this helps!
I am attempting to display a greeting before the date / time depending on what time of day it is.
Good Morning
Good Afternoon
Good Evening
I have that message working.
I also want to have the date and time displayed as "It is TIME on DATE."
Whenever I try to change something with the time date code I can no longer get it to display even that.
Any advice would be helpful.
function MakeArray(n) {
this.length = n;
}
monthNames = new MakeArray(13);
monthNames[1] = "January";
monthNames[2] = "February";
monthNames[3] = "March";
monthNames[4] = "April";
monthNames[5] = "May";
monthNames[6] = "June";
monthNames[7] = "July";
monthNames[8] = "August";
monthNames[9] = "September";
monthNames[10] = "October";
monthNames[11] = "November";
monthNames[12] = "December";
dayNames = new MakeArray(8);
dayNames[1] = "Sunday";
dayNames[2] = "Monday";
dayNames[3] = "Tuesday";
dayNames[4] = "Wednesday";
dayNames[5] = "Thursday";
dayNames[6] = "Friday";
dayNames[7] = "Saturday";
function dayPart(oneDate) {
var theHour = oneDate.getHours();
if (theHour < 12) {
return "Good morning";
}
if (theHour < 18) {
return "Good afternoon";
}
return "Good evening";
}
function customDateString(oneDate) {
var theDay = dayNames[oneDate.getDay() + 1],
theMonth = monthNames[oneDate.getMonth() + 1],
theYear = oneDate.getYear();
theYear += (theYear < 100) ? 1900 : 0;
return theDay + ", " + theMonth + " " + oneDate.getDate() + ", " + theYear;
}
var today = new Date();
alert(dayPart(today) + "." + customDateString(today));
On jsFiddle
Try this as your function:
function customDateString(oneDate) {
var theDay = dayNames[oneDate.getDay() + 1]
var theMonth = monthNames[oneDate.getMonth() + 1]
var theYear = oneDate.getFullYear()
theYear += (theYear < 100) ? 1900 : 0
return 'It is ' + new Date().timeNow() + ' on ' + theMonth + " " + oneDate.getDate() + ", " + theYear + '. ';
}
With this prototype:
Date.prototype.timeNow = function () {
return ((this.getHours() < 10)?"0":"") + this.getHours() +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds();
}
Demo
getFullYear returns a four-digit year, instead of just getYear which, for example, would return 14 in the year 2014.