Looking for Javascript XmlHTTP Request Help

Discussion in 'Code Snippets and Tutorials' started by ricky92, Nov 30, 2007.

  1. ricky92

    ricky92 Administrator
    Staff Member

    Joined:
    Nov 10, 2006
    Messages:
    1,866
    Likes Received:
    67
    If someone know how to use this Javascript Component properly with both Firefox and IE (version 6.0 and 7.0) and some other browsers can he/she please post here or PM me? I need help to make a MOD for this forum...

    I found this code on a site, but it doesn't work... (maybe it's just me...)
    Code (Javascript):
    1. <div class="javascript" id="{CB}" style="font-family: monospace;"><ol><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"> &nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> xmlhttp=<span style="color: #003366; font-weight: bold;">false</span>;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">/*@cc_on @*/</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">/*@if (@_jscript_version >= 5)</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #009900; font-style: italic;">&nbsp; &nbsp; // JScript gives us Conditional compilation, we can cope with old IE versions.</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #009900; font-style: italic;">&nbsp; &nbsp; // and security blocked creation of the objects.</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #009900; font-style: italic;">&nbsp; &nbsp; &nbsp;try {</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #009900; font-style: italic;">&nbsp; &nbsp; &nbsp; xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #009900; font-style: italic;">&nbsp; &nbsp; &nbsp;} catch (e) {</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #009900; font-style: italic;">&nbsp; &nbsp; &nbsp; try {</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #009900; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp;xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #009900; font-style: italic;">&nbsp; &nbsp; &nbsp; } catch (E) {</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #009900; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp;xmlhttp = false;</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #009900; font-style: italic;">&nbsp; &nbsp; &nbsp; }</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #009900; font-style: italic;">&nbsp; &nbsp; &nbsp;}</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #009900; font-style: italic;">&nbsp; &nbsp; @end @*/</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">(</span>!xmlhttp && <span style="color: #000066; font-weight: bold;">typeof</span> XMLHttpRequest!=<span style="color: #3366CC;">'undefined'</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #66cc66;">{</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xmlhttp = <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">}</span> <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #66cc66;">(</span>e<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xmlhttp=<span style="color: #003366; font-weight: bold;">false</span>;</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">}</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; <span style="color: #66cc66;">}</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">(</span>!xmlhttp && window.<span style="color: #006600;">createRequest</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #66cc66;">{</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xmlhttp = window.<span style="color: #006600;">createRequest</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">}</span> <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #66cc66;">(</span>e<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xmlhttp=<span style="color: #003366; font-weight: bold;">false</span>;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">}</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; <span style="color: #66cc66;">}</span></li></ol></div>
    When I try to create a request and use an alert() to display the source, I get nothing. Absolutely nothing. Is this code wrong? Please help! o_O