4

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();
palaѕн
  • 72,112
  • 17
  • 116
  • 136
Jackie Chan
  • 2,654
  • 6
  • 35
  • 70

2 Answers2

3

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();
});
Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
jbl
  • 15,179
  • 3
  • 34
  • 101
  • it doesn't solve the problem, but it was close... I just didn't realise that Rails provide multiple layouts for a single set of views... well my problem was solved – Jackie Chan Jun 12 '13 at 13:54
0

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.

Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
Khanh TO
  • 48,509
  • 13
  • 99
  • 115