...

View Full Version : Displaying content from one site on another



hinch
09-24-2009, 07:52 PM
You've all seen it before banner codes/rss feed displayers/twitter feeds/etc etc

all you do is copy and paste a <script src="http://somewebserver/display.js"></script> somewhere on your website and it retrieves the html from the remote server and displays it on your site.

My question as simple as it sounds is how the hell does this stuff work?

I have a back end php page that pulls the required information from my DB based off a userid passed to it ie: mywebserver.com/display.php?id=xxxx

Is the javascript just a simple ajax call to my php page passing straight on the userid passed to it or is there something more complex to it?
I was always under the impression you couldn't do remote ajax calls to a page on a different server, which if I get this right a script off my server included on another domain becomes a member script of that domain and not my own?

arthurakay
09-24-2009, 08:05 PM
You absolutely can call remote pages via AJAX. Otherwise, none of Google's APIs would work.



Is the javascript just a simple ajax call to my php page passing straight on the userid passed to it ... ?


And yes, I think you're right. While I've never really looked under-the-hood at these kinds of display ads, I'm pretty sure that's exactly how it works. The JS code may create some DOM elements, then fill them with images or text.

hinch
09-24-2009, 09:21 PM
basically what I "want" :) to do is this.

I have a XML feed from one of my sites (forums) and I want to create a "block" of code that others can just copy and paste into their website and get the 10 latest posts from my forums.

So as I understand it :)

I write some JS that others can include in their page.
This JS is just a simple ajax request to my rssparse.php page.
My rssparse.php page reads my XML feed and applies a preset CSS to it and returns html
My JS page then just does document.write(returned values from ajax call)

Think that would work? or can you think of any better ways of doing it?

arthurakay
09-24-2009, 09:25 PM
That sounds about right to me.

The only problem I can see is the CSS... if it wasn't inline, your users would also have to include a CSS file - otherwise your CSS might not be applied.

hinch
09-24-2009, 09:58 PM
it'll all be inline pulled from a DB as I want to give users the ability to select multiple css/layouts/forum sections to get the posts from.

Am I right to assume that I'd have to manually write the callbacks and stuff into the .js and can't use prototype or jquery as then that would also have to be included on the clients site if I did?

arthurakay
09-24-2009, 10:03 PM
Correct. If you wanted to use a JS framework, you'd have to somehow include that JS code.

rnd me
09-25-2009, 12:45 AM
You absolutely can call remote pages via AJAX. Otherwise, none of Google's APIs would work.

And yes, I think you're right. While I've never really looked under-the-hood at these kinds of display ads, I'm pretty sure that's exactly how it works. The JS code may create some DOM elements, then fill them with images or text.

no and no.
you CANNOT call remote pages via AJAX (at least not using last year's browsers anyway).

you can turn your content into valid JavaScript, which unlike ajax, can be pulled from anywhere.

php has the json_encode feature, which will turn strings into valid javascript.

look into jsonp; it's more-or-less how ads work.

once you get the content string into the page, the callback function injects it into a dom container.

you can use dom methods, innerHTML, or document.write to achieve this.


using document.write, it's dead simple:

html:

<html>hello <script scr="myphp.php?userid=fred" defer="defer" ></script></html>


php: (untested imaginary do-nothing example)

echo "document.write(" . json_encode(GET['userid']) . ")";

results:

hello fred

Old Pedant
09-25-2009, 01:22 AM
Yep, Rnd speaks truth. Go look at the code in some of those Google ".js" files, sometime. There ain't no AJAX in them.

hinch
09-25-2009, 11:35 AM
i'm horrible with JS so the google stuff made little sense to me :(

so basically then PHP page creates html and everything I need to output for me stores it as a var.

can I json encode the entire lot then and return that via the document.write in the remote website js then?

rnd me
09-25-2009, 12:29 PM
so basically then PHP page creates html and everything I need to output for me stores it as a var.

yes/perhaps; the important thing is to get the data from php.
you can build the html with javascript as well, and that's probably the preferred way these days.




can I json encode the entire lot then and return that via the document.write in the remote website js then?
if you made the html with php, otherwise you want to use dom methods to build/inject your content as you transform the data into html.
note that you can't use document.write after the page loads, so if you want partial-page refreshes, it makes sense to put in the extra time to create customized javascript injectors.


most major javascript libraries make this process a whole lot simpler, and they have lots of examples floating around.


if you like php better, and don't need updates between refreshes, use document.write.
it's a lot simpler and it recently stopped sucking, so long as you use the defer attribute in the script tag...

hinch
09-25-2009, 02:04 PM
yeah since my JS skills are pretty poor I'm hoping to create all the html/css from the php page then just pass it back to the initial JS call as a json string basically which i then decode and just spit out to screen.

if i have to start arsing around with the DOM and using JS for display on the end user script i'm really going to run a ground and fail.

I assume from your comments about document.write is that I can use it on page load but I can't do those funky timed ajax refreshes on it without using a full page refresh?
This tbh is perfectly fine though since I only need to display the information once on initial page load no funky timed refreshes required :)

