/    Sign up×
Community /Pin to ProfileBookmark

Help with Remote HTML Includes

Hi,

My apologies for my 1st post being a request for help… ?

Right, the scenario….

I have a bunch of HTML files which are adds to be included on sites all over the place. One file at a time, at random, would be included, and displayed on other sites and pages.

They are sometimes just a banner image with a link, other times, they are quite a bit of HTML, with a form, and more.

They all sit on 1 domain. Now, if being called from this same domain, it’s very easy.

On the SAME domain, I can do this in PHP:

[CODE]
srand((float) microtime() * 10000000);

$add[1]=’1.html’;
$add[2]=’2.html’;
$add[3]=’3.html’;
$add[4]=’4.html’;
$add[5]=’5.html’;

$rn = array_rand($add);

include(“$add[$rn]”);
[/CODE]

BUT, I can’t call that PHP file from different domains.

JS can be executed from different domains.

My JS is non-existent. ?

Any ideas on what the JS file should look like?

I know once I have the JS, I can just call it in HTML using <script></script>

Appreciate any help you can offer.

Kind Regards,

Richelo Killian

to post a comment
JavaScript

31 Comments(s)

Copy linkTweet thisAlerts:
@MrNobodyNov 24.2008 — Seems you could place code such as the following in the BODY section of a web page at the point where you wanted the ad to be displayed:
[code=html]<script type="text/javascript">
var myads =
[
'1.html',
'2.html',
'3.html',
...etc...
'n.html'
];
document.writeln('<iframe src="', myads[Math.round(Math.random()*(myads.length-1))],
'" style="border:0 solid black;"></iframe>');
</script>[/code]
Copy linkTweet thisAlerts:
@RicheloauthorNov 24.2008 — Hi MrNobody,

Thanks for your reply.

That would work, BUT, I would prefer to include a single JS file on MANY pages, and when I need to update the adds, I can just update the 1 JS file, instead of all the pages the code lives on.

I could just modify the HTML files being called, but, I WILL be adding more files to be loaded.

Thanks again for your speedy reply!

Cheers

Richelo
Copy linkTweet thisAlerts:
@MrNobodyNov 24.2008 — That would work, BUT, I would prefer to include a single JS file on MANY pages, and when I need to update the adds, I can just update the 1 JS file, instead of all the pages the code lives on.[/QUOTE]
So what's your problem? What I supplied works just as well from a js file.
Copy linkTweet thisAlerts:
@RicheloauthorNov 24.2008 — Hey MrNobody,

Thanks for that! Sorry, I am not a JS programmer at all.

Will implement that right now!

Thanks so much for your help!

Apologies for the total newbie questions and comments!

Cheers

Richelo
Copy linkTweet thisAlerts:
@RicheloauthorNov 24.2008 — Nope, did not work.

I put the following in test.js on 1 server:

[CODE]
<script type="text/javascript">
var myads =
[
'1.html',
'2.html',
'3.html',
'4.html',
'5.html'
];
document.writeln('<iframe src="', myads[Math.round(Math.random()*(myads.length-1))],'" style="border:0 solid black;"></iframe>');
</script>
[/CODE]


Then, I created a html file, and placed the following line, before the close of the body tag:

[CODE]
<script language=javascript src="http://www.imnica.com/adds/test.js"></script>
[/CODE]


You can see here: http://www.imnicamail.com/test.html it's not working.

It SHOULD display on of Test 1 through 5 in H2, center at the bottom of the page.

Thanks again for all the help.

Cheers

Richelo
Copy linkTweet thisAlerts:
@rnd_meNov 24.2008 — Nope, did not work.

I put the following in test.js on 1 server:

[CODE]
<script type="text/javascript">
</script>
[/CODE]
[/quote]

that's the promlem: you can't put tags in .js files, only javascript code.
Copy linkTweet thisAlerts:
@MrNobodyNov 24.2008 — By the way, I had a suspicion so I just did some testing and it seems this random number generation:
[CODE]Math.round(Math.random()*(myads.length-1))[/CODE]
should be changed to this for a more even dispersal of the randomly generated numbers:
[CODE]Math.floor(Math.random()*myads.length)[/CODE]
Copy linkTweet thisAlerts:
@RicheloauthorNov 24.2008 — Thanks so much for all the awesome help so far!

This is what my test.js file now looks like:

[CODE]
var myads =
[
'1.html',
'2.html',
'3.html',
'4.html',
'5.html'
];
document.writeln('<iframe src="', Math.floor(Math.random()*myads.length)],'" style="border:0 solid black;"></iframe>');
[/CODE]


But..... It's still not loading.

