admin管理员组

文章数量:1392007

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();

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();
Share Improve this question edited Jun 11, 2013 at 12:28 palaѕн 74k17 gold badges122 silver badges139 bronze badges asked Jun 11, 2013 at 12:16 Jackie ChanJackie Chan 2,6626 gold badges36 silver badges71 bronze badges 6
  • Or may be there is a way to add javascript to a created popup? – Jackie Chan Commented Jun 11, 2013 at 12:17
  • Did you try: module = popup.$("body")? – Stijn Geukens Commented Jun 11, 2013 at 12:23
  • Or try : module = $(".module-logo", popup.document.documentElement.outerHTML); – Mohammad Areeb Siddiqui Commented Jun 11, 2013 at 12:24
  • What does happen when that code executes? Note: "nothing" is not a suitable answer; it's at best inplete and at worst an outright lie. Have you checked your browser's console/developer tools for errors? – Anthony Grist Commented Jun 11, 2013 at 12:34
  • 1 You must wait for the popup content to be loaded before executing your script. Have a look at stackoverflow./a/3030893/1236044 – jbl Commented Jun 11, 2013 at 12:35
 |  Show 1 more ment

2 Answers 2

Reset to default 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();
});

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.

本文标签: How to execute javascriptjquery on a newly opened popup screenStack Overflow