Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Operadores Aritméticos</title>
- </head>
- <body>
- <script>
- //window.onload = boot(); //errado porque corresponde à execução neste exato instante (quando tantos elementos ainda NÃO foram carregados)
- window.onload = boot; //correto, porque significa "invocar boot quando (e apenas quando)" tudo (HTML+JS) tiver sido loaded/carregado
- var oOperandoEsquerdo, oOperador, oOperandoDireito, oCalcular;
- var oSectionResposta;
- function boot(){
- //1 - casamentos / associações entre vars do JS e elementos HTML
- oOperandoEsquerdo = window.document.getElementById("idOperandoEsquerdo"); //null se o id não existir
- oOperador = window.document.getElementById("idOperador");
- oOperandoDireito = window.document.getElementById("idOperandoDireito");
- oCalcular = window.document.getElementById("idCalcular");
- oSectionResposta = window.document.getElementById("idSectionResposta");
- //2 - conferir comportamentos aos objetos HTML relevantes
- //nomeDeObjeto.nomeDeEventToHandle = eventHandler;
- //oCalcular.onclick = function (){i1; i2; ... i3;}; //anonymous function
- //NÃO queremos invocar a function!
- //queremos indicar o nome de uma function a invocar
- oCalcular.onclick = functionQueVaiResponderAoMomentoDoClickEmCalcular; //named function
- }//boot
- function functionQueVaiResponderAoMomentoDoClickEmCalcular(){
- var oe = oOperandoEsquerdo.value.trim(); //string ; exemplo de trim " 222 " -> "222"
- var od = oOperandoDireito.value.trim(); //trim é um método de objetos do tipo string
- var op = oOperador.value.trim();
- var r = maquinaDeCalcularAritmetica (oe, op, od);
- /*
- TODO:
- onde e como apresentar a resposta?
- onde e como chamar boot?
- */
- //window.alert("O resultado é "+r); //caixa de diálogo com o resultado
- //innerHTML representa o conteúdo de qualquer elemento não vazio
- oSectionResposta.innerHTML = "O resultado é <mark>"+r+"</mark>";
- }//functionQueVaiResponderAoMomentoDoClickEmCalcular
- function maquinaDeCalcularAritmetica(
- pOperandoEsquerdo,
- pOperador,
- pOperandoDireito
- ){
- var strExpressaoParaSerCalculada =
- pOperandoEsquerdo + pOperador + pOperandoDireito;
- var resultado = eval (strExpressaoParaSerCalculada);
- return resultado;
- }//maquinaDeCalcularAritmetica
- </script>
- <!-- form é um elemento para colecionar inputs / entradas / respostas -->
- <form>
- <!-- operando esquerdo -->
- <label for="idOperandoEsquerdo">operando esquerdo</label>
- <input
- id="idOperandoEsquerdo"
- type="text"
- value="2"
- placeholder="operando esquerdo exº 2"
- ><!-- elemento vazio para representar uma entrada / resposta -->
- <!-- operador -->
- <label for="idOperador">operador</label>
- <select id="idOperador"><!-- conjunto fechado de possibilidades -->
- <option>+</option>
- <option>-</option>
- <option>*</option>
- <option>/</option>
- <option>%</option>
- </select>
- <!-- operando direito -->
- <label for="idOperandoDireito">operando direito</label>
- <input
- id="idOperandoDireito"
- type="text"
- value="1"
- placeholder="operando direito exº 1"
- >
- <input
- id="idCalcular"
- type="button"
- value="calcular"
- >
- </form>
- <hr>
- <section id="idSectionResposta">Aqui aparecerá a computação do resultado.</section>
- </body>
- </html>
- *****************************
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Operadores Aritméticos</title>
- <!-- o JS foi externalizado e é incorporado agora, pelo elemento script -->
- <script src="2.js"></script>
- </head>
- <body>
- <!-- form é um elemento para colecionar inputs / entradas / respostas -->
- <form>
- <!-- operando esquerdo -->
- <label for="idOperandoEsquerdo">operando esquerdo</label>
- <input
- id="idOperandoEsquerdo"
- type="text"
- value="2"
- placeholder="operando esquerdo exº 2"
- ><!-- elemento vazio para representar uma entrada / resposta -->
- <!-- operador -->
- <label for="idOperador">operador</label>
- <select id="idOperador"><!-- conjunto fechado de possibilidades -->
- <option>+</option>
- <option>-</option>
- <option>*</option>
- <option>/</option>
- <option>%</option>
- </select>
- <!-- operando direito -->
- <label for="idOperandoDireito">operando direito</label>
- <input
- id="idOperandoDireito"
- type="text"
- value="1"
- placeholder="operando direito exº 1"
- >
- <input
- id="idCalcular"
- type="button"
- value="calcular"
- >
- </form>
- <hr>
- <section id="idSectionResposta">Aqui aparecerá a computação do resultado.</section>
- </body>
- </html>
- ***************************
- //window.onload = boot(); //errado porque corresponde à execução neste exato instante (quando tantos elementos ainda NÃO foram carregados)
- window.onload = boot; //correto, porque significa "invocar boot quando (e apenas quando)" tudo (HTML+JS) tiver sido loaded/carregado
- var oOperandoEsquerdo, oOperador, oOperandoDireito, oCalcular;
- var oSectionResposta;
- function boot(){
- //1 - casamentos / associações entre vars do JS e elementos HTML
- oOperandoEsquerdo = window.document.getElementById("idOperandoEsquerdo"); //null se o id não existir
- oOperador = window.document.getElementById("idOperador");
- oOperandoDireito = window.document.getElementById("idOperandoDireito");
- oCalcular = window.document.getElementById("idCalcular");
- oSectionResposta = window.document.getElementById("idSectionResposta");
- //2 - conferir comportamentos aos objetos HTML relevantes
- //nomeDeObjeto.nomeDeEventToHandle = eventHandler;
- //oCalcular.onclick = function (){i1; i2; ... i3;}; //anonymous function
- //NÃO queremos invocar a function!
- //queremos indicar o nome de uma function a invocar
- oCalcular.onclick = functionQueVaiResponderAoMomentoDoClickEmCalcular; //named function
- }//boot
- function functionQueVaiResponderAoMomentoDoClickEmCalcular(){
- var oe = oOperandoEsquerdo.value.trim(); //string ; exemplo de trim " 222 " -> "222"
- var od = oOperandoDireito.value.trim(); //trim é um método de objetos do tipo string
- var op = oOperador.value.trim();
- var r = maquinaDeCalcularAritmetica (oe, op, od);
- /*
- TODO:
- onde e como apresentar a resposta?
- onde e como chamar boot?
- */
- //window.alert("O resultado é "+r); //caixa de diálogo com o resultado
- //innerHTML representa o conteúdo de qualquer elemento não vazio
- oSectionResposta.innerHTML = "O resultado é <mark>"+r+"</mark>";
- }//functionQueVaiResponderAoMomentoDoClickEmCalcular
- function maquinaDeCalcularAritmetica(
- pOperandoEsquerdo,
- pOperador,
- pOperandoDireito
- ){
- var strExpressaoParaSerCalculada =
- pOperandoEsquerdo + pOperador + pOperandoDireito;
- var resultado = eval (strExpressaoParaSerCalculada);
- return resultado;
- }//maquinaDeCalcularAritmetica
- ****
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>My Google Searcher</title>
- <script>
- window.onload = boot;
- const
- ID_TEXT_SEARCH_EXP = "idTextSearchExp", //better this way, for internal documentation
- ID_BTN_SEARCH = "idBtnSearch"; //cascade declaration
- var oTextSearchExp, oBtnSearch;
- function boot(){
- var x;
- //1 - associations
- oTextSearchExp = document.getElementById(ID_TEXT_SEARCH_EXP);
- oBtnSearch = document.getElementById(ID_BTN_SEARCH);
- //2 - set behaviors
- /*
- oBtnSearch.onclick = function(){
- window.alert("Will search for "+oTextSearchExp.value);
- }//anonymous function
- */
- oBtnSearch.onclick = willBuildTheRightGoogleQueryAndPerformTheSearch;
- }//boot
- function willBuildTheRightGoogleQueryAndPerformTheSearch(){
- /*
- A Google search is performed by calling a base URL
- https://www.google.com/search?
- and appending it the necessary params
- At least, the search expression must be provided:
- q param - is the search expression when the terms' order is NOT relevant
- as_epq param - is the search expression to use, when the terms' order IS relevant
- */
- var strSearchExpression = oTextSearchExp.value.trim();
- var strQuery = "q="+strSearchExpression; //q=a b
- var strSearchUrl = "https://www.google.com/search?"+strQuery;
- window.document.location.href=strSearchUrl;
- }//willBuildTheRightGoogleQueryAndPerformTheSearch
- </script>
- </head>
- <body>
- <form>
- <label for="idTextSearchExp">Search expression: </label>
- <input
- id="idTextSearchExp"
- type="text"
- value="blue mushrooms"
- placeholder="your search expression"
- >
- <input
- id="idBtnSearch"
- type="button"
- value="search!"
- >
- </form>
- </body>
- </html>
- ************
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>My Google Searcher</title>
- <script>
- window.onload = boot;
- const GOOGLE_SEARCH_BASE = "https://www.google.com/search?";
- const
- ID_TEXT_SEARCH_EXP = "idTextSearchExp", //better this way, for internal documentation
- ID_BTN_SEARCH = "idBtnSearch",
- ID_CHECK_ORDER_IS_RELEVANT = "idCheckOrderIsRelevant"
- ;//cascade declaration
- var oTextSearchExp,
- oBtnSearch,
- oCheckOrderIsRelevant
- ;
- function boot(){
- //1 - associations
- oTextSearchExp = document.getElementById(ID_TEXT_SEARCH_EXP);
- oBtnSearch = document.getElementById(ID_BTN_SEARCH);
- oCheckOrderIsRelevant = document.getElementById(ID_CHECK_ORDER_IS_RELEVANT);
- //TODO: quality-control
- //2 - set behaviors
- /*
- oBtnSearch.onclick = function(){
- window.alert("Will search for "+oTextSearchExp.value);
- }//anonymous function
- */
- oBtnSearch.onclick = willBuildTheRightGoogleQueryAndPerformTheSearch;
- }//boot
- function willBuildTheRightGoogleQueryAndPerformTheSearch(){
- /*
- A Google search is performed by calling a base URL
- https://www.google.com/search?
- and appending it the necessary params
- At least, the search expression must be provided:
- q param - is the search expression when the terms' order is NOT relevant
- as_epq param - is the search expression to use, when the terms' order IS relevant
- */
- var strSearchExpression = oTextSearchExp.value.trim();
- var bIsOrderRelevant = oCheckOrderIsRelevant.checked; //true (if it is checked) or false (if NOT checked)
- if (bIsOrderRelevant){
- //let would NOT work in this code
- var strQuery = "as_epq="+strSearchExpression;
- }
- else{
- //let would NOT work in this code
- var strQuery = "q="+strSearchExpression;
- }
- var strSearchUrl = GOOGLE_SEARCH_BASE+strQuery;
- window.document.location.href=strSearchUrl;
- }//willBuildTheRightGoogleQueryAndPerformTheSearch
- </script>
- </head>
- <body>
- <form>
- <label for="idTextSearchExp">Search expression: </label>
- <input
- id="idTextSearchExp"
- type="text"
- value="blue mushrooms"
- placeholder="your search expression"
- >
- <label>Terms' order is relevant</label>
- <input
- id="idCheckOrderIsRelevant"
- type="checkbox"
- >
- <input
- id="idBtnSearch"
- type="button"
- value="search!"
- >
- </form>
- </body>
- </html>
- *******************************
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>My Google Searcher</title>
- <script>
- window.onload = boot;
- const GOOGLE_SEARCH_BASE = "https://www.google.com/search?";
- const
- ID_TEXT_SEARCH_EXP = "idTextSearchExp", //better this way, for internal documentation
- ID_BTN_SEARCH = "idBtnSearch",
- ID_CHECK_ORDER_IS_RELEVANT = "idCheckOrderIsRelevant",
- ID_SELECT_SAFETY = "idSelectSafety"
- ;//cascade declaration
- var oTextSearchExp,
- oBtnSearch,
- oCheckOrderIsRelevant,
- oSelectSafety
- ;
- function boot(){
- //1 - associations
- oTextSearchExp = document.getElementById(ID_TEXT_SEARCH_EXP);
- oBtnSearch = document.getElementById(ID_BTN_SEARCH);
- oCheckOrderIsRelevant = document.getElementById(ID_CHECK_ORDER_IS_RELEVANT);
- oSelectSafety = document.getElementById(ID_SELECT_SAFETY);
- //TODO: quality-control
- //2 - set behaviors
- /*
- oBtnSearch.onclick = function(){
- window.alert("Will search for "+oTextSearchExp.value);
- }//anonymous function
- */
- oBtnSearch.onclick = willBuildTheRightGoogleQueryAndPerformTheSearch;
- }//boot
- function willBuildTheRightGoogleQueryAndPerformTheSearch(){
- /*
- A Google search is performed by calling a base URL
- https://www.google.com/search?
- and appending it the necessary params
- At least, the search expression must be provided:
- q param - is the search expression when the terms' order is NOT relevant
- as_epq param - is the search expression to use, when the terms' order IS relevant
- Other params:
- safety - controls the "safety" of the results (off for no safety)
- */
- var strSearchExpression = oTextSearchExp.value.trim();
- var bIsOrderRelevant = oCheckOrderIsRelevant.checked; //true (if it is checked) or false (if NOT checked)
- //TODO: there is something ugly related to the strSearchExpression
- if (bIsOrderRelevant){
- //let would NOT work in this code
- var strQuery = "as_epq="+strSearchExpression;
- }
- else{
- //let would NOT work in this code
- var strQuery = "q="+strSearchExpression;
- }
- strQuery+="&safety="+oSelectSafety.value;
- var strSearchUrl = GOOGLE_SEARCH_BASE+strQuery;
- window.document.location.href=strSearchUrl;
- }//willBuildTheRightGoogleQueryAndPerformTheSearch
- </script>
- </head>
- <body>
- <form>
- <label for="idTextSearchExp">Search expression: </label>
- <input
- id="idTextSearchExp"
- type="text"
- value="blue mushrooms"
- placeholder="your search expression"
- >
- <label>Terms' order is relevant</label>
- <input
- id="idCheckOrderIsRelevant"
- type="checkbox"
- >
- <label for="idSelectSafety">Results' safety</label>
- <select id="idSelectSafety">
- <!-- TODO value for option -->
- <option>full safety</option>
- <option>all images</option>
- <option>partial images</option>
- <option selected value="off">off</option>
- </select>
- <input
- id="idBtnSearch"
- type="button"
- value="search!"
- >
- </form>
- </body>
- </html>
Add Comment
Please, Sign In to add comment