Do I have to put either the full URL, or, the full path of the server infront of the x.html files?

Cheers

Richelo
Copy linkTweet thisAlerts:
@RicheloauthorNov 24.2008 — OOPS!

Left out some code!

THIS is what test.js looks like:

[CODE]
var myads =
[
'/adds/1.html',
'/adds/2.html',
'/adds/3.html',
'/adds/4.html',
'/adds/5.html'
];
document.writeln('<iframe src="', myads[Math.floor(Math.random()*myads.length)],'" style="border:0 solid black;"></iframe>');
[/CODE]


In that format, I see the frame, but, it says it can't find the html file. If I use the full URL, then it FF pops up a save box for the HTML file being loaded.

Cheers

Richelo
Copy linkTweet thisAlerts:
@MrNobodyNov 24.2008 — The middle argument of the [B]writeln()[/B] method got clobbered.
Copy linkTweet thisAlerts:
@MrNobodyNov 24.2008 — Since you said the adds can come from different sites, you will have to use full URLs. As for the behavior you described, I'd have to see it in action. Got a live link to the page?
Copy linkTweet thisAlerts:
@RicheloauthorNov 24.2008 — MrNobody,

The js now looks like this:

[CODE]
var myads =
[
'http://www.imnica.com/adds/1.html',
'http://www.imnica.com/adds/2.html',
'http://www.imnica.com/adds/3.html',
'http://www.imnica.com/adds/4.html',
'http://www.imnica.com/adds/5.html'
];
document.writeln('<iframe src="', myads[Math.floor(Math.random()*myads.length)],'" style="border:0 solid black;"></iframe>');
[/CODE]


The test page is here: http://www.imnicamail.com/test.html

You guys are awesome!

Cheers

Richelo
Copy linkTweet thisAlerts:
@RicheloauthorNov 24.2008 — I just did it in IE7, and it works 100&#37; there!!!
Copy linkTweet thisAlerts:
@RicheloauthorNov 24.2008 — Something is messed up with FireFox.

In IE7, it now works 100&#37;!

In FF, I still just get a file not found. I cleared the cache, and I made some iframe changes in the JS file. The size changes I made to iframe, can be seen in FF, but, it still says file not found, even though it works in IE!

Gotta love the browser incompatibilities! ;-)

Cheers

Richelo
Copy linkTweet thisAlerts:
@MrNobodyNov 24.2008 — I don't know that much about how Firefox supports IFRAMEs, but this is the generated source after executing that script:

<iframe src="http://www.imnica.com/adds/3.html" width="100&#37;" frameborder="0" height="100%"></iframe>

It looks fine to me. I don't know why Firefox is treating it like a file download request. But I think it has more to do with how your ad documents are formatted. Meaning, Firefox isn't recognizing them as a document it can render -- therefore, Firefox downloads it.
Copy linkTweet thisAlerts:
@RicheloauthorNov 24.2008 — Hey MrNobody,

Right now, FF is not trying to download anymore, it just gives me a not found error: The requested URL /1.html was not found on this server.

Which is just REALLY weird, as in IE it works.

I will do some editing of the test HTML files, and remove everything other than the actual HTML. No body tags or anything, and see of that makes any difference.

I will also do some research on FF and iframe, and see if I can find anything.

Thanks so much for your help thus far! MUCH appreciated!

Cheers

Richelo
Copy linkTweet thisAlerts:
@MrNobodyNov 24.2008 — It's not giving me any errors -- just downloading each ad document. This is the generated source I got this time:

<iframe src="http://www.imnica.com/adds/1.html" width="100&#37;" frameborder="0" height="100%"></iframe>
Copy linkTweet thisAlerts:
@rnd_meNov 24.2008 — your main site page is down in FF.

adjust your content-type on the server to "text/html".
Copy linkTweet thisAlerts:
@RicheloauthorNov 24.2008 — I'm using FF 3.0.4, and I just keep getting the file not found.

With my non-working knowledge of JS, I am trying to decipher this: http://www.quirksmode.org/js/iframe.html

Does seem to be something in FF3. They are going VERY strict on DOM call compliance.
Copy linkTweet thisAlerts:
@RicheloauthorNov 24.2008 — Found something else.....

If I run the same test on FF, but, put the html file on the same domain as the JS script, THEN it works!!!

http://www.imnica.com/test.html

So, somehow the remote call for HTML from within the JS, is not working in FF!

Well, now I know which direction to investigate in.....
Copy linkTweet thisAlerts:
@RicheloauthorNov 24.2008 — One more thing in FF..... On the site where its working now, it only ever loads one of the 5 test html files. I cleared cache, etc, and still just loads the one.
Copy linkTweet thisAlerts:
@rnd_meNov 24.2008 — i don't think it has anything to do with frames; your pages won't open even in the main browser window...
Copy linkTweet thisAlerts:
@RicheloauthorNov 24.2008 — The actual HTML add files are: http://www.imnica.com/adds/1.html through to 5.

It does work 100&#37; in IE7 as well.
Copy linkTweet thisAlerts:
@rnd_meNov 24.2008 — why can i access this?

users should never but never get to see php code...

[CODE]<?php

/**

* Front to the WordPress application. This file doesn't do anything, but loads

* wp-blog-header.php which does and tells WordPress to load the theme.

*

* @package WordPress

*/

/**

* Tells WordPress to load the WordPress theme and output it.

*

* @var bool

*/

define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */

require('./wp-blog-header.php');

?>[/CODE]


just because IE behaves in the non-standard content-sniffing way, doesn't mean it's good to depend upon it...

check out http://www.imnica.com/gotrythis/[B]gotrythis.php[/B]?id=adds&#37;2Ftest1.html, and ask around on the PHP forums.
Copy linkTweet thisAlerts:
@RicheloauthorNov 24.2008 — Wow, NO IDEA why you see that! I run WP on the main domain, in the root dir, and I loaded the test file in the root dir, but, you should NOT see that!

How do you get to that?!
Copy linkTweet thisAlerts:
@RicheloauthorNov 24.2008 — Maybe I'm messing something up in my .htaccess file:
Copy linkTweet thisAlerts:
@rnd_meNov 24.2008 — Wow, NO IDEA why you see that! I run WP on the main domain, in the root dir, and I loaded the test file in the root dir, but, you should NOT see that!

How do you get to that?![/QUOTE]


i posted in firebug to test1.html...

obviously, you have server issues. gotrythis.php is suspect #1.

this goes beyond javascript and hence this thread.

it looks like something is loaded that should be getted.

why you would need to get from your own site is beyond me...


as far as firefox behavior (one more time)

change your mime from "application/x-httpd.php" to "text/html".

if it were my call, i would shut down the whole site until this is resolved.

it seems like a HUGE security issue to me, but then again i am not a big PHP guy, so perhaps it's just an annoyance.


[B]Edit[/B]: i would remove your .htaccess file for all the world to see [U]ASAP[/U].
Copy linkTweet thisAlerts:
@RicheloauthorNov 24.2008 — Thanks for all the input and suggestions.

I have edited out my .htaccess file. I changed some stuff around, and now this is working on FF: http://www.imnicamail.com/test.html

So, it's now loading properly from a different domain.

ONLY things is now that it still only displays 1 of the 5 files, even after clearing cache, and refreshing.

Any thoughts?

Thanks again everyone for your amazing help!!!

Cheers

Richelo
Copy linkTweet thisAlerts:
@MrNobodyNov 25.2008 — ONLY things is now that it still only displays 1 of the 5 files, even after clearing cache, and refreshing.[/QUOTE]
Let's be sure... Change this part:
[CODE]document.writeln('<iframe src="', myads[Math.floor(Math.random()*myads.length)],'" width="100&#37;" frameborder=0 height="100%"></iframe>');[/CODE]
to this:
[CODE]var _x = Math.floor(Math.random()*myads.length);
document.writeln('<iframe src="', myads[_x], '?', _x, '" width="100%" frameborder=0 height="100%"></iframe>');[/CODE]
Copy linkTweet thisAlerts:
@RicheloauthorNov 25.2008 — Still the same..... Only displays the one it loaded 1st.

When I look at the source through FireBug, on every refresh, it DOES have a different file in the actual URL!

Something fishy going on!
Copy linkTweet thisAlerts:
@MrNobodyNov 25.2008 — I tried it five times and got these (changing) results:
[code=html]<iframe src="http://www.imnica.com/adds/5.html?4" id="addframe" name="addframe" width="100%" frameborder="0" height="100%"></iframe>
<iframe src="http://www.imnica.com/adds/2.html?1" id="addframe" name="addframe" width="100%" frameborder="0" height="100%"></iframe>
<iframe src="http://www.imnica.com/adds/1.html?0" id="addframe" name="addframe" width="100%" frameborder="0" height="100%"></iframe>
<iframe src="http://www.imnica.com/adds/5.html?4" id="addframe" name="addframe" width="100%" frameborder="0" height="100%"></iframe>
<iframe src="http://www.imnica.com/adds/4.html?3" id="addframe" name="addframe" width="100%" frameborder="0" height="100%"></iframe>[/code]
×

Success!

Help @Richelo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...