<!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})(,|\\.|$)");
Related
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 = '';
I'm new to JavaScript and already encountered a problem. When I run the code and the browser pops up, it[browser] does not show anything. What I have is the testMethod.js file with one method:
function testMethod(num1, num2){
var value = num1 + num2;
return value;
}
and an HTML file from where I'm trying to run:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title> My JavaScript</title>
<script language = "javascript" src = testMethod.js"></script>
</head>
<body>
<script language = "javascript" type = "text/javascript">
// var getValue = testMethod(2,3);
document.write("The result is " + testMethod(5,3));
</script>
<noscript>
<h3> This site requires JavaScript</h3>
</noscript>
</body>
</html>
The code is not implementing the result at all. It shows only a blank page browser.
It seems you have a quote missing in the html, it should say src="testMethod.js" where you are including the script in the first place.
I'm trying to replace the context of a loaded element with the following code:
$("#menu1").load("./templates/menu.html");
var str = $("#menu1").html();
alert(str);
str.replace("[number]", "1");
$("#menu1").replaceWith(str);
But I always get an empty str, the menu1 gets loaded correctly with the menu.html content so I have no idea what's going on.
You need to test the string after the load - the way you have done it, it can be run whilst the load is still going. Try this:
$("#menu1").load("./templates/menu.html", function() {
var str = $("#menu1").html();
alert(str);
str.replace("[number]", "1");
$("#menu1").replaceWith(str);
});
More information
$("#menu1").load("./templates/menu.html", function(){
//complete
var str = $("#menu1").html();
});
Loading part isn't instant so you are trying to apply var str = $("#menu1").html(); before load() has time to complete
You need to do the task in callback function of load:-
For example :-)
<!DOCTYPE html>
<html>
<head>
<script data-require="jquery#*" data-semver="2.1.3" src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
Hello this is rachit 1 1 1 1.
<div id="menu1"></div>
<script>
$("#menu1").load("index.html", function() {
var str = $("#menu1").html();
alert(str);
str.replace("[number]", "1");
$("#menu1").replaceWith(str);
});
</script>
</body>
</html>
Plunker
I copy some javascript example form jsfiddle and run them on local server but it shows the error on google chrome at inspect_element/console. Any suggestions for fixing this? Thanks.
error:
Uncaught TypeError: Object #<HTMLDocument> has no method 'getElementByName'
compute onclick
my code:
<!DOCTYPE html>
<html>
<head>
<title>My fruit</title>
<script type="text/javascript">
function checkFruit(){
var fruit_radio_pointers = document.getElementsByName("fruit");
var which_fruit = null;
for(var i=0; i<fruit_radio_pointers.length; i++){
if(fruit_radio_pointers[i].checked){
which_fruit = fruit_radio_pointers[i].value;
break;
}
}
alert(which_fruit);
}
document.getElementById("my_btn").addEventListener("click", checkFruit, false);
</script>
</head>
<body>
<p>
<button id="my_btn">Which Fruit?</button>
</p>
</body>
</html>
Names do not enforce uniqueness in html, so the function is getElementsByName (note the s after Element). When you change this, remember it will return an array, not one element.
<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.