1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

AJAX, eval(), and loading javascript via javascript

Discussion in 'JavaScript' started by Kendothpro, Apr 28, 2006.

  1. #1
    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...
     
    Kendothpro, Apr 28, 2006 IP
  2. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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.
     
    mad4, Apr 28, 2006 IP
  3. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    exam, Apr 28, 2006 IP
  4. Kendothpro

    Kendothpro Well-Known Member

    Messages:
    574
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    120
    #4
    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 28, 2006 IP
  5. Kendothpro

    Kendothpro Well-Known Member

    Messages:
    574
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    120
    #5
    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 :)
     
    Kendothpro, Apr 29, 2006 IP
  6. Shoemoney

    Shoemoney $

    Messages:
    4,474
    Likes Received:
    588
    Best Answers:
    0
    Trophy Points:
    295
    #6
    IMO ajax is overkill in this instance

    have you tried iframes ?
     
    Shoemoney, Apr 29, 2006 IP
  7. Kendothpro

    Kendothpro Well-Known Member

    Messages:
    574
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    120
    #7
    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
     
    Kendothpro, Apr 29, 2006 IP
  8. Shoemoney

    Shoemoney $

    Messages:
    4,474
    Likes Received:
    588
    Best Answers:
    0
    Trophy Points:
    295
    #8
    Ok I understand.. just seemed like maybe iframe might be a easy fix
     
    Shoemoney, Apr 29, 2006 IP
  9. rpacker

    rpacker Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    What was the code for the solution you finally came up with? Can you please share that? Thanks a bunch.

    -rob
     
    rpacker, May 8, 2006 IP
  10. redhits

    redhits Notable Member

    Messages:
    3,023
    Likes Received:
    277
    Best Answers:
    0
    Trophy Points:
    255
    #10
    Hey dude... i don't know if you are still alive but i also want the code...
     
    redhits, Oct 31, 2008 IP
  11. oxidati0n

    oxidati0n Peon

    Messages:
    744
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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
    
    Code (markup):
    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.
     
    oxidati0n, Nov 1, 2008 IP