jQuery load txt file .html() - c#

how can I decode the html contents of the .txt file when using the $('myDiv').load('mytxtFile.txt')?
I know: $('<div/>').html(value).text();
In other words, How do I combine these two?
important note: the mytextFile.txt, is the container of some of codes which jenerated with ckeditor, and i want to show the content of this txt file in my html form as a html form.
the codes in my txt file is:
<p>hellohome</p>
and I want to show it as : sss hello home sss
in my html page
tank you for your attantion

If I understand the question correctly you can do this:
$('myDiv').load('mytxtFile.txt', function(text) {
$(this).text(text);
});

I think you're after something like this:
$('<div/>').load('mytextFile.txt', function(textStr) {
var htmlStr = $(this).html(textStr).text();
$(this).html(htmlStr);
}).appendTo('form');
That'll effectively encode the html entities, return a properly formed htmlStr, which you can use to set the html() of the element.
Here's a demo fiddle

You can't. load() is designed to pull fragments of HTML directly into the document. If you want to preprocess the data, then load it using $.ajax instead and write your own logic for updating the element content with success.
success: function (data) {
$('myDiv').text(data); // Note: Your selector but not one that is valid for an HTML document
}

You can load the textfile with $.ajax and then add the text with "$('#YOURDIV').text(data)"
Example:
Fiddler: http://jsfiddle.net/ZXMha/
HTML:
JavaScript:
$.ajax({
url: '/echo/html/',
type: "POST",
data: {
html: "<p>Text echoed back to request</p>" + "<script type='text/javascript'>$('target').highlight();<\/script>",
delay: 0
},
success: function(data){
$('#text').text(data);
}
})
Output:
<p>Text echoed back to request</p><script type='text/javascript'>$('target').highlight();</script>
Note: Post is used in this example to simulate an ajax call where html is returned. See http://doc.jsfiddle.net/use/echo.html for Fiddler echo HTML Options.
For your second question: you can simply post the text with .html(data) into your div.
Code:
$.ajax({
url: '/echo/html/',
type: "POST",
data: {
html: "<p>hellohome</p>",
delay: 0
},
success: function(data){
$('#text').html(data);
}
})
Fiddler: http://jsfiddle.net/KWpUC/

Related

Converting string back to HTML

I took a HTML string from tinyMCE and wrapped it with jQuery html(). I am now trying to unwrap it and display it in the the view.
I get tinyMCE contents and put them into to a div. I then extract html contents of that div and pass it to my database via AJAX. I am now trying to convert the contents back into format that will allow me to display it properly.
<textarea name="tinycontent" class="form-control"
id="textFromContentEditor" value=""></textarea>
<div id="textContent" style="display:none"></div>
var sectionContent = tinyMCE.activeEditor.getContent();
sectionContent = $('#textContent').text(sectionContent).html();
$.ajax({
url: '/Home/CreateNoteContent',
async: false,
type: 'POST',
data: {
headerID: headerID,
categoryID: categoryID,
sectionContent: sectionContent
},
success: function (result) {
if (result == false) {
toastr.error("Cannot create new content")
} else {
toastr.success("Successfully created new content")
$("#feed-" + releaseID).trigger('click');
jumpTo(scrollPos);
}
}
});
My input into tinyMCE: Hello World
What gets stored in the db: <p>Hello World</p>
I want to be able to display just the text contents in the view.
I am certainly not very good at js, ajax and so on, but this is what I did in my project to display html in one of my divs from the pages -tag.
var blobs = document.getElementById('your-div-Id').innerHTML = "<h4>somethingRandom</h4>";
OBS! I believe this is javascript and therefor might not be the right solution.
Hope this helps!

Anglesharp Parse after clicking <a> which calls JavaScript function and loads additional data to page

I'm trying to parse html page that have link which loads additional data to current page.
<a href="#" data-href="/some-url/">
There a javascript function that calls after clicking on <a href="#">.
I'm able to parse data that already loaded with page, but can't parse additional block that loads by click on <a>. How can I parse all of the data including post-load data using AngleSharp?
JS code:
$('.button_add a').click(function (e) {
e.preventDefault();
var link = $(this);
var page = link.data('page');
$.ajax({
method: 'get',
url: link.data('href'),
data: {page: page},
success: function(data) {
var linkParent = link.parent();
var parentBlock = linkParent.parent();
parentBlock.children('.items').first().append(data.view);
}
});
});
Make sure to allow AngleSharp to load JS files.
var config = Configuration.Default
.WithDefaultLoader(new LoaderOptions { IsResourceLoadingEnabled = true })
.WithJs();
Keep in mind that AngleSharp.Js may not be able to deal with the complexity of the given script. Check the debug output for any exceptions.

