-3

Maybe a stupid question, but I'm wrecked from this issue.

The issue: cross domain loading of webpage via javascript and having access to variables and elements within

Theoretical solution:

  1. Send an ajax request to a local php file with the requested URL as a parameter
  2. Get the PHP file to load the page
  3. Display the loaded page and have access to the elements in a box specified

Before I endeavor, is something like this possible or worth the work involved? I am familar with PHP Simple HTML DOM Parser.

Thanks in advance, have spent quite some time on this issue already.


Update

First, the javascript:

function loadTheUrl(x){      // x = the url, e.g. http://example.com
    $.ajax({ url: 'loader.php', // explained below
         data: {url: x},
         type: 'get',
         success: function(output) {
                      $('.loading-space').html(output);
                  }
            });
         
  } 

This calls a local PHP file called loader.php

loader.php

<?php

    echo file_get_contents($_GET['url']);
    

And that's it. This works, but it actually gives me some issues with CSS that I am trying to figure out. The CSS is overriding my site too.. I'd like to keep the CSS, but limit it to within that div.


Update

A semi-working example. Only works in Chrome due to data:text/html.

HTML

<div value='http://example.com' onClick='javascript:loadURL(this)'>Click Here</div>

JavaScript

function loadURL(x){
    var xURL = $(x).attr('value');
    $.ajax({ url: 'load.php',
         data: {url: xURL},
         type: 'get',
         success: function(output) {
                $('.page-loaded').attr("data", "data:text/html,"+output); // .page-loaded is an HTML object element
                  }
            });
  } 

load.php

<?php
    error_reporting(0);
    $doc = new DOMDocument();
    $doc->loadHTML(file_get_contents($_GET['url']));
    echo $doc->saveHTML();
  

Just curious as to why the downvotes are coming in. I would have thought this issue would have been more widespread? Thanks


Update

PHP way doesn't allow to access elements. CORS seems the only way to go. Just need to limit CSS to within the box.

Community
  • 1
  • 1
shanehoban
  • 870
  • 1
  • 9
  • 30
  • is the other domain another that you control, or is it controlled by someone else? – Jeff Lambert Jul 15 '14 at 14:16
  • I don't have control of the other site, but they are using a solution I've provided. I am just trying to replicate the site without the ordinary callbacks. – shanehoban Jul 15 '14 at 14:19

2 Answers2

0

Is this what you are looking to do?

   <script>
    $(function(){
      $.get("youFile.php", function(data){
          $("#container").html(data);
      });
    }):
    </script>
Mark
  • 4,773
  • 8
  • 53
  • 91
  • Yes but it's cross domain, so I don't have access to the elements/variables. Trying for a work around. All I want is to display the page really, and ensure no onbeforeunloads are initiated. – shanehoban Jul 15 '14 at 14:15
  • If it's cross domain, I don't think there is going to be a solution. – Mark Jul 15 '14 at 14:17
  • Using a local PHP file I think it can be done, I've got the page contents into the div, but the CSS included is causing issues with my site. – shanehoban Jul 15 '14 at 14:23
  • Could you provide a fiddle of what you are trying to do? I'm a little confused, cross site scripting and manipulating a local php file are a little different in my opinion. – Mark Jul 15 '14 at 14:25
  • can't fiddle with php file loading the actual URL but have updated the above with the three files that work together – shanehoban Jul 15 '14 at 17:10
0

You could use DOMDocument, find all the script and link tags, remove them, and spit out the HTML:

<?php
$doc = new DOMDocument();
$doc->loadHTML(file_get_contents($_GET['url']));
$internal_css = $doc->getElementsByTagName('style');
foreach ($internal_css as $css) {
    $css->nodeValue = "#container_id " . implode(" #container_id ", explode("}", $css->nodeValue));
}
echo $doc->saveHTML();
dave
  • 62,300
  • 5
  • 72
  • 93
  • clever solution; but what I'm trying to keep the CSS but limit it to just within the div I'm displaying the file in - if that's possible? – shanehoban Jul 15 '14 at 14:41
  • 1
    This edit shows how to do it for anything inside a ` – dave Jul 15 '14 at 16:10
  • Thanks @dave, appreciate it but it's still not working as intended. I wonder can I create the DOM element and pass it as a 'file' via `DOMDocument::saveHTMLFile` and initiate an object element with that 'file' as the data? Or something along those lines.. Thanks anyways! – shanehoban Jul 15 '14 at 16:17