I am trying to assign to a javascript variable some html script, but cannot succeed. Here is what I am trying:
var blah = #Html.Raw("Some <strong>text<\/strong>");
I cannot even say what the error is, as the result I get looks cryptic:
Thanks
Html.Raw is used to keep a fragment of html from being encoded and displayed as plain text. You're not using it with actual html, you're using escaped text. try adding single quotes inside the double quotes:
<script>
var plork = #Html.Raw("'Some <strong>text</strong>'");
alert(plork);
</script>
OR
putting the Html.Raw inside single quotes:
<script>
var plork = '#Html.Raw("Some <strong>text</strong>")';
alert(plork);
</script>
instead
Related
Currently I use $('div#edit').froalaEditor('html.get') to retrieve the html data inside editor, but it sucks when it comes to process / store the text data in my backend because of all the p tags and &npsb; symbols in raw html string.
To be honest they do not even pass to the database without losing parts of the data. Is there a way or an api to directly extract the text data as a string with proper symbols like "\n\t" from the froala editor?
If you dont like p tags you should use the enter option:
$('div#froala-editor-br').froalaEditor({
enter: $.FroalaEditor.ENTER_BR
});
If you really would like to remove all tags you could use jQuery text method
jQuery($('div#edit').froalaEditor('html.get')).text()
Or you could use HTML options
var html = $('div#edit').froalaEditor('html.get') ;
var div = document.createElement("div");
div.innerHTML = html;
alert(div.innerText);
There is http://textversionjs.com/ library which can be used to convert HTML to plain text. It also keeps new lines.
<script src="textversion.js"></script>
<script>
var html = $('div#edit').froalaEditor('html.get');
var text = createTextVersion(html);
</script>
Honestly, the easiest way to do this is
var body = $('#body').froalaEditor('html.get');
alert($(body).text());
You can use stringify() method in JSON to convert the HTML form to a text form.
<script>
var html = $('div#edit').froalaEditor('html.get');
var text = JSON.stringify(html)
</script>
And you can store them in any backend and use them later.
var html = '#Html.Partial("_mypartialview")';
$("#container").html(html);
I am working in a single page application and I don't want to hide the container div element; instead I want to load the html content into my javascript object.
But I get the error:
Unterminated string constant.
I have tried various methods without any success. Any help as to why I get this error would be really appreciated.
Like this
var html = "#Html.Partial("_mypartialview").ToHtmlString().Replace("\n", "<br/>").Replace(" ", "")";
The ToHtmlString() method HTML encodes your html so the quotes won't confuse your JavaScript.
The two replace methods will remove new lines and whitespace so the content is all on one line.
Update
_mypartialview
<div>
<span data-bind="text:playerInfo.Name"> </span>
</div>
Code
var html = "#Html.Partial("_mypartialview").ToString().Replace(Environment.NewLine, "").Replace(" ", "")"
This outputs the following
var html = "<div><spandata-bind="text:playerInfo.Name"></span></div>"
Assume
<html>
<head>....</head>
<body>
.
. // Occurrences are here
.
</body>
</html>
I do
$(function()
{
$('html').html() = $('html').html().replace(/strToFind/g,'somethingElse');
});
in head, but it does't work. How I do to find and replace all occurrence of string in html document itself (not store in variable)?
Thanks
.html() is a function that returns the HTML, you can't assign to it. If you want to change the HTML of an object in jQuery, you put the new HTML as a parameter to the function:
$('html').html($('html').html().replace(/strToFind/g,'somethingElse'));
$('html').html() would return the html string , but won't set it.
You can use this function
function rep_all()
{
var str = $('html').html();
str.replace('strtofind','strtoreplace');
$('html').html(str);
}
.html() is something which writes on the html page it is not like variable so you can't assign anythig to it. you need to put the value inside it.like this:
$('html').html( $('html').html().replace(/strToFind/g,'somethingElse'));
see here:http://api.jquery.com/html/
It's been answered that the .html() is not assignable, you would have to pass the replaced content as a parameter.
Just to point out, it is very uncommon to replace the whole page's html content.
As you defined that the contents to be replaced are inside the body, you should at least (still not good) target only the body:
$('body').html($('body').html().replace(/strToFind/g,'somethingElse'))
this is the code i want to put in
var htm = "<center><div id="evp-8847b53dee625a133cfb02e88fd7bbf8-wrap" class="evp-video-wrap"></div><script type="text/javascript" src="http://convernet.evplayer.com/framework.php?div_id=evp-8847b53dee625a133cfb02e88fd7bbf8&id=Q29udmVybmV0L25ldyBjb252ZXJuZXQvVGhlIGZ1dHVyZS5tcDQ%3D&v=1333642408&profile=default"></script><script type="text/javascript"><!--
_evpInit('Q29udmVybmV0L25ldyBjb252ZXJuZXQvVGhlIGZ1dHVyZS5tcDQ=[evp-8847b53dee625a133cfb02e88fd7bbf8]');//--></script></center>"
$('#inner_slide .module').replaceWith(htm);
but its remove the script. please help
First of all, you need to escape the double-quote in the string; for example:
var myStr = "hi this is an \"inner quote\".";
Secondly, break up the string as such:
myStr += "some html: <scr"+"ipt>my script</s"+"cript>";
document.write(myStr);
That should work.
jQuery messes with script tags when you pass HTML to it.
You can use http://api.jquery.com/jQuery.getScript/ to pull in the script or use the native javascript option http://www.hunlock.com/blogs/Howto_Dynamically_Insert_Javascript_And_CSS
InnerHTML is also an option. You can check out this question Can scripts be inserted with innerHTML?
I am not sure how practical this question is , but what i am trying to do is find html inside a string and then append pre tags to it using jquery. let say i have a variable with following string.
var string = "Some normal text <html><body><div> This is the html </div> </body></html>";
Html inside the string is dynamic and can contain any tags , what i want is to find the starting and ending of html and append,prepend pre tags accordingly.
Thanks
The following code does pretty much what you want, however it does not allow html, body tags etc. But those are not allowed in pre tags anyway.
var string = "Some normal text <html><body><div> This is the html </div> </body></html> more text<p>more html content</p>";
var holder = $('<div>').html(string);
holder.children().wrap('<pre>');
//Print result to console
console.log(holder.html());
Also a jsFiddle here: http://jsfiddle.net/evBCm/1/
Add the text to dynamic html tag e.g span or div and find desired node e.g
var string = "Some normal text <html><body><div> This is the html </div> </body></html>";
$("<span/>").html(string).find('div')
Apply bellow regix..
var string = "Some normal text <html><body><div> This is the html </div> </body></html>";
var iMatches = string.match("<html>(.*)</html>");
var iHtml='<html>'+iMatches[1]+'</html>';
alert(iHtml);