Adobe Muse shows document.createElement link, but won't preview - javascript

I'm trying to show a link only at certain times of the day.
The link is visible within Adobe Muse, but is hidden on browser.
Here's my code
<script type="text/javascript">
var day= new Date();
var hr= day.getUTCHours();
if ((hr == 0) || (hr == 1) || (hr == 2) || (hr == 3) || (hr == 4) || (hr == 5) || (hr == 6) || (hr == 7) || (hr == 8) || (hr == 9) || (hr == 10) || (hr == 11) || (hr == 12) || (hr == 13) || (hr == 22) || (hr == 23)) { var a = document.createElement('a'); var linkText = document.createTextNode("Example"); a.appendChild(linkText); a.title = "Example"; a.style.fontSize = "16px"; a.style.color="#C31E2F"; a.href = "http://www.example.com/"; document.body.appendChild(a);}
if ((hr == 14) || (hr == 15) || (hr == 16) || (hr == 17) || (hr == 18) || (hr == 19) || (hr == 20) || (hr == 21)) { document.write("<br><font>CALL TOLL FREE:xxx-xxx-xxxx</font></br>");}

What do you mean it's hidden? This script is getting time from your device local time. If it's correct, you should get correct result.
<script type="text/javascript">
var day= new Date();
var hr= day.getUTCHours();
if ((hr>=0 && hr<13) || (hr>=22 && hr<=23)) {
var a = document.createElement('a');
var linkText = document.createTextNode("Example");
a.appendChild(linkText); a.title = "Example";
a.style = "font-size: 16px; #C31E2F; color: #C31E2F";
a.href = "http://www.example.com/";
document.body.appendChild(a);
} else {
var out=document.createElement("P");
out.innerHTML = "<br>CALL TOLL FREE:xxx-xxx-xxxx</br>";
document.body.appendChild(out);
}
</script>

Ended up trashing most of the existing code. I think document.createElement was causing the problem.
Here's what I'm using instead.
<script type="text/javascript">
var day= new Date();
var hr= day.getUTCHours();
if ((hr>=0 && hr<13) || (hr>=22 && hr<=23)) {
document.write('<font>Example</font>'.link('http://www.example.com/')); }
else { document.write("<font>CALL TOLL FREE:xxx-xxx-xxxx</font>"); }

Related

If condition does not follow (JavaScript)

