Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts

    Displaying content from one site on another

    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?
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #2
    Regular Coder
    Join Date
    Nov 2007
    Location
    Chicago
    Posts
    134
    Thanks
    2
    Thanked 9 Times in 9 Posts
    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.

  • #3
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    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?
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #4
    Regular Coder
    Join Date
    Nov 2007
    Location
    Chicago
    Posts
    134
    Thanks
    2
    Thanked 9 Times in 9 Posts
    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.

  • #5
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    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?
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #6
    Regular Coder
    Join Date
    Nov 2007
    Location
    Chicago
    Posts
    134
    Thanks
    2
    Thanked 9 Times in 9 Posts
    Correct. If you wanted to use a JS framework, you'd have to somehow include that JS code.

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,379
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Quote Originally Posted by arthurakay View Post
    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:
    Code:
    <html>hello <script scr="myphp.php?userid=fred" defer="defer" ></script></html>
    php: (untested imaginary do-nothing example)
    Code:
    echo "document.write(" . json_encode(GET['userid']) . ")";
    results:
    Code:
    hello fred
    Last edited by rnd me; 09-24-2009 at 11:49 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    Yep, Rnd speaks truth. Go look at the code in some of those Google ".js" files, sometime. There ain't no AJAX in them.

  • #9
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    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?
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #10
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,379
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Quote Originally Posted by hinch View Post
    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.


    Quote Originally Posted by hinch View Post
    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...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #11
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    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
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #12
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    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

    PHP Code:
    <!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 Code:
    <?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
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #13
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,379
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Quote Originally Posted by hinch View Post
    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()...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #14
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    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 ?
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #15
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,379
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Quote Originally Posted by hinch View Post
    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.
    Last edited by rnd me; 09-26-2009 at 10:15 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •