简体   繁体   中英

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

So basically I have jquery code on a page where:

  1. Open a new popup window
  2. Display rails view page
  3. Manipulate items on a newly opened page

Don't know what sort of solutions are there, however I thought it should be really easy.

That's the 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();

Not sure it will be cross-browser, but you may 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();
});

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

In that case, just do it as normal:

$(document).ready(function(){
    var module = $(".module-logo")

    // manipulating the element
    $(module).hide();
});

And don't need to do anything when opening a new window, just call:

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

This solution is simple and easier to maintain as scripts are self-contained. Scripts on a page do jobs only for the containing page. Let's say, if you have another page opening the same url window.open("/fault_books/" + <%= @fault_book.id %> , "popup"); . You don't have to duplicate code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM