2

My issue is following: My Partner's have websites and I want that partners can include a script from my server that dynamically generates a link and an image.

i.e. myscript.php (on my server) generates following output:

<a href="http://www.myserver.com/apples.php?12345">
<img src="http://www.myserver.com/images/12345.png" />
</a>

I don't want my partners to be bothered with any php scripting, hence the best solution for now is to supply them with an iframe, where the src is the link to my script on my server that generates the output above.

However I clearly don't have any control over my partner's website and i.e. opening a lightbox out of the iframe certainly won't work.

Because of that I tried a different approach. I used Ajax to dynamically load my script, however this also doesn't work due to ajax security features (you cannot call an external script via ajax - only if you do a php workaround).

So basically, my question is: Is there any good solution to call my script on my server on the partner's website and display the generated code on my partner's website without the use of php?

0

3 Answers 3

2

Have your PHP generate JavaScript (don't forget the content-type header). Then they can include it with a <script> element.

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

5 Comments

you mean having the link and the image in the js code? Not sure how I can accomplish that?
+1 This is the standard used for "drop in" plug ins -- Google Analytics, Facebook share/like buttons, Tweet buttons, etc.
@Mike - document.write is common. In other words, you say "include <script type="text/javascript" src="http://yourwebsite/script.js?their_site_id"></script> in your HTML" and then at the script.js url you output some document.write or append the body or something else.
Hey Renesis, wow thanks this seems to be THE great solution. Can I generate the JS file on the fly or can I also link to let's say: <script type="text/javascript" src="yourwebsite/script.php?their_site_id"></script> and include the correct javascript header in my PHP file? Otherwise I can't call php logic in the js file right?
@Mike Sorry, I was a bit misleading with the .js extension -- I meant a PHP-generated script file with the right text/javascript header. I also typically mod_rewrite the URL from .js so that it looks like a JS file even though it's actually PHP. Remember to do the correct cache header and HTTP 304 stuff so that you don't have to send data if it hasn't changed!
1

Make a PHP file, like

<?php
$url = "http://www.myserver.com/apples.php?12345";
$img = "http://www.myserver.com/images/12345.png";
echo "document.getElementById('divthing').innerHTML = '<a href=\"" . $url . "\"><img src=\"" . $img . "\" /></a> '";
?>

Your partner's page would be like:

<html>
<body>
Hey, check this site out: <div id="divthing"></div>
<script type="text/javascript" src="http://yoursite.com/script.php"></script>
</body>
</html>

(I know, not really clean code (innerHTML etc), but you should get the idea :))

1 Comment

this is actually the approach I am trying. I tryed to combine that with above code (1st answer). Will update my question once I figured out some things. Thanks! :-)
0

Could you make it with javascript file which replace/creates that anchor where ever you put the javascript link.

picture.js:

$('#image').ready(function(){
  var image = 'http://host/url/to/image.jpg';
  $('#image').load(image);
});

on you partners site:

<div id="image">
 <script type="text/javascript" src="http://yourhost/picture.js"></script>
</div>

I don't know if it possible, but.. :) and this needs jQuery. And im slow.

1 Comment

No sorry, this is not possible, but thanks anyway. You cannot load external ressources via load due to ajax security features.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.