Why event not firing (jquery)












-1















I have a main page, with 2 buttons (each button refer to another page):



<html>
<head>
<title>TBD</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="js/login.js"></script>
</head>

<body>
<button id="A">A</button>
<button id="B">B</button>
</body>
</html>


login.js:



$(document).ready(function(){

$("#A").click(function(){
$.get( "/A.html", function( data ) {
console.log("A button clicked");
document.write(data);
});

});

$("#B").click(function(){
$.get( "/B.html", function( data ) {
console.log("B button clicked");
document.write(data);
});

});
});


A and B (HTMLs and JSs) have the same behavior:
for example (B.html & B.js):



<html>
<head>
<title>B</title>
<script type="text/javascript" src="js/B.js"></script>
</head>

<body>
B
<button id="backButton">Back</button>
</body>
</html>


$(document).ready(function(){
console.log("B js ready");
$("#backButton").click(function(){
console.log("back button clicked");
$.get( "/back.html", function( data ) {
console.log("get results");
document.write(data);
});

});

});


I when I clicked from main page on A or B button and get to the new page,
I can see in the Console log that the A.js (or B.js) are ready (so the A.js or B.js are loaded),
But after it , if I click on the "back" button, nothing happen (even the line console.log("back button clicked"); is not called.



Why this happen and how to fix it ?










share|improve this question



























    -1















    I have a main page, with 2 buttons (each button refer to another page):



    <html>
    <head>
    <title>TBD</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript" src="js/login.js"></script>
    </head>

    <body>
    <button id="A">A</button>
    <button id="B">B</button>
    </body>
    </html>


    login.js:



    $(document).ready(function(){

    $("#A").click(function(){
    $.get( "/A.html", function( data ) {
    console.log("A button clicked");
    document.write(data);
    });

    });

    $("#B").click(function(){
    $.get( "/B.html", function( data ) {
    console.log("B button clicked");
    document.write(data);
    });

    });
    });


    A and B (HTMLs and JSs) have the same behavior:
    for example (B.html & B.js):



    <html>
    <head>
    <title>B</title>
    <script type="text/javascript" src="js/B.js"></script>
    </head>

    <body>
    B
    <button id="backButton">Back</button>
    </body>
    </html>


    $(document).ready(function(){
    console.log("B js ready");
    $("#backButton").click(function(){
    console.log("back button clicked");
    $.get( "/back.html", function( data ) {
    console.log("get results");
    document.write(data);
    });

    });

    });


    I when I clicked from main page on A or B button and get to the new page,
    I can see in the Console log that the A.js (or B.js) are ready (so the A.js or B.js are loaded),
    But after it , if I click on the "back" button, nothing happen (even the line console.log("back button clicked"); is not called.



    Why this happen and how to fix it ?










    share|improve this question

























      -1












      -1








      -1








      I have a main page, with 2 buttons (each button refer to another page):



      <html>
      <head>
      <title>TBD</title>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
      <script type="text/javascript" src="js/login.js"></script>
      </head>

      <body>
      <button id="A">A</button>
      <button id="B">B</button>
      </body>
      </html>


      login.js:



      $(document).ready(function(){

      $("#A").click(function(){
      $.get( "/A.html", function( data ) {
      console.log("A button clicked");
      document.write(data);
      });

      });

      $("#B").click(function(){
      $.get( "/B.html", function( data ) {
      console.log("B button clicked");
      document.write(data);
      });

      });
      });


      A and B (HTMLs and JSs) have the same behavior:
      for example (B.html & B.js):



      <html>
      <head>
      <title>B</title>
      <script type="text/javascript" src="js/B.js"></script>
      </head>

      <body>
      B
      <button id="backButton">Back</button>
      </body>
      </html>


      $(document).ready(function(){
      console.log("B js ready");
      $("#backButton").click(function(){
      console.log("back button clicked");
      $.get( "/back.html", function( data ) {
      console.log("get results");
      document.write(data);
      });

      });

      });


      I when I clicked from main page on A or B button and get to the new page,
      I can see in the Console log that the A.js (or B.js) are ready (so the A.js or B.js are loaded),
      But after it , if I click on the "back" button, nothing happen (even the line console.log("back button clicked"); is not called.



      Why this happen and how to fix it ?










      share|improve this question














      I have a main page, with 2 buttons (each button refer to another page):



      <html>
      <head>
      <title>TBD</title>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
      <script type="text/javascript" src="js/login.js"></script>
      </head>

      <body>
      <button id="A">A</button>
      <button id="B">B</button>
      </body>
      </html>


      login.js:



      $(document).ready(function(){

      $("#A").click(function(){
      $.get( "/A.html", function( data ) {
      console.log("A button clicked");
      document.write(data);
      });

      });

      $("#B").click(function(){
      $.get( "/B.html", function( data ) {
      console.log("B button clicked");
      document.write(data);
      });

      });
      });


      A and B (HTMLs and JSs) have the same behavior:
      for example (B.html & B.js):



      <html>
      <head>
      <title>B</title>
      <script type="text/javascript" src="js/B.js"></script>
      </head>

      <body>
      B
      <button id="backButton">Back</button>
      </body>
      </html>


      $(document).ready(function(){
      console.log("B js ready");
      $("#backButton").click(function(){
      console.log("back button clicked");
      $.get( "/back.html", function( data ) {
      console.log("get results");
      document.write(data);
      });

      });

      });


      I when I clicked from main page on A or B button and get to the new page,
      I can see in the Console log that the A.js (or B.js) are ready (so the A.js or B.js are loaded),
      But after it , if I click on the "back" button, nothing happen (even the line console.log("back button clicked"); is not called.



      Why this happen and how to fix it ?







      javascript jquery






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 19:17









      user3668129user3668129

      89411228




      89411228
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Try this way... it worked for me... you need to add the jquery.min.js library link in the second page.



          <html>
          <header>

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> //add it
          </script>

          </header>

          <body >
          B
          <button id="backButton">Back</button>
          <script type="text/javascript" src="js/B.js"></script> // add the line here

          </body>

          </html>





          share|improve this answer


























          • It doesn't help. I see the following warning on the console login.js:14 A parser-blocking, cross site (i.e. different eTLD+1) script, ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js, is invoked via document.write. The network request for this script MAY be blocked by the browser in this or a future page load due to poor network connectivity. If blocked in this page load, it will be confirmed in a subsequent console message. See chromestatus.com/feature/5718547946799104 for more details.

            – user3668129
            Nov 23 '18 at 19:53



















          0














          You need to move your script tag to the end of your html tag, so the page will render before the javascript call.



          <html>
          <head>
          <title>B</title>
          </head>

          <body>
          B <button id="backButton">Back</button>
          <script type="text/javascript" src="js/B.js"></script> //add script line here!
          </body>
          </html>


          To show the rendering problem, place the script tag right before the head tag and add a debugger in your B.js file and run the page with the developer tool open.



          $(document).ready(function() {
          debugger; // Add debugger here!
          console.log('B js ready');
          $('#backButton').on('click', function() {
          console.log('back button clicked');
          $.get('/back.html', function(data) {
          console.log('get results');
          document.write(data);
          });
          });
          });


          When you load the page B you will see that the button doesn't exist yet, so the script can't bind the click event.






          share|improve this answer


























          • Doesn't help...

            – user3668129
            Nov 23 '18 at 19:49











          • The problem happens because the $ (document).ready fires before the page rendering, this probably happens because the script thinks that the page is already ready, since you just changed the document content with javascript. I've improved my answer to better show you.

            – Gustavo Pereira
            Nov 24 '18 at 12:54











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451953%2fwhy-event-not-firing-jquery%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Try this way... it worked for me... you need to add the jquery.min.js library link in the second page.



          <html>
          <header>

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> //add it
          </script>

          </header>

          <body >
          B
          <button id="backButton">Back</button>
          <script type="text/javascript" src="js/B.js"></script> // add the line here

          </body>

          </html>





          share|improve this answer


























          • It doesn't help. I see the following warning on the console login.js:14 A parser-blocking, cross site (i.e. different eTLD+1) script, ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js, is invoked via document.write. The network request for this script MAY be blocked by the browser in this or a future page load due to poor network connectivity. If blocked in this page load, it will be confirmed in a subsequent console message. See chromestatus.com/feature/5718547946799104 for more details.

            – user3668129
            Nov 23 '18 at 19:53
















          0














          Try this way... it worked for me... you need to add the jquery.min.js library link in the second page.



          <html>
          <header>

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> //add it
          </script>

          </header>

          <body >
          B
          <button id="backButton">Back</button>
          <script type="text/javascript" src="js/B.js"></script> // add the line here

          </body>

          </html>





          share|improve this answer


























          • It doesn't help. I see the following warning on the console login.js:14 A parser-blocking, cross site (i.e. different eTLD+1) script, ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js, is invoked via document.write. The network request for this script MAY be blocked by the browser in this or a future page load due to poor network connectivity. If blocked in this page load, it will be confirmed in a subsequent console message. See chromestatus.com/feature/5718547946799104 for more details.

            – user3668129
            Nov 23 '18 at 19:53














          0












          0








          0







          Try this way... it worked for me... you need to add the jquery.min.js library link in the second page.



          <html>
          <header>

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> //add it
          </script>

          </header>

          <body >
          B
          <button id="backButton">Back</button>
          <script type="text/javascript" src="js/B.js"></script> // add the line here

          </body>

          </html>





          share|improve this answer















          Try this way... it worked for me... you need to add the jquery.min.js library link in the second page.



          <html>
          <header>

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> //add it
          </script>

          </header>

          <body >
          B
          <button id="backButton">Back</button>
          <script type="text/javascript" src="js/B.js"></script> // add the line here

          </body>

          </html>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 19:47

























          answered Nov 23 '18 at 19:42









          Fathma siddiqueFathma siddique

          887




          887













          • It doesn't help. I see the following warning on the console login.js:14 A parser-blocking, cross site (i.e. different eTLD+1) script, ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js, is invoked via document.write. The network request for this script MAY be blocked by the browser in this or a future page load due to poor network connectivity. If blocked in this page load, it will be confirmed in a subsequent console message. See chromestatus.com/feature/5718547946799104 for more details.

            – user3668129
            Nov 23 '18 at 19:53



















          • It doesn't help. I see the following warning on the console login.js:14 A parser-blocking, cross site (i.e. different eTLD+1) script, ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js, is invoked via document.write. The network request for this script MAY be blocked by the browser in this or a future page load due to poor network connectivity. If blocked in this page load, it will be confirmed in a subsequent console message. See chromestatus.com/feature/5718547946799104 for more details.

            – user3668129
            Nov 23 '18 at 19:53

















          It doesn't help. I see the following warning on the console login.js:14 A parser-blocking, cross site (i.e. different eTLD+1) script, ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js, is invoked via document.write. The network request for this script MAY be blocked by the browser in this or a future page load due to poor network connectivity. If blocked in this page load, it will be confirmed in a subsequent console message. See chromestatus.com/feature/5718547946799104 for more details.

          – user3668129
          Nov 23 '18 at 19:53





          It doesn't help. I see the following warning on the console login.js:14 A parser-blocking, cross site (i.e. different eTLD+1) script, ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js, is invoked via document.write. The network request for this script MAY be blocked by the browser in this or a future page load due to poor network connectivity. If blocked in this page load, it will be confirmed in a subsequent console message. See chromestatus.com/feature/5718547946799104 for more details.

          – user3668129
          Nov 23 '18 at 19:53













          0














          You need to move your script tag to the end of your html tag, so the page will render before the javascript call.



          <html>
          <head>
          <title>B</title>
          </head>

          <body>
          B <button id="backButton">Back</button>
          <script type="text/javascript" src="js/B.js"></script> //add script line here!
          </body>
          </html>


          To show the rendering problem, place the script tag right before the head tag and add a debugger in your B.js file and run the page with the developer tool open.



          $(document).ready(function() {
          debugger; // Add debugger here!
          console.log('B js ready');
          $('#backButton').on('click', function() {
          console.log('back button clicked');
          $.get('/back.html', function(data) {
          console.log('get results');
          document.write(data);
          });
          });
          });


          When you load the page B you will see that the button doesn't exist yet, so the script can't bind the click event.






          share|improve this answer


























          • Doesn't help...

            – user3668129
            Nov 23 '18 at 19:49











          • The problem happens because the $ (document).ready fires before the page rendering, this probably happens because the script thinks that the page is already ready, since you just changed the document content with javascript. I've improved my answer to better show you.

            – Gustavo Pereira
            Nov 24 '18 at 12:54
















          0














          You need to move your script tag to the end of your html tag, so the page will render before the javascript call.



          <html>
          <head>
          <title>B</title>
          </head>

          <body>
          B <button id="backButton">Back</button>
          <script type="text/javascript" src="js/B.js"></script> //add script line here!
          </body>
          </html>


          To show the rendering problem, place the script tag right before the head tag and add a debugger in your B.js file and run the page with the developer tool open.



          $(document).ready(function() {
          debugger; // Add debugger here!
          console.log('B js ready');
          $('#backButton').on('click', function() {
          console.log('back button clicked');
          $.get('/back.html', function(data) {
          console.log('get results');
          document.write(data);
          });
          });
          });


          When you load the page B you will see that the button doesn't exist yet, so the script can't bind the click event.






          share|improve this answer


























          • Doesn't help...

            – user3668129
            Nov 23 '18 at 19:49











          • The problem happens because the $ (document).ready fires before the page rendering, this probably happens because the script thinks that the page is already ready, since you just changed the document content with javascript. I've improved my answer to better show you.

            – Gustavo Pereira
            Nov 24 '18 at 12:54














          0












          0








          0







          You need to move your script tag to the end of your html tag, so the page will render before the javascript call.



          <html>
          <head>
          <title>B</title>
          </head>

          <body>
          B <button id="backButton">Back</button>
          <script type="text/javascript" src="js/B.js"></script> //add script line here!
          </body>
          </html>


          To show the rendering problem, place the script tag right before the head tag and add a debugger in your B.js file and run the page with the developer tool open.



          $(document).ready(function() {
          debugger; // Add debugger here!
          console.log('B js ready');
          $('#backButton').on('click', function() {
          console.log('back button clicked');
          $.get('/back.html', function(data) {
          console.log('get results');
          document.write(data);
          });
          });
          });


          When you load the page B you will see that the button doesn't exist yet, so the script can't bind the click event.






          share|improve this answer















          You need to move your script tag to the end of your html tag, so the page will render before the javascript call.



          <html>
          <head>
          <title>B</title>
          </head>

          <body>
          B <button id="backButton">Back</button>
          <script type="text/javascript" src="js/B.js"></script> //add script line here!
          </body>
          </html>


          To show the rendering problem, place the script tag right before the head tag and add a debugger in your B.js file and run the page with the developer tool open.



          $(document).ready(function() {
          debugger; // Add debugger here!
          console.log('B js ready');
          $('#backButton').on('click', function() {
          console.log('back button clicked');
          $.get('/back.html', function(data) {
          console.log('get results');
          document.write(data);
          });
          });
          });


          When you load the page B you will see that the button doesn't exist yet, so the script can't bind the click event.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 24 '18 at 12:49

























          answered Nov 23 '18 at 19:35









          Gustavo PereiraGustavo Pereira

          12




          12













          • Doesn't help...

            – user3668129
            Nov 23 '18 at 19:49











          • The problem happens because the $ (document).ready fires before the page rendering, this probably happens because the script thinks that the page is already ready, since you just changed the document content with javascript. I've improved my answer to better show you.

            – Gustavo Pereira
            Nov 24 '18 at 12:54



















          • Doesn't help...

            – user3668129
            Nov 23 '18 at 19:49











          • The problem happens because the $ (document).ready fires before the page rendering, this probably happens because the script thinks that the page is already ready, since you just changed the document content with javascript. I've improved my answer to better show you.

            – Gustavo Pereira
            Nov 24 '18 at 12:54

















          Doesn't help...

          – user3668129
          Nov 23 '18 at 19:49





          Doesn't help...

          – user3668129
          Nov 23 '18 at 19:49













          The problem happens because the $ (document).ready fires before the page rendering, this probably happens because the script thinks that the page is already ready, since you just changed the document content with javascript. I've improved my answer to better show you.

          – Gustavo Pereira
          Nov 24 '18 at 12:54





          The problem happens because the $ (document).ready fires before the page rendering, this probably happens because the script thinks that the page is already ready, since you just changed the document content with javascript. I've improved my answer to better show you.

          – Gustavo Pereira
          Nov 24 '18 at 12:54


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451953%2fwhy-event-not-firing-jquery%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen