Javascript exit button ok

I am new to web programming but still trying to develop a website. I use HTML and JavaScript to get everything done, but I'm really stuck. After looking at a few examples online, I was able to create an OK / Cancel button. Now I'm trying to create the same type of button using CSS, because I do not want to write the same piece of code on every page. Can CSS be used for this purpose? If yes, can someone please show me how to do this?

This is the code I have for buttons, which, by the way, work just fine:

<html>
   <body>
     <script type="text/javascript">
        if (confirm("Press 'OK' to leave the Eternity Test, or 'Cancel' if you want to stay: "))
        {         
           window.location="http://google.com";
        }
        else
        {         
           window.location= "http://www.myWebPage.com";
        }
      </script>
   </body>
</html>

Thank.

+3
source share
6 answers

, css, . , javascript. javascript :

<script type="text/javascript" src="external.js"></script>

, , ( ).

+2

, , , CSS ( div, / ). :

javascript, :

function closeWindow(){
   if (confirm("Press 'OK' to leave the Eternity Test, or 'Cancel' if you want to stay: "))
   {         
      window.location="http://google.com";
   }
      else
   {         
      window.location= "http://www.myWebPage.com";
   }
}

.js, , this , , :

<a href="javasctipt:closeWindow();">Close Window</a>
+1

CSS, javascript (, confirm.js), :


<html>
   <body>
     <script type="text/javascript" src="confirm.js"> </script>
   </body>
</html>

confirm.js :



if (confirm("Press 'OK' to leave the Eternity Test, or 'Cancel' if you want to stay: ")) {
window.location="http://google.com"; } else {
window.location= "http://www.myWebPage.com"; }

+1

css, , css. , javascript .

+1

/ CSS.

. if/else, {}

    <html>
   <body>
     <script type="text/javascript">
        if (confirm("Press 'OK' to leave the Eternity Test, or 'Cancel' if you want to stay: "))       
           window.location="http://google.com";
        else     
           window.location= "http://www.myWebPage.com";
      </script>
   </body>
</html>
+1

Create a script called "exit.js" with the code between the tags, and then simply access the script using <script type="text/javascript" src="/exit.js"> </script>all of your pages.

+1
source

Source: https://habr.com/ru/post/1792687/


All Articles