Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

Yes. The entire loaded HTML from the iFrame needs to be sent to a PHP document where it will be uploaded to a database – Haroon Baig Sep 4, 2016 at 9:59

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.

That's because you are trying to fetch domain outside of your own domain. As I already told - it's impossible to do. – mdziekon Sep 4, 2016 at 10:09 I don't understand but if I go into Console and try calling iframe it works. But why doesn't it send the code? – Haroon Baig Sep 4, 2016 at 10:10 Read about Same Origin Policy. Things you have already loaded into your browser are not exactly the same things that JavaScript lets you reference. – mdziekon Sep 4, 2016 at 10:12 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. – Haroon Baig Sep 4, 2016 at 10:26 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. – Haroon Baig Sep 4, 2016 at 10:32

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); 
$html = file_get_contents($_POST['val']);
                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.
– Haroon Baig
                Sep 4, 2016 at 10:53

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:

$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

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. – Haroon Baig Sep 4, 2016 at 10:01 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. – Pezhvak Sep 4, 2016 at 10:10 I need a client-side user to load an iFrame on their side and then that needs to be sent to a server. – Haroon Baig Sep 4, 2016 at 10:21 i understand that, what are you trying to achieve? sending iframe DOM to server is impossible because it's useless.. – Pezhvak Sep 4, 2016 at 10:29

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.