How to execute javascript / jquery on a newly opened popup screen

So basically I have jquery code on a page where:

  • Open a new popup
  • Show Rail View Page
  • Manipulate items on a recently opened page

I don’t know what solutions exist, but I thought it should be very simple.

What code:

// open a popup window for example /fault_books/3 popup = window.open("/fault_books/" + <%= @fault_book.id %> , "popup"); // trying to get the scope of the element var module = $(".module-logo", popup.document.body) // manipulating the element $(module).hide(); 
+4
source share
2 answers

Not sure if this will be a cross browser, but you can try something like:

 var popup = window.open("/fault_books/" + <%= @fault_book.id %> , "popup"); $(popup.document).ready(function(){ var module = $(".module-logo", $(popup.document)) // manipulating the element $(module).hide(); }); 
+3
source

Why don't you just put your javascript code in the generated response from "/fault_books/" + <%= @fault_book.id %>"

In this case, just do it as usual:

 $(document).ready(function(){ var module = $(".module-logo") // manipulating the element $(module).hide(); }); 

And nothing needs to be done when opening a new window, just call:

 popup = window.open("/fault_books/" + <%= @fault_book.id %> , "popup"); 

This solution is simple and easier to maintain, since the scripts are self-contained. Scripts on the page perform tasks only for the content page. Say if you have another page that opens the same URL window.open("/fault_books/" + <%= @fault_book.id %> , "popup"); . You do not need to duplicate the code.

0
source

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


All Articles