0

I'm having an issue with sending some HTML code using AJAX please see my code below

<iframe src="http://www.w3schools.com" width="10" height="10" id="awc_frame"></iframe> <script>var iframe = document.getElementById("awc_frame");</script> 

Here is the AJAX code below

<script> $.ajax({ type: "POST", url: "mobileView.php", data: { val : iframe }, success: function(data){ console.log(data); } }) </script> 

The code isn't sending the variable to the PHP file. Looking into the Network side of things it sends text ie if I put "" around iframe it sends this code "val = iframe" but not the actual code within the iframe. The "var iframe"does work and pulls back the HTML code of the iframe

Please tell me what I'm doing wrongly.

Thanks in advance.

EDIT: I'm sorry. It's not the HTML code within the iFrame I need to send, It's the entire iFrame code I need to send.

Another Edit: What I'm trying to accomplish when a visitor from my company goes to my website I would like Javascript or Jquery to load an internal website from the visitors computer and then have all of the code from that website that's on the client's end to be sent to a Server which will store the entire iFrame code in a database.

2
  • What is exactly that you want to send here? IFrame's loaded HTML? Commented Sep 4, 2016 at 9:50
  • Yes. The entire loaded HTML from the iFrame needs to be sent to a PHP document where it will be uploaded to a database Commented Sep 4, 2016 at 9:59

4 Answers 4

0

First of all, var iframe does not contain HTML of the iframe element - it contains a DOM Node, which is kind of a wrapper around the iframe element (it contains various properties of that element, including the HTML).
Next thing, you probably want to wait for the iframe to completely load all the contents, so you'll have to bind to the load event of it.

Something like this should work:

var $iframe = $("#awc_frame"); $iframe.on("load", function () { var iframeHTML = $iframe[0].contentWindow.document.body.innerHTML; // jQuery alternative var iframeHTML = $iframe.contents().find("body").html(); $.ajax({ type: "POST", url: "mobileView.php", data: { val: iframeHTML }, success: function(data){ console.log(data); } }); }); 

Super important thing in this example

Just one more thing - please note that for websites outside of your own domain, this code won't work (due to Same Origin Policy). Any other code won't work too.

Sign up to request clarification or add additional context in comments.

13 Comments

That's because you are trying to fetch domain outside of your own domain. As I already told - it's impossible to do.
I don't understand but if I go into Console and try calling iframe it works. But why doesn't it send the code?
Read about Same Origin Policy. Things you have already loaded into your browser are not exactly the same things that JavaScript lets you reference.
I tried the same thing on my server and had the same error. I even placed a crossdomain file allowing access from all. And the error occurred.
It's getting all the code and place it in a var which is okay but then it will not send via AJAX to a PHP script.
|
0

Since javascript has problems with getting the HTML from a cross-domain iframe, you can't do this across domains. However, why not just send the iframe's src attribute to the PHP page, and then just use file_get_contents to get the HTML, and then store that? Problem solved:

Javascript:

var iframe = $('#awc_frame').prop('src'); $.ajax({ type: "POST", url: "posttest.php", data: { val : iframe }, success: function(data){ console.log(data); } }); 

PHP:

$html = file_get_contents($_POST['val']); 

1 Comment

Because the page that needs to be loaded is on an intranet and the web server is not on that intranet domain so it can't load or access the website on the server end. Only the client can see the page as their on the domain/intranet.
0

what are you trying to do?

var iframe = document.getElementById("awc_frame"); 

above code is an javascript object of your iframe which contains a lot of properties.. since you are using jQuery, you could get that with:

var iframe = $('#awc_frame'); 

keep in mind that above code is the element it self in jquery object format you could get element object like this:

var iframe = $('#awc_frame')[0]; 

** you're doing something wrong.

if you're trying to get iframe HTML content:

var iframe_contents = $("#awc_frame").contents(); 

if you explain more about what you are trying to do, i can update my answer to suit you.

* UPDATE * considering what you are trying to do..

Method #1: (Easy Way)

you could use php to fetch content of the website you need:

<?php $contents = file_get_contents('http://www.w3schools.com'); // Saving $contents to database... ?> 

Method #2: (Hard Way)

as @mdziekon said, you first should wait until your iframe gets loaded then:

var iframe = $("#awc_frame"); iframe.on("load", function () { var contents = $(this)[0].innerHTML; $.ajax({ type: "POST", url: "mobileView.php", data: { val: contents }, success: function(data){ console.log(data); } }); }); 

hope it solves your problem

18 Comments

Yes I need all of those properties in the code, So the entire code from an iFrame with all the fully loaded website sent to the PHP document.
it think it's better for you to change your scenario, instead of loading website content on your browser and sending it's content to server, you could do the same with your PHP script.
@HaroonBaig why do you need properties of iframe in your php script?
I need a client-side user to load an iFrame on their side and then that needs to be sent to a server.
i understand that, what are you trying to achieve? sending iframe DOM to server is impossible because it's useless..
|
-1

This would send the entire html inside the iframe.

var iframe = $('#awc_frame').html(); 

2 Comments

Yes. I need to send the entire html code within the iframe
This won't return the contents of the iframe.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.