Debt Consolidation - Turquoise Rings - Discount Perfume - Free Credit Report - Kamala Harris

PDA

View Full Version : AJAX, eval(), and loading javascript via javascript


Kendothpro
Apr 28th 2006, 6:59 am
Ok so, this is my purpose:
- to be able to load asynchronously (via AJAX) some javascript ads (like google's or adbrite) so as to make them be loaded in the background, then update the page after the ads have loaded via innerHTML

Why?
-Because 90% of the time in my newer sites, javascript ads are the major offender in terms of speed of page rendering

My problem:
Via ajax, I can call a php file that retrieves some javascript and outputs it, XMLhttprequest returns those javascript lines, but they don't render in the page, since they miss the whole page loading, and are apparently not parsed
For example, let's say I call a php file via ajax, and it returns the output into a variable named "text" containing "document.write('hello')"
if I use xxx.innerHTML=text, nothing happens

My 1st solution:
Passing those javascript lines to eval() [like eval(text) ], but this produces a second problem, that I couldn't solve (probably because of my lack of knowledge in javascipt):
if I eval the code, it deletes my current page and renders a new one
for example, if I parse a document.write, my page disappears, and a new one is rendered with the document.write text

What I want is basically to make that "document.write" appear inside a div in my page, adding to the content (and not overwriting the whole page), much like what happens when using innerHTML

Is this even possible? How would you go about it?
I tried xxx.innerHTML=eval(outputfromphpfile) but it overwrites my whole page...

mad4
Apr 28th 2006, 7:04 am
I think this may be against googles TOS for Adsense, are you sure its Adsense that is causing the pages to load slowly? Millions of sites use Adsense and its normally OK.

exam
Apr 28th 2006, 7:13 am
Setting the content type of your back end php script to text/html should do the trick :) Or if you want to send HTML output but have the content type be text/xml, you have to use htmlentities on the HTML output first. Been there done that. But read on.

Your whole premise is illogical- you will only increase page load time if anything. Consider this:

::With normal Adsense::
1. Your page loads from your server with embedded JS
2. After page loads, the JS fetches the ads from googlesyndication.

::With what you are proposing::
1. Your page loads from your server with embedded AJAX code or with an independant JS file. This is larger than the formerly embedded Adsense code, so it takes longer than before to load.
2. After page loads, your AJAX has to hit your server again to retrieve the Adsense code, which then pulls the ads off googlesyndication. 2 HTTP requests- more traffic, you use more bandwidth and it takes more time- instead of one single HTTP request directly to googlesyndication.com

Conclusion: Scrap the idea.

Kendothpro
Apr 28th 2006, 3:43 pm
The total download time is larger, but don't forget that the user can start browsing the page without ads, and they will pop in later..this effectively makes the page be available sooner to the user, that's my definition of "fast"
Anyway could you elaborate on the html headers?? What I want is really to execute that js code, how would changing the header to text/html do the trick?

Kendothpro
Apr 29th 2006, 10:00 am
Hmmm seems like eval'ing an innerHTML command does the job! I also managed to find a way to use concurrent xmlhttprequests by creating a new object every time the function is called, seems to work fine both in IE and firefox :)

Shoemoney
Apr 29th 2006, 10:03 am
IMO ajax is overkill in this instance

have you tried iframes ?

Kendothpro
Apr 29th 2006, 3:08 pm
yep but I would need to change the iframed files to also contain the styling etc, and they don't resize to fit the content they contain (unless I use javascript, which would further increase the page load)
not to mention it's not as slick and cool as ajax :P

Shoemoney
Apr 29th 2006, 4:34 pm
yep but I would need to change the iframed files to also contain the styling etc, and they don't resize to fit the content they contain (unless I use javascript, which would further increase the page load)
not to mention it's not as slick and cool as ajax :P

Ok I understand.. just seemed like maybe iframe might be a easy fix

rpacker
May 8th 2006, 7:21 pm
What was the code for the solution you finally came up with? Can you please share that? Thanks a bunch.

-rob

softgroups
Oct 31st 2008, 12:28 am
Hey dude... i don't know if you are still alive but i also want the code...

bilawal360
Nov 1st 2008, 12:32 pm
I think you're commanding javascript to execute the javascript contents through eval() in the div tag but it probably didn't recognise it so it just overwrote the whole page.

You should try this, it may let the JS engine understand it a bit more.

var eval_it = eval(...);
document.getElementById(...).innerHTML = eval_it; //Updated


Try that, if it doesn't work then I have no clue on how to do it since I just use PHP and file include joints to develop mine.