There’s no reason to put a whole script inside a single button
- It is possible? The answer is yes
- Is recommended? The answer is nay
I’ll explain later why it’s not recommended.
Inline example
Even if it is not recommended I will give an example, to solve you need to exchange the quotes for "single quotes" (apostrophes):
<!DOCTYPE html>
<html>
<body>
<div id="demo">
<h1>The XMLHttpRequest Object</h1>
<button type="button" onclick="var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById('demo').innerHTML = this.responseText; } }; xhttp.open('GET', 'ajax_info.txt', true); xhttp.send();">Change Content</button>
</div>
</body>
</html>
Note that the page http://www.w3schools.com/code/tryit.asp?filename=FAK6YYP9UBMF is not working due to a browser security lock, it is w3schools.com’s fault and not even from the script.
What is preferable
Best is to move every script into a file .js
, model independent, circumstances; anyway it is possible to avoid all this using GIFT and no one is judging, this is an orientation of how to avoid problems and show that a lot is possible.
Use everything in files .js
will bring you advantages such as:
- Resource cache
- Reuse of code
- Decrease the HTML
- Avoid obstructing the HTML
- Facilitates organizing
An example, create a file called app.js
and put this content:
function ready(callback)
{
if (/^(interactive|complete)$/i.test(doc.readyState)) {
callback();
} else {
doc.addEventListener('DOMContentLoaded', callback);
}
}
function loadDoc(url, callback, error)
{
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
callback(this.responseText);
} else if (error) {
error(this.status);
}
};
xhttp.open("GET", url, true);
xhttp.send();
}
//Equivale ao $.ready do jQuery
ready(function() {
var botao1 = document.getElementById("botao1");
var botao2 = document.getElementById("botao2");
botao1.onclick = function() {
loadDoc("ajax_info1.txt", function(resposta) {
document.getElementById("demo").innerHTML = resposta;
});
};
botao2.onclick = function() {
loadDoc("ajax_info2.txt", function(resposta) {
document.getElementById("demo").innerHTML = resposta;
});
};
});
And the html should look like this:
<!DOCTYPE html>
<html>
<head>
<title>Exemplo</title>
<script type="text/javascript" src="app.js"></script>
</head>
<body>
<div id="demo"></div>
<button id="botao1">Testar 1</button>
<button id="botao2">Testar 2</button>
</body>
</html>
Look, it gets a lot less polluted
Putting all this inside a single button would almost obstruct the HTML unnecessarily, it would be better to put the loadDoc inside a file with the extension. js
– Guilherme Nascimento
I know that I am trying to implement a taghelper in c# will be all via code Behind, and at first I will try this way
– Warleson Reis
You better edit the question, because ae changes the whole meaning. If not people answer one thing and you want another. Please give details.
– Guilherme Nascimento
the question this correct I want to know that there
– Warleson Reis
It wasn’t clear to you ?
– Warleson Reis
All right, I’ll formulate an answer.
– Guilherme Nascimento
a since from the moment I look at an example putting the whole function within the onclick the rest I can manage to do, but it will not stay that way no, I just want to see an example so I can make the taghelper. Thank you :)
– Warleson Reis
Independent of the taghelper after the generated html you can do all interaction in the . js file by DOM control. Doing everything inline can be a problem.
– Guilherme Nascimento