Passing Large Html Content to API Controller in Asp.net MVC

I have created an api controller which is supposed to receive some html content. When the call fires, chrome shows the status of PUT request as pending for a looong long time.
Initially i thought the problem is with the html tags but once i tried small tags i discovered it worked.
Now i am guessing the problem is with the size of content which is the content of a full html page.
here is the code:
<script>
$("#createHtmlElement").click(function (event) {
$.ajax({
url: "/api/PageElements/#Model.Id",
contentType: 'application/json; charset=utf-8',
type: "PUT",
data: JSON.stringify({
Content: $("#newHtmlContent").val()
}),
success: function (html) {
}
});
});
</script>
here is the c# code for the API:
public class PageElementsController : ApiController
{
[ValidateInput(false)]
public void Put(int id, HtmlElement value)
{
HtmlContent contents = new HtmlContent(value.Content);
this.htmlElementBL.SaveHtmlContentElements(contents, id);
}
}
I need to either change my approach as to how i am passing data or find out what is the problem that it is not working with the large content.

Cannot pass large json string to another Page with asp.net c# and knockoutjs

I got a problem while working with knockoutJs and asp.net c#.
When I passed a json string from page to another popup page using jquery ajax and knockoutJs for printing.
The problem occur:
When the Json string is small. It works fine, the popup page shows string data in table.
However, when the json string is large. It doesn't work anymore. The error occurs with message:
"Uncaught Sys.ArgumentException: Sys.ArgumentException: Cannot deserialize. The data does not correspond to valid JSON.
Parameter name: data"
Here is my code:
$.ajax({
type: "POST",
url: 'InProgressBrief.aspx/PrintReport',
data: ko.toJSON({ reportData: ko.toJSON(InProgressBriefs.Briefs) }),
async: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
window.open("PrintInProgressBrief.aspx?month=" + InProgressBriefs.Month() + "&year=" + InProgressBriefs.Year(), "", "status=0,toolbar=0,width=1000,height=900");
}
})
Here is my webMethod
[WebMethod]
public static void PrintReport(string reportData)
{
PSCDialog.DataShare = reportData;
}
The popup page recieves the Json tring:
if (PSCDialog.DataShare != null)
return PSCDialog.DataShare as string;
In the popup page UI, I set the knockoutjs variable, my UI javascript code is something like follow:
var InProgressBriefs = {
Briefs: ""
;
$(function(){
InProgressBriefs.Briefs = Sys.Serialization.JavaScriptSerializer.deserialize('<%=ReportJSONData%>');
ko.applyBindings(InProgressBriefs, $('#mainDivPrint')[0]);
})
Would anyone please tell me what is the problem here? I will appreciate your help alot.
Thank you in advance.
One thing that's a little suspicious is the line
data: ko.toJSON({ reportData: ko.toJSON(InProgressBriefs.Briefs) }),
When that's deserialized, you wind up with reportData being a JSON-encoded string rather than a native object. I doubt that's what you want. More likely what you need is
data: ko.toJSON({ reportData: ko.toJS(InProgressBriefs.Briefs) }),
which serializes only once.

How to call aspx page from javascript with query string

Currently the functionality of a web app I am designing(I can not tell real details) is as follows :
alert('Hi');
var args = ShowModalDialogue(sURL,'','');
if(args[0] == 'Pass')
alert('Bye');
Now I want to replace ShowModalDialogue with ModalPopupExtender. But the problem is I don't know how I can call aspx page using Javascript/Jquery and how to return values to calling javascript in the form of array?
Can anyone please help me out?
Thanks in advance.
You can use jquery to make an ajax call to your aspx.
var query = 'value1';
$.ajax({
type: "POST",
url: "my_asp_file.aspx?q="+query,
async: true,
dataType: "json",
data: data,
success: function(response) {
var js_object = JSON.parse(response);
},
error: function() {
}
});
Then you can use JSON.parse() to convert the returned JSON element to a JS Object. Remember to convert the output of your query in ASPX to a JSON object (in your ASPX file).

Categories

Resources