This question already has answers here:
Executing <script> elements inserted with .innerHTML
(23 answers)
Closed 2 years ago.
So, I was adding some new js code to body and i notice that if you add with $('body').append(), it works but when i tried to add with document.body.innerHTML+="some script", It doesn't run.
HTML5 specifies that a <script> tag inserted with innerHTML should not execute.
Have a look at Element.innerHTML - Security considerations and the specification.
Related
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.
This question already has answers here:
How to call multiple JavaScript functions in onclick event?
(14 answers)
Closed 7 years ago.
I was trying to run two JS functions in one click using this. Can anybody show me how to do it correctly?
<button onclick="getlocation" onclick="showDiv()">Try It</button>
I tried adding a ; between the two functions but it didnt work too.
"Adding ";" semicolon didn't work, if you could help.."
It works for sure, you have to do it like that: onclick="getLocation();showDiv()"
Was not relevant in this case:
The problem could be, that both functions are setting the content of your div, so the second function will overwrite the content!
Use .innerHTML += "..."; on you second function, this will append the content to the exisitng content.
This question already has answers here:
How can I change div content with JavaScript?
(6 answers)
Closed 7 years ago.
How do i change the content of an div on an website that has an id with javascript?
<div class="chat-area" id="chat-area">[Changeable]</div>
I want to know how i add content where it says "[Changeable]" with javascript so i can run the command through the console.
I'd like if you keep your explainage simple, because im still very new to html/css/javascript ;)
In very simple JavaScript terms, you can use:
document.getElementById("chat-area").innerHTML = 'New Content';
And yes, this would work in GreaseMonkey / UserScripts too!
You can use the innerHTML property to achieve your goal like so:
document.getElementById("chat-area").innerHTML = "Your new content here";
This question already has answers here:
Assign div text to variable then show it
(3 answers)
Closed 8 years ago.
I have the following div:
<div id="mydiv">something</div>
I need to get the content of the div (mydiv) and put it into a JavaScript variable. How can i do it?
var content = document.getElementById("mydiv").innerHTML;
This question already has answers here:
Find currently visible div in jquery
(5 answers)
Closed 3 years ago.
Only one jQuery event runs per refresh of the page. What am I doing that's so wrong?
$("#contact_link_email").on("click",function(){
$("#contact-form").replaceWith('<h2>Email heading</h2>');
});
$("#contact_link_facebook").on("click",function(){
$("#contact-form").replaceWith('<h2>Facebook heading</h2>');
});
$("#contact_link_twitter").click(function(){
$("#contact-form").replaceWith('<h2>Twitter heading</h2>');
});
$("#contact_link_gplus").click(function(){
$("#contact-form").replaceWith('<h2>GPlus heading</h2>');
});
Once one of those events runs, $("#contact-form") doesn't reference anything. If you want to replace it with something, try making that something have the same id.