Time Zone JS, need help with formatting - javascript

This code displays times zones vertically. I would like to display them horizontally but with appropriate spacing. I want to replace the 'br' break tag (second to last line of code) with a tag that will provide spacing between a new name and zone. does not work. Simple but tricky for some reason.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/JavaScript">
//Universal clock 2006
//Genuine code by Corneliu Lucian "KOR" Rusu mailto:corneliulucian(AROND)apropo.ro
var wd=['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
var D=[
['Bucharest',120,60],//city,standard time zone(minutes), DST(minutes)
['Madrid',60,60],
['New York',-300,60],
['Nairobi',180,0]
]
//add in the array your cities,STZ, DST
function calc(){
var spans=document.getElementById('zonediv').getElementsByTagName('span')
for(var i=0;i<D.length;i++){
var t=new Date();
t.setTime(t.getTime()+(t.getTimezoneOffset()*60000)+((D[i][1]+D[i][2])*60000));//the zone's time
var Dy=t.getFullYear();
var Dd=t.getDate()<10?'0'+t.getDate():t.getDate();
var Dm=t.getMonth()<10?'0'+(t.getMonth()+1):t.getMonth()+1;
var Dh=t.getHours()<10?'0'+t.getHours():t.getHours();
var Di=t.getMinutes()<10?'0'+t.getMinutes():t.getMinutes();
var Ds=t.getSeconds()<10?'0'+t.getSeconds():t.getSeconds();
var Dz=wd[t.getDay()];
spans[i].firstChild.data=Dh+':'+Di+':'+Ds+' - '+Dz+' '+Dd+'/'+Dm+'/'+Dy;
}
setTimeout('calc()',1000)
}
onload=function(){
var root = document.getElementById('zonediv');
for(var i=0;i<D.length;i++){
root.appendChild(document.createTextNode(D[i][0]+' '))
var sp= document.createElement('span');
sp.appendChild(document.createTextNode(' '));
root.appendChild(sp);root.appendChild(document.createElement('br'))
}
calc();
}
</script>
</head>
<body>
<div id="zonediv">
</div>
</body>
</html>

Try replacing
root.appendChild(document.createElement('br'))
with something like
root.appendChild(document.createTextNode(' '))
This would append a space instead of a br element :)

Related

How to remove the base Lablel use javascript in html?

My website is reverse proxy google (I am in china ,cannot access google ),
like this url :
"https://accounts.google.com/SignUp?hl=zh_CN&continue=https://myaccount.google.com/intro"
I want to remove the base Lablel use javascript?,But my method doesn't work,Maybe I did something wrong, I don't know why.Thank you very much for any help.
my js method :
document.body.innerHTML = document.body.innerHTML.replace('<base href.*?>','')
Original code like this :
<!doctype html>
<html lang="zh-CN" dir="ltr">
<head>
<base href="https://accounts.google.com/">
I expect output :
<!doctype html>
<html lang="zh-CN" dir="ltr">
<head>
If your intent is to clear contents of head tag, this should do:
document.getElementsByTagName('head')[0].innerHTML = ''
If it's just base tag then:
const headTag = document.getElementsByTagName('head')[0];
const baseTag = headTag.getElementsByTagName('base')[0];
headTag.removeChild(baseTag);
Just take the element with querySelector and remove it.
var elem = document.querySelector('base');
elem.parentNode.removeChild(elem);
<!doctype html>
<html lang="zh-CN" dir="ltr">
<head>
<base href="https://accounts.google.com/"/>
</head>
</html>
I hope this helps.
var elem = document.getElementsByTagName('base');
elem[0].href = '';

Selecting specific part of an HTML text

