-1

I am facing the following problem:

On a webpage  page1 I have to click on a link.The second page opens on the same container (the link does not change plz check the photos)page2.

On the second page I have to execute some jquery. I am trying the following code:

`   $(document).ready(function(){
   $("a").click(function(event){
     alert("Thanks for visiting!");
        var vr1= document.getElementsByClassName("hc-inline-block hc-px1 hc-rounded hc-white")[0].innerText;
        alert("vr1 is: " + vr1);

    });

 });`

The problem of this code is that it gets executed after the link is clicked but before the second page is loaded. This makes the class unavaiable...

Is there anyway I can execute some Jquery after the second page is fully loaded???

Alduxo
  • 69
  • 5
  • Does this answer your question? [How to do jquery code AFTER page loading?](https://stackoverflow.com/questions/2926227/how-to-do-jquery-code-after-page-loading) – TheHunter Shergill Jan 19 '22 at 16:41
  • I have tried those solution but they do not work because the jquery get executed after the fist page is loaded... I need the jquery to be executed after the second page gets loaded.. – Alduxo Jan 19 '22 at 16:43
  • How is the second "page" being "loaded"? Is this jQuery code on the first "page" or is it on the second "page"? Are these even separate "pages" at all, such as in an `iframe`, or are you just dynamically hiding/showing content, or fetching data via AJAX, or something else? It's really not clear what specifically is happening here. Can you provide more of a [mcve] demonstrating? Because according to the code shown what you have is a click handler attached to all `` elements in the document, which I would certainly expect to be executed when clicking on an `` element in the document. – David Jan 19 '22 at 16:46
  • I am not sure about how you included the jQuery code file. Have you tried including your jQuery Code specifically on the second page? – TheHunter Shergill Jan 19 '22 at 16:47
  • Sorry if I have not been very clear... I am working on a wordpress template. The pages are not separated. When clicking on the link, the dom code of the second page gets loaded inside the dom code of the first page. The jquery is placed on a .js file which checks at what page we are based on the url. Actually if I choose to open the new webpage on a new tab everything works good but this is not the case. I can not change the fact that the second page should be loaded on the same page as the first one... – Alduxo Jan 19 '22 at 17:04
  • I guess that the problem must be in your selector. Check again that, and remember using the browser console when debugging, you'll get live output while you type in what you're testing. And I want to suggest you to try also selecting with plain jQuery code, you may get a surprise that way. – carloswm85 Jul 18 '22 at 17:42

1 Answers1

0

SetTimeout function worked on my case.

setTimeout(function() {

    // do stuff here

}, 1000);
Alduxo
  • 69
  • 5