See my code
<html>
<body>
<script type="text/javascript">
var str="Visit Microsoft!";
document.write( str = str.replace("",'ss'));
</script>
</body>
</html>
The output is
ssVisit Microsoft!
Why is it happening.?
This is correct because every string begins with an empty string. See below post for more info:
Why does "abcd".StartsWith("") return true?
Related
[
{
docPath: "c:\Project\Indofebweb\Attachment\images\indofab.png",
}
]
I want to split the string from \Attachment and get the rest of the string e.g
\Attachment\images\indofab.png . How can I do this?
For this particular string you can use :
var arr=test.split("Indofebweb");
var restString=arr[1]; //-----contains string \Attachment\images\indofab.png
You can make your own logic using split() function.
UPDATED CODE----TRY THIS----BELOW HTML
<!DOCTYPE html>
<html>
<body>
<p> a string display :</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "c:\\Project\\Indofebweb\\Attachment\\images\\indofab.png";;
var arr=str.split("Indofebweb");
document.getElementById("demo").innerHTML = arr[1];
}
</script>
</body>
</html>
use str.match()
path.match(/\\Attachment.*/);
this will return everything after and including "\Attachment". Keep in mind that backslashes need to be escaped.
I am new to javascript, and today i was trying my first example as shown below in the code section. I am using an editor called "Free Javascript Editor".
when I run the code, the browser starts and the text between the tags is displayed but the length of the string is never shown.
am I using it wrong?? please let me know how to do it correctly
lib
compile 'io.reactivex.rxjava2:rxjava:2.0.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
code:
<html>
<head>
<title>Title of the home pahe</title>
</head>
<body>
<script>
var str = new string ("MyString");
str.length;
</script>
<h2>My First JavaScript</h2>
</body>
</html>
Use Onload event and put it inside js function.
<body onload="myFunction()">
<script>
function myFunction() {
var str = ("MyString");
var n = str.length;
document.getElementById("printlength").innerHTML = n;
}
</script>
<h2>My First JavaScript</h2>
<p id="printlength"></p>
</body>
Use document.createElement
var str = "MyString";
var p = document.createElement("p");
p.textContent = str.length;
document.body.appendChild(p);
Scripts are not rendered by the browser, only executed. You can, however, do something like this:
<html>
<head>
<title>Title of the home pahe</title>
</head>
<body>
<h2>My First JavaScript</h2>
<p id="theLength"></p>
<script>
// No need to invoke the string constructor here.
var str = 'MyString';
// Find our placeholder element and set the textContent property.
document.getElementById('theLength').textContent = str.length;
</script>
</body>
</html>
It's good practice to put your script tags at the end of the body element - that way all of the HTML should render before the scripts are executed.
You should assign the length of your string to a variable. Then, you can show it.
<span id="stringLength"></span>
<script>
var str = "MyString";
var length = str.length;
document.getElementById('stringLength').textContent = 'Length: ' + length; // Show length in page
console.log('Length: ' + length); // Show length in console
alert('Length: ' + length); // Show length as alert
</script>
It must be String, not string. Code below works.
var str = new String ("MyString");
str.length;
Changed your code to this:
<html>
<head>
<title>Title of the home pahe</title>
</head>
<body>
<script>
var str = "MyString";
console.log(str.length);
</script>
<h2>My First JavaScript</h2>
</body>
</html>
Then you must look in the developer console for the output, here is how:
Google Chrome
FireFox
Safari
Why doesn't the following work?
<!DOCTYPE html>
<html>
<body>
<script>
var str="{"bmi":"25.25"}";
var unesc = decodeURIComponent(str);
document.write(unesc);
var obj = JSON.parse(unesc);
document.write(JSON.stringify(obj));
</script>
</body>
</html>
https://jsfiddle.net/p4c4q6q1/1/
Looks like JSON.parse() is not liking the output of decodeURIComponent() for some reason. If I manually replace the string reference with the actual string then JSON.parse() works.
I need to pass a value from a bean into a JavaScript part of a HTML page
<script language="javascript" for="obj" event="ControlInitialized>
obj.URL = #{myBean.ObjectURL};
</script>
where #{myBean.ObjectURL} is :
http://localhost:8080/project/descript.xsd
Always, always look at the generated output.
obj.URL = http://localhost:8080/project/descript.xsd;
Notice anything missing?
obj.URL = "http://localhost:8080/project/descript.xsd";
It must be a string:
<script language="javascript" ">
objURL = "#{myBean.ObjectURL}";
</script>
Try this:
<script language="javascript" > //Remove the extra quote
obj.URL = "#{myBean.ObjectURL}"; // Put it in quotes
</script>
I have this text in javascript:
'This is a %{component} with a %{value}'
I should replace all words that look like %{\w+} with <span>word</span>
The final result should look like:
'This is a <span>component</span> with a <span>value</span>'
As already written use String.replace and a RegEx:
var originalString = 'This is a %{component} with a %{value}';
var replacedString = originalString.replace(/%\{(\w+)\}/g, '<span>$1</span>');
// "This is a <span>component</span> with a <span>value</span>"
Is this what you are looking for?
<!DOCTYPE html>
<html>
<body>
<p id="demo">This is a %{component} with a %{value}</p>
<button onclick="myFunction()">Test</button>
<script>
function myFunction()
{
var str=document.getElementById("demo").innerHTML;
var n=str.replace("%{","< span >").replace("}","< /span >");
document.getElementById("demo").innerHTML=n;
}
</script>
</body>
</html>
You can do that with regex in javascript / jquery.
Have a look here: http://de.selfhtml.org/javascript/objekte/regexp.htm