I want to replace a big section of html code (the content on the side) with other html code.
I have used the JavaScript code below:
document.getElementById("content").innerHTML = "";
The thing is that I can't replace html code that got the "" characters in it. Can someone please help me?
I should ad that I´m a beginner with JavaScript and as of now I want to stay away from jQuery.
try with
document.getElementById("content").innerHTML ='text to be replaced with "" ';
or with
document.getElementById("content").innerHTML ="text to be replaced with \"\"";
or with
document.getElementById("content").innerHTML ='text to be replaced with \'\'';
( \" is the escape sequence for the character " and in javascript you can use either ' or " for the strings)
here the jsfiddle sample
Related
I am working on an asp.net project and on part of it, I needed to print a part of the page which contains an image using javascript. After some digging, I found a code and it works fine.
<script>
function VoucherSourcetoPrint(source) {
return "<html><head><script>function step1(){\n" +
"setTimeout('step2()', 10);}\n" +
"function step2(){window.print();window.close()}" +
"\n</scri" + "pt></head><body onload='step1()'>\n" + <%-- lokk this line--%>
"<img src='" + source + "' style='width: 300px; height: 150px; margine:none;' /></body></html>";
}
function VoucherPrint(source) {
Pagelink = "about:blank";
var pwa = window.open(Pagelink, "_new");
pwa.document.open();
pwa.document.write(VoucherSourcetoPrint(source));
pwa.document.close();
}
</script>
As you can see on the first function it is returning a string and in that there is a script closing tag, which is written as </scri" + "pt>, first I thought it was a mistake and tried removing the extra quotes and plus sign and then the string showing error.
I am confused, why is it have to be like </scri" + "pt>??
Because the HTML parser will find the sequence of characters "</script>" and end the script element. It will then pass invalid JavaScript to the JavaScript parser.
Here is a simplified example:
<script>console.log("</script>");</script>
▲ ▲ ▲▲ ▲ ▲
1 2 34 5 6
Script element start tag.
Begining of JavaScript (console.log(" - not a compilable script).
End of JavaScript
Script element end tag
Text to show as plain text in the HTML document (");)
Script end for with no matching open tag. Discarded by HTML parser as an error.
A more elegant approach is to just escape the /:
<script>console.log("<\/script>");</script>
I'm trying to add the script tag using JavaScript. I'm putting the tag into double quote.
My problem is something else but solves with this simple example:
<script>
document.write("<script>" + example_var + "<\/script>");
</script>
How can I fix it?
You can use two ways
Template Literals
document.write(`"<script>"${example_var}"<\/script>"`);
Single Quotes
document.write('"<script>"' + example_var + '"<\/script>"');
right now i m using \n to enter line break
document.getElementById('inputarea').value = "<h1>\nHead\n</h1>";
i want to write code as
document.getElementById('inputarea').value = "
<h1>
Head
</h1>";
but its not working. I m displaying value in text area.
suggest any way to display code without using \n or br.
Use HTML Backtick (`) instead
Check the snippet
document.getElementById('inputarea').value = `<h1>
Head
</h1>`;
<textarea id="inputarea"></textarea>
I'm using phonegap to share an article via WhatsApp.
The code for the button is as follows:
shareArticle += '<li class="rrssb-whatsapp"><a href="javascript: void(1)" onclick="window.plugins.socialsharing.shareViaWhatsApp(\''+$('.article_title').html().replace(/'/g, "'")+'\', null, \'http://www.myaddress.com/showArticle-'+articleId+'\', function() {console.log(\'share ok\')}, function(errormsg){alert(errormsg)});" class="popup" data-action="share/whatsapp/share">';
shareArticle += '<span class="rrssb-icon"><!-- Icon in SVG --></span>';
shareArticle += '</a></li>';
The part that I'm asking about is this:
onclick="window.plugins.socialsharing.shareViaWhatsApp(\''+$('.article_title').html().replace(/'/g, "'")+'\', null, \'http://www.myaddress.com/showArticle-'+articleId+'\', function() {console.log(\'share ok\')}, function(errormsg){alert(errormsg)});"
The button is not working when there is an apostrophe in the title.
The strangest thing is that if I replace ' with " it work perfectly (even thought the result is wrong).
Doe's anybody has any idead why ' fails?
Thank you all for your support.
The solution is to change the apostrophe to another sign that doesn't break the string.
So what I did is:
$('.opinion_content_title').html().replace(/'/g, "′")
Again, thank you all.
The named character reference ' (the apostrophe, U+0027) was introduced in XML 1.0 but does not appear in HTML. Authors should therefore use ' instead of ' to work as expected in HTML 4 user agents.
I have some javascript that will turn
[b]test[/b]
into
<span class="bbcode_bold">test</span>
Also, this
[i]test 2[/i]
will turn into
<span class="bbcode_italic">test</span>
That works fine, but I need to be able to decode that as well. With multiple types of bbcode that all have the same end tags, how can I figure a way to identify end tags as the bbcode tag they started as?
Edit: The code I use to parse text with bbcode in it:
function bbencode(input){
return input
.replace(/\n/ig, '<br/>')
.replace(/\[b\]/ig, '<span class="bbcode_bold">')
.replace(/\[\/b\]/ig, '</span>')
.replace(/\[i\]/ig, '<span class="bbcode_italic">')
.replace(/\[\/i\]/ig, '</span>')
;
}
My problem is in the decoder:
function bbdecode(input){
return input
.replace(/\n/ig, "<br/>")
.replace(/<span class="bbcode_bold">/ig, "[b]")
.replace(/<\/span>/ig, "[/b]")
.replace(/<span class="bbcode_italic">/ig, "[i]")
.replace(/<\/span>/ig, "[/i]")
;
}
Every span end tag needs to be interpreted as the bbcode end tag that it started as but there's no way to tell the difference. I tried putting classes in the end tags but firefox doesn't allow it.
You could put a comment after the tag then test for that, e.g.:
function bbencode(input){
return input
.replace(/\n/ig, '<br/>')
.replace(/\[b\]/ig, '<span class="bbcode_bold">')
.replace(/\[\/b\]/ig, '</span><!--BOLD-->')
.replace(/\[i\]/ig, '<span class="bbcode_italic">')
.replace(/\[\/i\]/ig, '</span><!--ITALIC-->')
;
}