I have a function that returns some HTML fragment that I store in a variable called data, with its whole structure. What I want is to extract from it some of those parts.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script id="hello">
</script>
</head>
<body>
</body>
</html>
For example, I want to get the body and save it in a new variable:
var body = data.split("<body")[1].split(">").slice(1).join(">").split("</body>")[0];
Where data is the HTML text as a string that the original function is returning.
Is there any way I could save an specific script, from its ID (in this case with id = hello), and save it in another variable??
Thank you very much
var newVar = $("#hello").html();
Let's suppose you have an HTML string in a variable, for example
var foo = '<body><span>bar</span></body>';
Now, let's initialize a parser, to convert this into HTML:
var parser = new DOMParser();
var doc = parser.parseFromString(foo, "text/html");
Now, you can read anything from foo, as it is converted into HTML:
document.getElementsByTagName("body")[0].innerHTML = doc.querySelectorAll("body")[0].innerHTML;
$html = document.querySelector("body").innerHTML;
$hello = document.getElementById("hello").innerHTML;
console.log($html);
console.log($hello);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script id="hello">
// script data
</script>
</head>
<body>
</body>
</html>

simple javascript code does not work: var count = colors.push(“red”, “green”);

<html>
<head></head>
<body>
<script>
var colors = new Array();
var count = colors.push(“red”, “green”);
alert(count);
</script>
</body>
</html>
I tried this on firefox and IE, do you think that my version of JavaScript needs to be updated?
You need to use real quotes, either " or ', for example:
var count = colors.push('red', 'green');
The quote character you have used is illegal and is showing the JavaScript error SyntaxError: Unexpected token ILLEGAL.
Demo:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>demo</title>
<script type='text/javascript'>
var colors = new Array();
var count = colors.push('red', 'green');
// alert(count);
alert(colors[0]);
</script>
</head>
<body>
</body>
</html>
I just copy pasted your code in jsFiddle and it looks like your quotes are illegal characters (copy pasted from word / web?). Try typing them out again and it should work.

Javascript RegExp search

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
str = "This is an apple and I love it.";
function searching(query){
var patt = new RegExp(query,"gi");
var query = "<span style='color: red'>"+query+"</span>";
document.getElementById("text").innerHTML = str.replace(patt,query);
return str = document.getElementById("text").innerHTML;
}
/*
patt1 = new RegExp("is","gi");
patt2 = /\san\s/gi;
document.write(str.replace(patt2,"<span style='color:red'> are </span>"));
*/
</script>
</head>
<body>
<div id="text"><script>document.write(str)</script></div>
<form>
<input type="text" id="querys" value="" onkeyup="searching(this.value)" />
</form>
</body>
</html>
I want after each search the result replaces the original string with the hightlight (red).
For example,
For the first time, I search "apple".
"apple" is highlighted in the string.
Return the string in the div with the red highlight.
For the second time, I search "is"
"is" is highlighted with red in the string.
I tried to use innerHTML but innerHTML will also return the inline CSS style tags.
I don't understand the purpose of this line:
return str = document.getElementById("text").innerHTML;
Is str being used somewhere else? In that case, you might need to keep two separate variables: one for the original string (which you'll do your searches against) and one for the modified string with the <span> tags.

document.title problems ie8

Why does IE8 fail to change the documents title with document.title="test title";
Following works on IE8 for me. But I did get the ActiveX security popup, so perhaps your IE8 is not set to prompt for these issues and just deny scripting.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Test</title>
<script type="text/javascript">
function changeTitle() {
document.title = 'Foobar';
}
</script>
</head>
<body onload="changeTitle()">
</body>
</html>
Really? Using document.title = 'Foo Bar'; has always worked for me. Is your script even executing?
Try shoving this right before the document.title = ...:
alert('I work.');
If you don't get an alert box, your script isn't even running.
found this:
http://support.microsoft.com/kb/296113
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JAVASCRIPT">
function runTest()
{
var s ="We should set this as the new title"
var mytitle = document.createElement("TITLE");
mytitle.innerHTML = s;
alert(s);
document.documentElement.childNodes[0].appendChild(mytitle);
}
function fix()
{
var s = "Now we change the title";
alert(s);
document.title = s;
}
</SCRIPT>
</HEAD>
<BODY>
<input type="button" value="Problem" onclick="runTest()"/>
<input type="button" value="Workaround" onclick="fix()"/>
</BODY>
for me this is works in IE 9,8,7
maybe you dont call your function, or there is something which not works.
the document.title must work!

Categories

Resources