0

So I am having an issue of actually getting my data to load correctly to a new HTML page. The goal is to make it so when a label is clicked, a new page opens up with the data from that respective label. This project is using D3.

I'm following instructions given in this question, but when I click on a label (let's say it has two results), I get [object Object],[object Object] as opposed to the actual data itself. Am I missing a step to get my actual data to output?

Code in parent:

label.on('click', function(){
    var OpenWindow = window.open("results.html", "mywin", '');
    OpenWindow.dataFromParent = results; 
    OpenWindow.init();
});

Code in child

<script type="text/javascript">
var dataFromParent;    

function init() {
    document.write("<p>" + dataFromParent + "</p>");
}

init();

</script>

Any help would be greatly appreciated! Thanks!

Community
  • 1
  • 1
BDD
  • 625
  • 18
  • 29
  • 1
    Because that is what toString() does on an object. You need to write the data out a different way. – epascarello Nov 03 '14 at 19:24
  • How do you mean in a different way? Do you mean I need to write it out as something other than the format it's in (currently JSON)? – BDD Nov 03 '14 at 19:49
  • Yes...what do you expect the browser to output to the page? Do you want to see the JSON object as a string? – epascarello Nov 03 '14 at 19:51
  • Yeah, that's what I'm looking to happen. How would I go about doing that? – BDD Nov 03 '14 at 19:54

1 Answers1

1

From the comments you said you want to show the conetents of a JSON object.

What you have is

document.write("<p>" + dataFromParent.toString() + "</p>");

What you want is

document.write("<p>" + JSON.stringify(dataFromParent) + "</p>");

Now it makes NO sense that you are calling init in the parent window and also in the child window. You need to realize that the child window takes time to load so calling the method from the opener, it might not be there, same goes with setting the value.

Also document.write() is a bad idea.

epascarello
  • 185,306
  • 18
  • 175
  • 214
  • Why is document.write() a bad idea? If I want it to write out the data, isn't that the correct way to approach it? (I'm still reasonably new to the JavaScript world) – BDD Nov 03 '14 at 20:04
  • http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – epascarello Nov 03 '14 at 20:06
  • Thank you for all the help epascarello! – BDD Nov 03 '14 at 20:08