hinch
09-25-2009, 02:58 PM
right done a little testing it seems I don't really need to use json if I just echo the html straight out in a document.write via the php script.

This is my local test script



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>Testing inclusion from remote server</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<body>
<script src='http://www.angel-computers.co.uk/remote.php?uid=1&fid=2' type='text/javascript' defer="defer"></script>
</body>
</html>


This is my remote.php script


<?php
$uid = $_GET['uid'];
$fid = $_GET['fid'];
echo "document.write('<strong>UID Submitted =</strong> {$uid}<br />');";
echo "document.write('<strong>FID Submitted =</strong> {$fid}');";
?>


Outputted results are
UID Submitted = 1
FID Submitted = 2

This should work pretty much how I think now so I'm going to expand a little and start an xml pull from the forums going to start off just spitting it all out with no format and build up the html slowly so eventually it should spit out the entire thing.

escaping single and double quotes is going to be fun :)

rnd me
09-26-2009, 02:30 AM
right done a little testing it seems I don't really need to use json if I just echo the html straight out in a document.write via the php script.
...
escaping single and double quotes is going to be fun :)

that why i said to use json_encode; it handles quotes, breaks, and all the other bad string mojo that break javascript, turning it into a guaranteed valid string with one function call: json_encode()...

hinch
09-26-2009, 11:01 AM
ok don't I need to json decode in the js at the other end though? is there a pre-set function for that or is it going to have to be manually coded up ?

rnd me
09-26-2009, 11:10 AM
ok don't I need to json decode in the js at the other end though? is there a pre-set function for that or is it going to have to be manually coded up ?

no, the output of json_encode( phpString ) is a valid javascript string expression, escaped and end-quoted...

you only need to 'decode' when you load it via ajax, which you've indicated isn't an option for you. as is, the json will arrive to a script tag as a live javascript variable, essentially a hard-coded declaration.


if you want to pass a whole phpAssocArray or something complex like that, you need a 'handler'. Often though it's really just a path like gramps.mom.me that points to the data you need relative to the structure of the incoming assocArray, which is a javascript object.

hinch
09-26-2009, 11:12 AM
right so in the case of my example above it'd just become



echo "document.write('".json_encode(."<strong>FID Submitted =</strong> {$fid}".)."');";

rnd me
09-27-2009, 12:09 AM
right so in the case of my example above it'd just become



echo "document.write('".json_encode(."<strong>FID Submitted =</strong> {$fid}".)."');";

no, you are still adding extra quotes...
json_encode is supposed to be simple.

look at the example i posted; you go object to object, function to function.

im not a php guy, but i think it's something like:

echo "document.write(" . json_encode("<strong>FID Submitted =</strong>" . $fid ) . ")";

hinch
09-27-2009, 11:58 AM
{$fid} is the same as ".$fid." anyway so your examples the same as mine just I put in extra single quotes '

I thought document.write would need them to write out the json string but if it doesn't that makes it a little bit simplier



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum