How to remove the base Lablel use javascript in html? - javascript

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 = '';

Related

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>

Large List For Random Text/URL - Opi

I have a JavaScript code for random text/url using an external JavaScript, but lets say I have about 100 urls to put in for random text/urls, What would be the best option? JavaScript or something else? I would love to be able to use an external .txt file but I'm a newbie when it comes to PHP. Any ideas or code that may help me on this strange question?
heres the JavaScript code
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML .01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
(function(){
var myLinks = [
{href: 'http://www.whatever.com/somepage.htm', text: 'Some Page'},
{href: 'http://www.whatever.com/anotherpage.htm', text: 'Another Page'},
{href: 'http://www.whatever.com/yetanotherpage.htm', text: 'Yet Another Page'}
];
onload = function(){
var link = myLinks[Math.floor(Math.random() * myLinks.length)],
pageLink = document.getElementById('myLink');
pageLink.href = link.href;
pageLink.firstChild.nodeValue = link.text;
pageLink.target = '_blank';
};
})();
</script>
</head>
<body>
<div>
<a id="myLink" href="somedefault.htm">Some Default Text</a>
</div>
</body>
</html>
I haven't learned PHP, If I understand what you say,use PHP random with a text/url assigned to the href attribute of tag a。

Error occurred in Chrome but not Firefox

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>a demo page</title>
</head>
<body>
<script>
var formatNum=function(num)
{
if(!/^(\+|-)?\d+(\.\d+)?$/.test(num)){
return num;
}
var re = new RegExp().compile("(\\d)(\\d{3})(,|\\.|$)");
num += "";
while(re.test(num))
num = num.replace(re, "$1,$2$3")
return num;
}
alert(formatNum(1000000));
</script>
</body>
</html>
i use these codes to fomat number look like this 1,000,000 it works well in firefox , but not work in chrome
here is the error shows in chrome Uncaught TypeError: Cannot call method 'test' of undefined
how can i resolve this error
The RegExp().compile() method is deprecated.
Why don't you use your regex like this -:
var regexp = new RegExp("(\\d)(\\d{3})(,|\\.|$)");
It's deprecated, and not working in firefox too. Maybe your version is an old one.
Deprecated and obsolete features
Use #Raoul's suggestion.
Try with this
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>a demo page</title>
</head>
<body>
<script>
var formatNum=function(num)
{
if(!/^(\+|-)?\d+(\.\d+)?$/.test(num)){
return num;
}
var re = new RegExp();
re.compile("(\\d)(\\d{3})(,|\\.|$)");
num += "";
while(re.test(num))
num = num.replace(re, "$1,$2$3")
return num;
}
alert(formatNum(1000000));
</script>
</body>
</html>
Reason is chrome doesn't return a reference to the compiled RegExp object when calling compile().
So this line var re = new RegExp().compile("(\\d)(\\d{3})(,|\\.|$)"); will not work instead a less flexible version needs to be followed.
var re = new RegExp();
re.compile("(\\d)(\\d{3})(,|\\.|$)");

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!

Time Zone JS, need help with formatting

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 :)

Categories

Resources