Kumar
Kumar

Reputation: 5147

How to access JSON from an iframe originating from same domain?

In my webpage a hidden iframe is loaded with some JSON in it. This JSON is refreshed by some actions on the page. How do I access this JSON in iframe from my web page? for some unknown arcane unexplainable reason I am forced to use jQuery 1.3.2. so no $.parseJSON()

Upvotes: 7

Views: 19443

Answers (3)

Evi Song
Evi Song

Reputation: 882

The code @Paulpro posted:

var json = $.parseJSON($("#hiddeniframe").contents().text());

doesn't work for me.

I changed the code to:

var json = $.parseJSON($("#hiddeniframe").contents().find("*").first().text());

And now it works.

Upvotes: 1

db48x
db48x

Reputation: 3178

All modern browsers include a JSON parsing library:

var data = JSON.parse($("#hiddeniframe").contents().text());

If you need to support older browsers there are several libraries to choose from that will provide the same interface. The better ones will check to see if the browser is providing a native implementation and not override it, since it's bound to be faster.

See also JSON.stringify()

Upvotes: 8

Paul
Paul

Reputation: 141827

I think you can use:

var json = $.parseJSON($("#hiddeniframe").contents().text());

Something along those lines will work at least.

Upvotes: 14

Related Questions