Large Html is displaying as Raw Html - html

I have a raw Html which I get from backend and I have saved in the javascript variable. It is huge. This is my code. I have used struts property to set them. The value is coming, I have checked the struts property value.
var largeHtml= "<s:property value='ss' />";
document.getElementById("content").innerHTML = largeHtml;
When I changed that code to
var su = "<s:property value='ss' />";
document.getElementById("content").innerHTML = "<p>Test</p>";
It is working. But for the largehtml, It is displaying raw html. Thanks in advance.

You should try to escape simple quotes like this :
var largeHtml= "<s:property value=\'ss\' />";
Hope it helps !

Related

Json parsing fails out of the blue

So I've been struggling with JSON for awhile now, however last night something weird happened, even tho I have " escaped it brings up an error, here's my JSON string
var data = $.parseJSON('{"rows":[{"type":"row","width_class":"row new_row","column_class":"col3 column_model","columns":{"0":{"class":"column one","children":[]},"1":{"class":"column one","children":[{"type":"bullet-block","html":"<div class=\\"bullet-block-element\\"><ul><li style='padding-left:36px;background-image:url(\\"http://example.com/includes/images/bulletins/large-0.png\\");'>123</li><li style='padding-left:36px;background-image:url(\\"http://example.com/includes/images/bulletins/large-0.png\\");'>456</li><li style='padding-left:36px;background-image:url(\\"http://example.com/includes/images/bulletins/large-0.png\\");'>789</li></ul></div>","image":"http://example.com/includes/images/bulletins/large-0.png","size":"large","items":["123","456","789"]}]},"2":{"class":"column one","children":[]}}}]}');
This is generated via
var data = $.parseJSON('<?= str_replace('\\','\\\\',base64_decode($data['d'])) ?>');
Am I just being blind or have I had too much redbull? Help would be appreciated!
json_encode does the escaping and it will automatically be exposed as JSON, you don't need $.parseJSON, it's double decoding there.
Simply use this:
<?php
$php = array('test' => 'hi');
$data['d'] = base64_encode(json_encode($php)); // 'eyJ0ZXN0IjoiaGkifQ=='
?>
<script>
var data = <?php echo base64_decode($data['d']); ?>;
console.debug(data.test); // Prints 'hi' in the console ;-)
</script>
See the codepad: http://codepad.org/VmKGt0JD
you need to escape the ''s as well (styles in the html tags)
so this will work
var data = $.parseJSON('{"rows":[{"type":"row","width_class":"row new_row","column_class":"col3 column_model","columns":{"0":{"class":"column one","children":[]},"1":{"class":"column one","children":[{"type":"bullet-block","html":"<div class=\\"bullet-block-element\\"><ul><li style=\'padding-left:36px;background-image:url(\\"http://example.com/includes/images/bulletins/large-0.png\\");\'>123</li><li style=\'padding-left:36px;background-image:url(\\"http://example.com/includes/images/bulletins/large-0.png\\");\'>456</li><li style=\'padding-left:36px;background-image:url(\\"http://example.com/includes/images/bulletins/large-0.png\\");\'>789</li></ul></div>","image":"http://example.com/includes/images/bulletins/large-0.png","size":"large","items":["123","456","789"]}]},"2":{"class":"column one","children":[]}}}]}');
copied and pasted to a fiddle

Passing a javascript array into <datalist> form tag

I'm reading a csv file with the javascript File API. I want to display those data in a datalist ( html5 tag). I'm using the same code as in http://www.html5rocks.com/en/tutorials/file/dndfiles/. The array that is holding the csv data is named datacsv. I just don't know how to pass the array (datacsv[] at this case) into the datalist tag, in order to display the data. Any thoughts would be greatly appreciated, Thanks
You could try using jQuery:
var datacsv = ['example', 'of', 'some', 'data'];
for(var i in datacsv){
$('#my-datalist').append('<option value=' + datacsv[i] + '>');
}
In response to your comment, jQuery is a useful library for the client-side JavaScript language, so it will work off-line. If you're looking for a non-jQuery way of doing this, you can use this code:
var datacsv = ['example', 'of', 'some', 'data'];
for(var i in datacsv){
var datalist = document.getElementById('my-datalist');
datalist.innerHTML += '<option value=' + datacsv[i] + '>';
}

jQuery - Parse Json then Display Data

I'm having a problem parsing valid Json from a Twitter List then displaying the list on the page.
Here is my code;
var url = "http://api.twitter.com/1/aplusk/lists/5676047/statuses.json&callback=?";
$.getJSON(url, function(data) {
var results = '';
$(data.results).each(function() {
results += "<p class='tweet_result' id='tweet" + this.id_str + "'><a href='http://twitter.com/" + this.user.screen_name + "' title='' class='tweet_user'></p>";
});
$(results).prependTo("#twitter_results");
});
If you put the url in to www.jslint.com you can view the structure of the json
I'm new to json so I could be doing something stupid here.
Thanks in advance for your help and advice.
The URL has to be:
http://api.twitter.com/1/aplusk/lists/5676047/statuses.json?callback=?
(Note the question mark instead of the ampersand)
Also see the returned object, it does'nt have a member "results", it's a native javascript-array.
You'll have to iterate over data itself:
$(data).each(function(i,item)
where you can access the properties inside via
item.someProperty

Get text from XML file and print it in HTML File

I have a simple XML file created in R that consists of the following lines:
<statistics>
<mean>15.75</mean>
<sd>2.83</sd>
</statistics>
I want to extract the mean and sd to a HTML page, that has a Flash graph and I would like this underneath:
Statistics
Mean = 15.75
Standard Deviation = 2.83
What is the easiest way to achieve this?
Regards,
Anthony.
You should use PHP and SimpleXml.
Just load your xml with simplexml:
$xml = new SimpleXMLElement(file_get_contents("statistics.xml"));
and afterwards echo the desired elements to the page (or add them to your template engine):
echo "Mean: ".$xml->statistics[0]->mean."<br />";
echo "Standard Deviation: ".$xml->statistics[0]->sd."<br />";
If you have more then one statistics element, for example:
<statistics>
<mean>15.75</mean>
<sd>2.83</sd>
</statistics>
<statistics>
<mean>25.75</mean>
<sd>28.3</sd>
</statistics>
Simply use a foreach loop to iterate trough each element:
foreach ($xml->statistiscs as $statistic) {
echo "Mean: ".$statistics->mean."<br />";
echo "Standard Deviation: ".$statistics->sd."<br />";
}
jQuery is one way to do it. This will dynamically load the xml file on the client. The downside is that you have to include the library which you can download here: www.jquery.com. It can be used to so many other things so look into it. Your code would be something like:
$(document).ready(function () {
$.get('address/to/xml.file', function(data) {
var mean = $(data).find('mean').text();
var sd = $(data).find('sd').text();
$("somecontainer").text(mean + "|" + sd);
});
});
Or do you want some server-side language to do your xml parsing and printing?

Sending values through links

Here is the situation: I have 2 pages.
What I want is to have a number of text links(<a href="">) on page 1 all directing to page 2, but I want each link to send a different value.
On page 2 I want to show that value like this:
Hello you clicked {value}
Another point to take into account is that I can't use any php in this situation, just html.
Can you use any scripting? Something like Javascript. If you can, then pass the values along in the query string (just add a "?ValueName=Value") to the end of your links. Then on the target page retrieve the query string value. The following site shows how to parse it out: Parsing the Query String.
Here's the Javascript code you would need:
var qs = new Querystring();
var v1 = qs.get("ValueName")
From there you should be able to work with the passed value.
Javascript can get it. Say, you're trying to get the querystring value from this url: http://foo.com/default.html?foo=bar
var tabvalue = getQueryVariable("foo");
function getQueryVariable(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++)
{
var pair = vars[i].split("=");
if (pair[0] == variable)
{
return pair[1];
}
}
}
** Not 100% certain if my JS code here is correct, as I didn't test it.
You might be able to accomplish this using HTML Anchors.
http://www.w3schools.com/HTML/html_links.asp
Append your data to the HREF tag of your links ad use javascript on second page to parse the URL and display wathever you want
http://java-programming.suite101.com/article.cfm/how_to_get_url_parts_in_javascript
It's not clean, but it should work.
Use document.location.search and split()
http://www.example.com/example.html?argument=value
var queryString = document.location.search();
var parts = queryString.split('=');
document.write(parts[0]); // The argument name
document.write(parts[1]); // The value
Hope it helps
Well this is pretty basic with javascript, but if you want more of this and more advanced stuff you should really look into php for instance. Using php it's easy to get variables from one page to another, here's an example:
the url:
localhost/index.php?myvar=Hello World
You can then access myvar in index.php using this bit of code:
$myvar =$_GET['myvar'];
Ok thanks for all your replies, i'll take a look if i can find a way to use the scripts.
It's really annoying since i have to work around a CMS, because in the CMS, all pages are created with a Wysiwyg editor which tend to filter out unrecognized tags/scripts.
Edit: Ok it seems that the damn wysiwyg editor only recognizes html tags... (as expected)
Using php
<?
$passthis = "See you on the other side";
echo '<form action="whereyouwantittogo.php" target="_blank" method="post">'.
'<input type="text" name="passthis1" value="'.
$passthis .' " /> '.
'<button type="Submit" value="Submit" >Submit</button>'.
'</form>';
?>
The script for the page you would like to pass the info to:
<?
$thispassed = $_POST['passthis1'];
echo '<textarea>'. $thispassed .'</textarea>';
echo $thispassed;
?>
Use this two codes on seperate pages with the latter at whereyouwantittogo.php and you should be in business.