Problem when trying to enter age alert in online store

Asked

Viewed 26 times

-1

<body>
<script>
function menor_idade(){
location.href="https://criancas.uol.com.br/"
}
function maior_idade(){
document.cookie="aceita_conteudo_pagina:sim";
window.location = location.href;
}
if(document.cookie.indexOf("aceita_conteudo_pagina")<0){
var home_principal = $$(".cms-index-index")
home_principal[0].innerHTML+="<h1 style="font-size: 32px; margin: 30px auto; text-align: center;">Este site oferece conteúdo impróprio para menos de 18 anos</h1>";
home_principal[0].innerHTML="<div id="master" style="height: 40px; text-align: center;"><span style="padding: 5px; background: green; color: #FFF; font-size: 18px; cursor: pointer;" onclick="maior_idade()">Tenho mais de 18 anos</span><span style="margin-left: 40px; padding: 5px; background: red; color: #FFF; font-size: 18px; cursor: pointer;"onclick="menor_idade()">Tenho menos de 18 anos</span></div>"
}
</script>

</body>
  • Tip: Add Multiple tags just by adding, it won’t help you and probably other members can ask for the closing of the question.

  • 2

    About the error, remove a dollar sign $$ correct would be $; The second innerHTML is missing the sign of +, thus it is only rewriting the internal code of the element; it is necessary to "escape" the quotation marks of its code (of both innerHTML), for example "<div id=\"master\" …"

1 answer

1

As @Valdeirpsr said...

On the line

var home_principal = $$(".cms-index-index")

Remove one of $, would look like this:

var home_principal = $(".cms-index-index")

In the innerHTML te 2 problems:

1- In

home_principal[0].innerHTML+="<h1 style="font-size: 32px; margin: 30px auto; text-align: center;">Este site oferece conteúdo impróprio para menos de 18 anos</h1>";
home_principal[0].innerHTML="<div id="master" style="height: 40px; text-align: center;"><span style="padding: 5px; background: green; color: #FFF; font-size: 18px; cursor: pointer;" onclick="maior_idade()">Tenho mais de 18 anos</span><span style="margin-left: 40px; padding: 5px; background: red; color: #FFF; font-size: 18px; cursor: pointer;"onclick="menor_idade()">Tenho menos de 18 anos</span></div>"

In the second line you are about writing the innerHTML first-line

home_principal[0].innerHTML+="<h1 style="font-size: 32px; margin: 30px auto; text-align: center;">Este site oferece conteúdo impróprio para menos de 18 anos</h1>";
home_principal[0].innerHTML="<div id="master" style="height: 40px; text-align: center;"><span style="padding: 5px; background: green; color: #FFF; font-size: 18px; cursor: pointer;" onclick="maior_idade()">Tenho mais de 18 anos</span><span style="margin-left: 40px; padding: 5px; background: red; color: #FFF; font-size: 18px; cursor: pointer;"onclick="menor_idade()">Tenho menos de 18 anos</span></div>"

2- When you want to use quotes inside other quotes you need to "escape" the internal quotes with \ or change double quotation marks by single quotation marks, i.e., outside use " and inside ' or vice versa.

With the two mistakes of innerHTML corrected would look like this:

home_principal[0].innerHTML+="<h1 style='font-size: 32px; margin: 30px auto; text-align: center;'>Este site oferece conteúdo impróprio para menos de 18 anos</h1>";
home_principal[0].innerHTML+="<div id='master' style='height: 40px; text-align: center;'><span style='padding: 5px; background: green; color: #FFF; font-size: 18px; cursor: pointer;' onclick='maior_idade()'>Tenho mais de 18 anos</span><span style='margin-left: 40px; padding: 5px; background: red; color: #FFF; font-size: 18px; cursor: pointer;' onclick='menor_idade()'>Tenho menos de 18 anos</span></div>"

Double quotes to mark the beginning and end of the string and single quotes to mark the beginning and end of attributes of HTML tags

Browser other questions tagged

You are not signed in. Login or sign up in order to post.