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.
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.
My localStorage returns null, and I don't know why, the read function is not used, but just for help I put it anyway. Chrome says that it cannot ste innerHTML into null, and my troubleshooting alert info also returns null, but the code goes to the end. Any help would be useful, thank you. The cookie:
<!doctype html>
<html>
<head>
<cookie>
<script>localStorage.setItem('id',Math.floor(Math.random()*10))
</script>
</cookie>
</head>
<body>
<script>var id = localStorage.getItem('id')
alert(id)</script>
</body>
</html>
The Script:
<!doctype html>
<html>
<head>
<script>
var theId=localStorage.getItem('id')
function change(id,target){
var info = localStorage.getItem(id);
alert(info)
document.getElementById(target).innerHTML=info;
}
function read(){
var element=document.createElement('h1')
element.innerHTML='You Want To Read'
document.body.appendChild(element)
alert('debug')
}
</script>
</head>
<body>
<input type="button" value="Read" name="read_story" id="read_story"
onclick=read();change(theId,info)>
<p id='info'>initial</p>
</body>
<script>
alert('debug');
</script>
</html>
You've misinterpreted the error message.
If it can't set the innerHTML of null then you have something.innerHTML = a_value. It is the something that is null not anything to do with local storage.
In change(theId,info), info is a variable. It is a reference to the element with id="info".
You use it here: document.getElementById(target).
info (now target) gets converted into a string ("[object HTMLParagraphElement]").
There is no element with id="[object HTMLParagraphElement]", so you get null.
When you call the function, you need to pass a string, not a variable.
How can I get JSON data embedded in a script tag?
<!DOCTYPE html>
<html>
<head>
<script id="data" type="application/json">{org: 10, items:['one','two']}</script>
<script type="text/javascript">
var obj = JSON.parse( document.getElementById('data').value );
console.log( obj );
console.log( obj.org );
</script>
</head>
<body>
</body>
</html>
I'm getting:
Uncaught SyntaxError: Unexpected token u in JSON at position 0
<script> elements are not form controls. They don't have value properties (so. when you read it, you get undefined which is "undefined" when cast to a string, and that isn't valid JSON).
Read the content of the text node inside the element instead.
You also need to write JSON instead of a JavaScript object literal.
Property names must be strings, not identifiers.
Strings must be quoted with " not '.
<script id="data" type="application/json">{"org": 10, "items":["one","two"]}</script>
<script type="text/javascript">
var obj = JSON.parse(document.getElementById('data').firstChild.data);
console.log(obj);
console.log(obj.org);
</script>
The u comes from "undefined". Try:
JSON.parse( document.getElementById('data').innerHTML );
...but keep in mind that your current input is not JSON. So correctly formatted it would be:
<script id="data" type="application/json">{"org":10,"items":["one","two"]}</script>
I'm trying to replace html using innerHTML javascript.
From:
aaaaaa/cat/bbbbbb
To:
Helloworld
This's my code
<html>
<head>
</head>
<body>
<p id="element1">aaaaaa/cat/bbbbbb</p>
<script language="javascript">
var strMessage1 = document.getElementById("element1") ;
strMessage1.innerHTML = strMessage1.innerHTML.replace( /aaaaaa./g,'<a href=\"http://www.google.com/') ;
strMessage1.innerHTML = strMessage1.innerHTML.replace( /.bbbbbb/g,'/world\">Helloworld</a>') ;
</script>
</body>
</html>
When i run this code it disappears Helloworld hyperlink.
what I'm doing wrong. Please help.
Thank you for all your help.
You should chain the replace() together instead of assigning the result and replacing again.
var strMessage1 = document.getElementById("element1") ;
strMessage1.innerHTML = strMessage1.innerHTML
.replace(/aaaaaa./g,'<a href=\"http://www.google.com/')
.replace(/.bbbbbb/g,'/world\">Helloworld</a>');
See DEMO.
You are replacing the starting tag and then putting that back in innerHTML, so the code will be invalid. Make all the replacements before you put the code back in the element:
var html = strMessage1.innerHTML;
html = html.replace( /aaaaaa./g,'<a href=\"http://www.google.com/');
html = html.replace( /.bbbbbb/g,'/world\">Helloworld</a>');
strMessage1.innerHTML = html;
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?