This answer does not use backticks/template literals/template strings [``
], which are not supported by Internet Explorer.
Using HTML + JavaScript:
You could keep the HTML block in an invisible container [like a ] within your HTML code, then use its
innerHTML
at runtime in JS
For example:
// Create a temporary to load into
var div = document.createElement['div'];
div.setAttribute['class', 'someClass'];
div.innerHTML = document.getElementById['blockOfStuff'].innerHTML;
// You could optionally even do a little bit of string templating
div.innerHTML = div.innerHTML
.replace[/{VENDOR}/g, 'ACME Inc.']
.replace[/{PRODUCT}/g, 'Best TNT']
.replace[/{PRICE}/g, '$1.49'];
// Write the to the HTML container
document.getElementById['targetElement'].appendChild[div];.red {
color: red
}
Here's some random text.
Including HTML markup
And quotes too, or as one man said, "These are quotes, but
'these' are quotes too."
Vendor: {VENDOR}
Product: {PRODUCT}
Price: {PRICE}
Idea from this answer:
JavaScript HERE-doc or other large-quoting mechanism?
Using PHP:
If you want to insert a particularly long block of HTML in PHP you can use the Nowdoc syntax, like so:
Here's a PHP Fiddle demo of the above code that you can run in your browser.
One
important thing to note: The !
I can see it's your birthday on , what are you hoping to get?
And some more HTML!
Contrast that to the simplicity of Nowdoc.
This answer does not use backticks/template literals/template strings [``
], which are not supported by Internet Explorer.
Nội dung chính
- Using HTML + JavaScript:
- Why use Nowdoc in PHP?
- Can I write HTML in JavaScript?
- Can I include HTML in a JavaScript file?
- How do you add HTML in JavaScript?
- Can you mix HTML and JavaScript?
Using HTML + JavaScript:
You could keep the HTML block in an invisible container [like a
] within your HTML code, then use its innerHTML
at runtime in JS
For example:
// Create a temporary to load into
var div = document.createElement['div'];
div.setAttribute['class', 'someClass'];
div.innerHTML = document.getElementById['blockOfStuff'].innerHTML;
// You could optionally even do a little bit of string templating
div.innerHTML = div.innerHTML
.replace[/{VENDOR}/g, 'ACME Inc.']
.replace[/{PRODUCT}/g, 'Best TNT']
.replace[/{PRICE}/g, '$1.49'];
// Write the to the HTML container
document.getElementById['targetElement'].appendChild[div];.red {
color: red
}
Here's some random text.
Including HTML markup
And quotes too, or as one man said, "These are quotes, but
'these' are quotes too."
Vendor: {VENDOR}
Product: {PRODUCT}
Price: {PRICE}
Idea from this answer: JavaScript HERE-doc or other large-quoting mechanism?
Using
PHP:
If you want to insert a particularly long block of HTML in PHP you can use the Nowdoc syntax, like so:
Here's a PHP Fiddle demo of the above code that you can run in your browser.
One important thing to note: The !
I can see it's your birthday on , what are you hoping to get?
And some more HTML!
Contrast that to the simplicity of Nowdoc.
View Discussion
Improve Article
Save Article
Read Discuss View Discussion
Improve Article
Save Article
There are two ways to append HTML code to a div through JavaScript
- Using
the innerHTML attribute
- Using the insertAdjacentHTML[] method
Using the innerHTML attribute:
To append using the innerHTML attribute, first select the element [div] where you want to append the code. Then, add the code enclosed as strings using the += operator on innerHTML.
Syntax:
element.innerHTML += "additional HTML code"
or
element.innerHTML = element.innerHTML + "additional HTML code"
Example:
How to Append HTML Code to a Div using Javascript
body {
text-align: center;
padding: 5%;
}
h2{
color:green;
}
GeeksforGeeks
This is the text which has already been typed into the div
Add Stuff
function addCode[] {
document.getElementById["add_to_me"].innerHTML +=
"This is the text which has been inserted by JS";
}
Output:- Before Clicking the Button:
- After Clicking the Button:
Note: This method basically
destroys all the content of the div and recreates it. So, if you have any listeners attached to the child nodes of that div, they will be lost.
Using the insertAdjacentHTML[] method
HTML code can be appended to a div using the insertAdjacentHTML[] method. However, you need to select an element inside the div to add the code. This method takes two parameters:
- The position [in the document] where you want to insert the code [‘afterbegin’, ‘beforebegin’,
‘afterend’, ‘beforeend’]
- The HTML code you want to insert enclosed in quotes
Syntax:
elementInsideDiv.insertAdjacentHTML['afterend', 'additional HTML code'];
Example:
How to Append HTML Code to a Div using Javascript
body {
text-align: center;
padding: 5%;
}
h2{
color: green
}
GeeksforGeeks
This is the text which has already been typed into the div
Add Stuff
function addCode[] {
document.getElementById["add_after_me"].insertAdjacentHTML["afterend",
"This is the text which has been inserted by JS"];
}
Output:- Before Clicking the Button:
- After Clicking the Button:
Note:You should never insert the HTML code in this way if you are taking it as an input from the user. It opens your website to cross-site scripting vulnerabilities.
JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the
ground up by following this JavaScript Tutorial and JavaScript Examples.
Can I write HTML in JavaScript?
There are many ways to write html with JavaScript. document. write is only useful when you want to write to page before it has actually loaded. If you use document.
Can I include HTML in a JavaScript file?
The process is as simple as
defining a JavaScript file then calling it within the HTML through a script tag. Write the HTML you want to be repeated in the form of a JavaScript file. For a simple copyright insertion, create a file with a single line of JS, for example: document.
How do you add HTML in JavaScript?
Using the innerHTML attribute: To append using the innerHTML attribute, first select the element [div] where you want to append the
code. Then, add the code enclosed as strings using the += operator on innerHTML.
Can you mix HTML and JavaScript?
JavaScript can be used in combination with HTML to power modern web applications that are intuitive, interactive and user-friendly. By using simple client-side validation, it reduces server traffic and improves the overall efficiency of the website.
Chủ Đề