7

I'm working on a little project in JavaScript/jQuery.

In order to display results from a calculation done in javascript, I'd like to open a new window with some predefined content and modify this content in order to display the results: Im using code like this:

var resultwindow = window.open('result.html')
var doc = $('body', resultwindow.document);
doc.append("<p>Result</p>")

This is not working since the result document is not yet loaded when I append the content, so it is overwritten with the contents of 'result.html'.

I also tried

$(resultwindow.document).ready(function() {
    // ... Fill result document here
})

and

$(resultwindow.document).load(function() {
    // ... Fill result document here
})

but ready() works only on the current document (it is called immediately, if the current document is already loaded), and load doesn't get called at all.

Perhaps someone can point me to the right direction. Thanks in advance!

EDIT:

I finally solved this by creating the new document "by hand" in Javascript like:

w = window.open('','newwinow','width=800,height=600,menubar=1,status=0,scrollbars=1,resizable=1);
d = w.document.open("text/html","replace");
d.writeln('<html><head>' +
  '<link rel="stylesheet" type="text/css" href="style.cs"/></head>' +
  +'<body></body></html>');
// use d to manipulate DOM of new document and display results

If I were to do the same thing today (two years of experience later), I'd use some Javascript template library like Handlebars to maintain a template and compile it to javscript.

2
  • Martin did you get this working? I have a similar issue. Commented Aug 8, 2012 at 15:40
  • @Shaunak I posted my solution from two years ago as edit to the question, also mentioned how I would solve it today... hth. Commented Aug 9, 2012 at 11:19

3 Answers 3

2

Your load call doesn't work because you're attempting to handle the load of the document, and chances are document does not even exist at this point. Which means you are passing null into jQuery, and it gracefully ignores you. Handle the load event of the raw window reference instead, and then you should be good to go...

var win = window.open("result.html");
$(win).load(function() {
  $("body").append("<p>Result</p>");
});
Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately, the event handler never gets called. Also the debugger reveals that - at least in Chrome - the document object already exists but is empty ("about:blank").
1

The problem you have is that load() doesn't do what you think it does.

Instead, use bind("load", function() { /* Your function here */ }); then everything should work.


Correction:

load() is actually a dual-use function -- if it's called with a function as its first parameter, then it binds it to the load event of the object (or objects) in question, otherwise it loads the returned data (if any) into the elements in question. See Josh's answer for the real reason why it's not working.

5 Comments

Wrong. The load in jQuery is smart enough. It fires based on parameters passed in. So if you pass in a function only, it knows that you mean the onload event. If you pass in all the required parameters for an AJAX call, it does that.
@Harv It is wrong. Load should have worked fine for the OP, and there is a reason it did not. See my answer for a detailed explanation.
@Josh -- very nice, I did not know about that. Thanks!
@Sean No problem! jQuery has several functions that behave differently depending on arguments. Take attr for example. You pass in one argument, you get an attribute, pass in two arguments and you set an attribute! Kind of crazy.
@Josh -- oh, that I know -- it makes it convenient as all get out though. I expected that all of the uses would be documented in one place (or prominently linked to) -- so when I checked api.jquery.com and saw only the one use documented under load, I naively assumed that that was all it did. One deep dive into the source later, and I've learned something new :-D. Thanks again!
0

Send the data to result.html in the querystring and then have the result.html display the data from there. If you want to be less obvious about it going through you could hash the data in the querystring and have the result page dehash it.

2 Comments

The software should also work offline just from the filesystem (i.e. without any web server). Therefore your suggestion is not an option, I think. Thanks anyway.
Not sure why you would want to build a web page or a series of web pages that don't actually live on some sort of web server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.