Creating a SharePoint Portal using JavaScript and HTML where the problem is when i'm inputting a number around 100,000 to 800,000; ex(523,546) it would enter in the first if condition and do the statement below in the. even though the value is less than the given MDV "Total Estimated Freight".
The value of the pagetdv is inputted.
var pagetdvz = document.getElementById('pagetdv');
var pagesrz = document.getElementById('pagesr');
var pagemdvz = document.getElementById('pagemdv');
/------------ Maximum Declared Value ----------/
if (document.getElementById('dropct').selectedIndex == 0)
document.getElementById('pagemdv').value = 1500000;
else if (document.getElementById('dropct').selectedIndex == 1)
document.getElementById('pagemdv').value = 3000000;
/------------ Dest + 20 Ftr ----------/
if ((document.getElementById('dropdest').selectedIndex == 0) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 45900;
else if ((document.getElementById('dropdest').selectedIndex == 1) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 50000;
else if ((document.getElementById('dropdest').selectedIndex == 2) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 46700;
else if ((document.getElementById('dropdest').selectedIndex == 3) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 47583.67;
else if ((document.getElementById('dropdest').selectedIndex == 4) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 59981.33;
else if ((document.getElementById('dropdest').selectedIndex == 5) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 45900;
else if ((document.getElementById('dropdest').selectedIndex == 6) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 59000;
else if ((document.getElementById('dropdest').selectedIndex == 7) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 45900;
else if ((document.getElementById('dropdest').selectedIndex == 8) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 59,981.33;
else if ((document.getElementById('dropdest').selectedIndex == 9) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 58500;
else if ((document.getElementById('dropdest').selectedIndex == 10) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 49000;
else if ((document.getElementById('dropdest').selectedIndex == 11) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 46700;
else if ((document.getElementById('dropdest').selectedIndex == 12) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 51000;
/------------ Dest + 40 Ftr----------/
if ((document.getElementById('dropdest').selectedIndex == 0) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 89600;
else if ((document.getElementById('dropdest').selectedIndex == 1) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 95500;
else if ((document.getElementById('dropdest').selectedIndex == 2) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 91096.87;
else if ((document.getElementById('dropdest').selectedIndex == 3) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 94944.66;
else if ((document.getElementById('dropdest').selectedIndex == 4) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 119739.26;
else if ((document.getElementById('dropdest').selectedIndex == 5) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 89600;
else if ((document.getElementById('dropdest').selectedIndex == 6) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 117487.73;
else if ((document.getElementById('dropdest').selectedIndex == 7) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 89600;
else if ((document.getElementById('dropdest').selectedIndex == 8) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 119739.26;
else if ((document.getElementById('dropdest').selectedIndex == 9) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 113000;
else if ((document.getElementById('dropdest').selectedIndex == 10) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = "--Null--";
else if ((document.getElementById('dropdest').selectedIndex == 11) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 91096.87;
else if ((document.getElementById('dropdest').selectedIndex == 12) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = "--Null--";
/------------ Total Estimated Freight ----------/
if (document.getElementById('pagetdv').value > document.getElementById('pagemdv').value){
var tdva = parseFloat(pagetdvz.value) - parseFloat(pagemdvz.value);
var tdvb = tdva / 1000;
var tdvc = tdvb * 3.36;
var tef = tdvc + parseFloat(pagesrz.value);
document.getElementById('pagetef').value = tef;
}
else if (document.getElementById('pagetdv').value <= document.getElementById('pagemdv').value)
document.getElementById('pagetef').value = document.getElementById('pagesr').value;

displaying person before greeting

Hi I need those two to show in the same line(Now they show one after another). I tried many things but nothing works.
document.write();
var day = new Date();
var hr = day.getHours();
if ((hr == 1) || (hr == 2) || (hr == 3) || (hr == 4) || (hr == 5) || (hr ==
6) || (hr == 7) || (hr == 8) || (hr == 9) || (hr == 10) || (hr == 11) ||
(hr == 12)) {
document.write("Good Morning!");
}
if ((hr == 13) || (hr == 14) || (hr == 15) || (hr == 16) || (hr == 17)) {
document.write("Good Afternoon!");
}
if ((hr == 18) || (hr == 19) || (hr == 20) || (hr == 21) || (hr == 22) ||
(hr == 23) || (hr == 24)) {
document.write("Good Evening!");
}
document.write();
/*--------------------------------------------------------------------------
---------------------------*/
function myFunction() {
var person = prompt("Please enter your name", "Harry Potter");
if (person != null) {
document.getElementById("demo").innerHTML =
person;
}
}
Is this what you are looking for:
<html>
<body onload="myFunction()">
<div id="demo"></div>
<script>
function myFunction() {
var day = new Date();
var hr = day.getHours();
var greeting;
var person = prompt("Please enter your name", "Harry Potter");
if (person != null) {
if ( hr <= 12 ) {
greeting = "Good morning " + person;
} else if ( hr <= 18 ) {
greeting = "Good afternoon " + person;
} else {
greeting = "Good evening " + person;
}
document.getElementById("demo").innerHTML = greeting;
}
}
</script>
</body>
</html>
I hope this'll Help you. i also done code optimization for if
Conditions.
You have to invoke myfunction method
<body>
<span id="demo"></span>
<script>
(function () {
var person = prompt("Please enter your name", "Harry Potter");
if (person != null) {
document.write();
var day = new Date();
var hr = day.getHours();
if ((hr >= 1) && (hr <= 12)) {
person = person + " Good Morning!";
}
else if ((hr >= 13) && (hr <= 17)) {
person = person + " Good Afternoon!";
}
else if ((hr >= 18) && (hr <= 24)) {
person = person + "Good Evening!" ;
}
document.getElementById("demo").innerHTML = person;
}
})()
</script>
</body>
document.write() does not add a new line to document but rather changes your document element to the content, where as document.getElementById() refers to specific element in the DOM. Anyway here is my
javascript:
function greeting() {
var person = prompt("Please enter your name", "Harry Potter");
if (person != null) {
// Set hr to current hour
var hr = new Date().getHours();
// Tests current hour
if (hr > 0 && hr < 13) {
document.write("Good Morning! " + person);
} else if (hr > 12 && hr < 18) {
document.write("Good Afternoon! " + person);
} else if (hr > 17 && hr < 25) {
document.write("Good Evening! " + person);
}
}
}

Max Value with decimal 99.999 HTML block input

I'm trying to make the input takes only the value 99.999. I don't want to use MaxLength because it would not calculate the length of the decimal digits. I don't want to use any other functions that erase when it doesn't match a specific regex. I want it to stop it in the input.
function IsCurrencyNoMinus1 (e, thisobj, min, max) {
var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode == 44) || (keyCode == 46) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode))
var inStr = $(thisobj).val();
if (ret && (keyCode == 45) && ((thisobj.selectionStart != 0) || (inStr.indexOf('-') != -1)))
ret = false;
if (ret && (keyCode == 46) && (inStr != '' && inStr.indexOf('.') != -1) && !(Math.abs(thisobj.selectionStart - thisobj.selectionEnd) == inStr.length)) {
ret = false;
}
var dotPos = (inStr.indexOf('.') != -1) ? inStr.indexOf('.') : inStr.length;
inStr = inStr.replace(/\,/g, '');
var parts = inStr.split('.');
var maxParts = max.toString().split('.');
if (ret && (inStr != '' && (keyCode >= 48 && keyCode <= 57))) {
if ((parts[0].length >= maxParts[0].length) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0)
&& (thisobj.selectionStart <= dotPos)) {
ret = false;
}
if (ret && (parts[1] != undefined && parts[1].length >= 2) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0)
&& (thisobj.selectionStart > dotPos) && (thisobj.selectionStart <= dotPos + 3))
ret = false;
var firstPos = thisobj.selectionStart < thisobj.selectionEnd ? thisobj.selectionStart : thisobj.selectionEnd;
if (ret && (parts[0].length >= maxParts[0].length) && (parts[1] != undefined && parts[1].length >= 1)
&& ((dotPos - firstPos == 0 && Math.abs(thisobj.selectionStart - thisobj.selectionEnd) < 4)
|| (dotPos - firstPos == 1 && (Math.abs(thisobj.selectionStart - thisobj.selectionEnd) >= 2 && Math.abs(thisobj.selectionStart - thisobj.selectionEnd) < 4))))
ret = false;
}
if (Number(inStr) > max) {
thisobj.value = '';
ret = true;
}
if (Number(inStr) < min) {
thisobj.value = '';
ret = true;
}
// var re = new RegExp(/^\(?-?[0-9]{0,12}(\.[0-9]{0,2})?\)?$/)
// if (!re.test(inStr)) {
// thisobj.value = ""
// }
return ret
}
I found the solution! Please check the code below in case someone needs it.
function Format3DigitDecimal(e, thisobj, min, max)
{
var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode
var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode == 44) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode))
var inStr = $(thisobj).val()
inStr = inStr.replace(/\,/g, '')
if (ret && (inStr != '' && (keyCode >= 48 && keyCode <= 57)))
{
if ((inStr.length >= max.toString().length) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0))
{
ret = false
}
}
if (ret && (inStr != '' && (keyCode >= 48 && keyCode <= 57)))
{
if ((inStr.length == 2) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0))
{
ret = false
}
}
return ret
}

Combining two scripts in Google sheets (Javascript)

I'm trying to combine two scripts I use on google sheets into one.
They will both work on different tabs.
What is the proper way of combining scripts together?
My two scripts are as follows:
function onEdit(e) {
var sheets = ['Sanshiro', 'Yujiro', 'Mei', 'Suil', 'Martin', 'Yuta', 'Rachel','So'],
cols = [1, 6, 4],
writeCols = [15, 11],
ind = cols.indexOf(e.range.columnStart);
if (sheets.indexOf(e.source.getActiveSheet()
.getName()) === -1 || ind === -1 || !e.value) return;
if (ind === 0 && e.value === 'Update') {
e.range.setValue(new Date());
} else if (ind === 1) {
if (e.range.offset(0, 5)
.getValue() === '') e.range.offset(0, 5)
.setValue(2);
if (e.range.offset(0, 9)
.getValue() === '') e.range.offset(0, 10)
.setValue(new Date());
if (e.range.offset(0, -5)
.getValue() === '') e.range.offset(0, -5)
.setValue(new Date());
else if (ind === 1) {
if (e.range.offset(0, 1)
.getValue() === 'Updated') e.range.offset(0, 1)
.setValue(Col1);
}
}}
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Must Place candis" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 2 ) { //checks the column
var nextCell = r.offset(0,12);
nextCell.setValue(new Date());
}
}
}
Any help would be much appreciated! If you can help me understand the process then I would like to learn!
Thanks so much!
How about following script? 2 script was summarized in one using "IF".
function onEdit(e) {
var s = SpreadsheetApp.getActiveSheet();
var sheets = ['Sanshiro', 'Yujiro', 'Mei', 'Suil', 'Martin', 'Yuta', 'Rachel','So'];
if (sheets.indexOf(e.source.getActiveSheet().getName()) > -1){
script1();
}
if(s.getName() == "Must Place candis") {
script2(s);
}
}
function script1(){
var cols = [1, 6, 4],
writeCols = [15, 11],
ind = cols.indexOf(e.range.columnStart);
if (ind === 0 && e.value === 'Update') {
e.range.setValue(new Date());
} else if (ind === 1) {
if (e.range.offset(0, 5)
.getValue() === '') e.range.offset(0, 5)
.setValue(2);
if (e.range.offset(0, 9)
.getValue() === '') e.range.offset(0, 10)
.setValue(new Date());
if (e.range.offset(0, -5)
.getValue() === '') e.range.offset(0, -5)
.setValue(new Date());
else if (ind === 1) {
if (e.range.offset(0, 1)
.getValue() === 'Updated') e.range.offset(0, 1)
.setValue(Col1);
}
}
}
function scritp2(s){
if( s.getName() == "Must Place candis" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 2 ) { //checks the column
var nextCell = r.offset(0,12);
nextCell.setValue(new Date());
}
}
}

JavaScript time greeting?

How do you make something with JavaScript that when your time is below 12 o'clock, it says "good morning!" and when it is after 12 o'clock, it says "good afternoon!"?
Here it is!
var d = new Date();
var time = d.getHours();
if (time < 12) {
document.write("<b>Good morning!</b>");
}
if (time > 12) {
document.write("<b>Good afternoon!</b>");
}
if (time == 12) {
document.write("<b>Go eat lunch!</b>");
}
const date = new Date;
let hours = date.getHours();
let status = (hours < 12)? "Morning" :
((hours <= 18 && hours >= 12 ) ? "Afternoon" : "Night");
console.log(status);
The following should work:
var hours = new Date().hours;
if(hours > 12){
alert("Good Afternoon!");
}
else{
alert("Good Morning!");
}
Just for fun, here's a one liner:
(new Date().hours > 12) ? alert("Good Afternoon!") : alert("Good Morning!");
Working Demo
<SCRIPT LANGUAGE="JavaScript">
currentTime=new Date();
//getHour() function will retrieve the hour from current time
if(currentTime.getHours()<12)
document.write("<b>Good Morning!! </b>");
else if(currentTime.getHours()<17)
document.write("<b>Good Afternoon!! </b>");
else
document.write("<b>Good Evening!! </b>");
</SCRIPT>
//if hour is between 6am-12pm ,print good morning
//if hour is between 12pm-6pm ,print good afternoon
//else good evening
let hour = 5;
if(hour>=6 && hour<12) {
console.log('Good Morning')
}
else if(hour>+12 && hour<18) {
console.log('Good Afternoon')
}
else {
console.log('Good Evening')
}
<script type="text/javascript">
document.write("<h2>");
var day = new Date();
var hr = day.getHours();
if (hr >= 4 && hr < 12) {
document.write("Goedenmorgen");
} else if (hr == 12) {
document.write("Goedemiddag");
} else if (hr >= 12 && hr <= 16) {
document.write("Goedemiddag");
} else if (hr >= 16 && hr <= 23) {
document.write("Goedenavond");
} else {
document.write("Goedennacht");
}
document.write("</h2>");
</script>

Categories