How can i make html code sample within a HTML? [duplicate] - javascript

This question already has answers here:
How to display raw HTML code on an HTML page
(30 answers)
Closed 1 year ago.
Suppose i need to write a website that gives a tutorial on html or codes.
<html><h1>Hello World</h1></html>
Then how can i write this code within it. i tried to use <code></code> and even <pre></pre> to enclose the html code so that it will not be intrepreted but it got intrepreted by the browser. So how can i display the html code within the html file in browser like a code sample..

You have to write htlm reserved code with html entities
https://www.w3schools.com/HTML/html_entities.asp
The 2 main ones
< -> <
> -> >

Related

Javascript multiple script tag are allowed? [duplicate]

This question already has answers here:
multiple versus single script tags
(5 answers)
Closed 1 year ago.
I have an html file where I see two javascript tag :
<script type="text/javascript">
var varname = .... etc
</script>
<script language ="JavaScript">
function namefunction . . . etc
</script>
is this allowed? Or can create issue with loading js in the page?
There is no problem in using multiple Script in html.
How it is loaded is depending on which on the order it is added into the html.
It is totally allowed. It shouldn't cause any issues whatsoever.

Convert text into HTML elements using jQuery [duplicate]

This question already has answers here:
jquery not working to change inner html?
(4 answers)
Closed 5 years ago.
My html file looks like this:
<div id="myDiv" class="div-sample"></div>
I have a text file which has the following text:
<b>abracadabra</b>
Now after reading the file from an ajax request(via a php script which just reads the file), when I write the following jQuery code:
$('#myDiv').innerHTML = text;
inspite of showing abracadabra it showing with the tags.
what is going on here?
The first line var text = '<b>abracadabra</b>' sets the variable text to be equal to the string with the open and closing tags.
The second line sets the contents of the $('#myDiv') to be equal to that variable '<b>abracadabra</b>'. Because they are valid html tags they get parsed and this is why you don't see them in the browser.
When you log the content of the variable text you get it's value not parsed by the browser with the tags which is '<b>abracadabra</b>'

Is it possible to get the root HTML tag, not just body node? [duplicate]

This question already has answers here:
How to get the <html> tag HTML with JavaScript / jQuery?
(6 answers)
Closed 6 years ago.
In both of the following examples, speechTextString ends up including text from the entire document, including text outside of the BODY tag.
Example 1:
r = document.body;
speechTextString = r.innerText;
Example 2:
r = document.getElementsByTagName("BODY").item(0);
speechTextString = r.innerText;
Is it possible to get the root HTML tag, not just the body element?
text outside of the BODY tag
Assuming you aren't talking about the content of the <head> element:
You can't have text outside the <body> element. HTML does not allow it. The browser performs error recovery and moves it inside the <body> element when it parsing the (invalid) HTML into a DOM.
If you want to process a document with that error in it, then you will need to fetch the raw source code (e.g. with XMLHttpRequest) and then write a custom parser for it.

Is there a way in Javascript to turn links in an HTML document into clickable links? [duplicate]

This question already has answers here:
How to replace plain URLs with links?
(25 answers)
Closed 8 years ago.
Is there a way in pure Javascript to turn links in an HTML document into clickable links? I do not want to use regex to solve this problem.
Let's say I have this list in an HTML5 doc:
<pre>
http://www.test.com
http://www.example.com
http://nicelink.com
</pre>
And I want it to automatically turn into this when the page is loaded:
<pre>
http://www.test.com
http://www.example.com
http://nicelink.com
</pre>
What would the vanilla JS code look like if I put a script in the header? Something looping through the HTML code and replacing the lines I suppose but I cannot write it in JS.
Signy
A way to do this:
var str = $('pre').html().trim();
var ar = str.split('\n');
var html = "";
for(var i=0;i<ar.length;i++)
{
html +="<a href='"+ar[i]+"'>"+ar[i]+"</a><br>";
}
console.log(html);
$('body').html(html);
DEMO

How to get JavaScript script tags inside jQuery .html() function? [duplicate]

This question already has answers here:
Can I create script tag by jQuery?
(5 answers)
Closed 9 years ago.
Is it possible to get JavaScript script tags inside jQuery .html() function?
function pleaseWork(){
$('#content').html('<h3 style="color:#335c91">This is the header</h3><div class="holder"><script type="text/javascript" src="javascriptFile.js"></script></div>');
}
What I am trying to accomplish is when the user clicks on a button, this header and JavaScript code show up in the div content, right now when I click on the button, there is no header showing and it goes to a blank page and shows the JavaScript code (it works, but not how I would like it to work.)
Escape the </script> tag, by replacing it with this: <\/script>
If that doesn't work, also surround the script with cdata tags.
Working demo: http://jsfiddle.net/BgxZN/1/
EDIT:
If you want to actually show the contents of the javascript file inside the div, you'll need to use an XMLHTTP request or an iframe.
New working demo: http://jsfiddle.net/BgxZN/13/

Categories

Resources