Server side code encode characters automatically when used in Javascript - javascript

I have an MVC Application wherein on page load I am initializing javascript function call with a Model value received from the server.
Something like this :
myFunction('#Model.ServerUnicodeCharacters');
Now, when I set to value
Model.ServerUnicodeCharacters = 'Côte d'Ivoire'
Inside javascript I get "C &#244 ;te d&#39 ;Ivoire"
(Intentionally added space before semicolon to show the output)
Is there any way to skip this character encoding which is default in ASP.NET MVC?

try replacing:
myFunction('#Model.ServerUnicodeCharacters');
with:
myFunction('#Html.Raw(Model.ServerUnicodeCharacters)');
Taken from MSDN:
The Razor syntax # operator HTML-encodes text before rendering it to the HTTP response. This causes the text to be displayed as regular text in the web page instead of being interpreted as HTML markup.
MSDN Reference

Related

JDBC in Google Script "&" to "&"

When getting the values from MySql database in Google Spreadsheets using JDBC service and I get the ampersand character as & instead of &. I would like to know how should I make the call so I directly get & as value.
I am using the following connection: "jdbc:mysql://ip:port/server?useUnicode=true&characterEncoding=UTF-8". Also, I am using rs.getString() method to take the data from the query.
Thanks a lot!
I am not sure where you are outputting your data, but this is usually a cause:
In HTML, the ampersand character (“&”) declares the beginning of an
entity reference (a special character). If you want one to appear in
text on a web page you should use the encoded named entity “&”
This seems like a simple rule, but what about urls in HTML, javascript files, javascript in HTML, etc… Here’s a little guide to help clear up that ampersand HTML confusion:
Text in HTML:
<p>Jack & Jill ran up the hill.</p>
A link in HTML (or any HTML attribute value):
tired meme
A link in javascript:
window.location = 'http://google.com/?l=1&q=rick+roll';
If you’re using a web framework that escapes variables for you and you pass in a url as a variable into javascript, then you’ll have to make sure it doesn’t encode the ampersands. In Django, you would write something like this: window.location = '{{ url|escapejs }}';
Also, if this is inline javascript—in an HTML document, not a separate .js file—then you still shouldn’t escape the ampersand, which means the document will not validate as XHTML. Either throw it into a separate .js file or stop worrying so much about validating your code.
Inside an onclick in HTML:
<a href="#" onclick="window.location='?l=1&q=rick+roll';return false">
kablammo!
</a>
This is redundant to the second example, but worth pointing out since it’s javascript inside an attribute of an HTML tag.
Dynamically in Javascript (example using jQuery):
$('#result').text('Jack & Jill'); // .text() escapes the text for you
$('#result').html('Jack & Jill'); // .html() sets the HTML directly
document.getElementById('result').innerHTML = 'Jack & Jill';
Reference information: Click Here

Remove white spaces in html contents apart from using str.replace(/\s+/g, '');

I am developing a web application using codeigniter and am generating a letter content using html and javascript.What i do is that i get the html content using .html in javascript and assign it in a javascript variable and then remove white spaces using str.replace(/\s+/g, ' ') and then pass the content using jquery to my controller method so that i generate a PDF file format document as a response letter and then send it to user.When i post the content to my controller function and view them in a chrome console or using mozila firebug i see the post data but when i do var_dump in the controller function the content is empty and it only happens for some cases one or two.
Is there any better way i can do it without getting these few cases...
Thanks in advance,will be grateful
I have found a solution to my question
I ad to replace the white spaces with a hashtag(#) from my codeigniter view as
content_with_hashtag = content_with_hashtag.replace(/\s+/g, '#');
and then pass the data to the controller function via javascript and then to the controller i had to replace the hashtag(#) with a space as
$content_with_hashtag = $this->input->get_post('content_with_hashtag');
$content_with_hashtag = preg_replace('/#+/', ' ', $content_with_hashtag);
and i was able to generate the content in PDF format

Can I get result from javascript and use it in vb.net?

I have this code to show confirm message using vb.net, but I want to get the return result and use it in vb.net code.
Dim str As String = "<script>" &
"$('.test').live('click',function(){" &
"confirm('test');" &
"})</script>"
ScriptManager.RegisterClientScriptBlock(control, GetType(Button), "sas", str, False)
I am thinking in storing the result data in html compnent and use it in vb.net code, is there another solution ?
You can store the result in a hidden field (<input type="hidden">), then retrieve it in your code after postback.
Not directly. The server side code is outputting text down a one-way channel. The JavaScript is running on a different system. You can't get data from the JS to the server without making a new HTTP request.
See also Using Pylons global variables with JavaScript which is the same problem, just with a different language.

Is there a javascript equivalent of htmlencode / htmldecode from asp.net?

The problem is this:
You have a textbox, you type in some text, send it to the server. On another page, that value is retrieved and displayed on screen in a textbox and a label.
It's important to stop scripting attacks, and asp.net won't let you submit unsafe code, so on submit you javascript replace < with < and the same for >
When the values are retrieved from the server, they will come back with < and > which is fine for displaying in the label, but when put into the textbox, they must be replaced back to < and >
The data should be stored securely in the database as other people might use this content. From a safety point of view I'd like to call htmlencode on it then store it. It is this encoded html I'd like to display in the label on the client, but the decoded version I'd like to display in the textbox.
So what I need, is a htmldecode solution in javascript. htmlencode/decode replaces more than just < > and without a definitive list I can't create my own method. Is there a solution out there?
Instead of trying to turn a string of text into HTML and then adding it to the document using innerHTML; use standard DOM methods.
myElement.appendChild(
document.createTextNode(myString)
);

How to display a string, based on number in a form with JavaScript

I have the user entering a post-number in a form-field, and then I want to display the town with that post-number.
I have the server-side function set up, it takes a variable from the URL and returns a plain string.
As I see it, I need to get the variable from the form-field after the user has written it and the focus has left the form-field, use that number in an XMLHttpRequest call to the server and display the resulted string.
But the problem is, that I've never written any JavaScript, more complex than a Hello World. So I would like some help with writing the JavaScript for that page, any help would be appreciated.
Since you are new to JavaScript I won't recommend using any JS framework for this.
To get the value from an input box you can use
document.getElementById("txt1").value;
where txt1 is the id of the input element.
Then you can append the element value to the query string and call the server-side function. And if the response text is a plain string put that inside a div or span
document.getElementById("divTown").innerText = "response string"; // for IE
document.getElementById("divTown").textContent = "response string"; // for FF
You can get a basic understanding of AJAX and JavaScript read these
AJAX Introduction
JavaScript Tutorial

Categories

Resources