Skip to posts
  • About

Win Web Hosting

Web Hosting Information
  • Home
  • ASP.NET
  • Cloud Hosting
  • Dedicated Servers
  • Linux Web Hosting
  • Microsoft AJAX
  • Microsoft Siverlight
  • Windows Web Hosting

JavaScript: Using a variable that is outside a function, inside a function.?

April 4, 2010 James 3 comments
Microsoft AJAX function, Inside, JavaScript, outside, Using, variable

Comments

Leave a comment Trackback
  • Written by Germann A about 2 years ago.

    To be able to access external vars in JavaScript function they will have to be global vars.
    When you put “var” before variable definition you specify that it is going to be a (scope) local variable – remove the “var” from
    var chat_status = document.getElementById(‘chat_status’);
    line, making it
    chat_status = document.getElementById(‘chat_status’);
    and it will work…

  • Written by Doug G about 2 years ago.

    This has to be outside all other functions:

    var chat_status = document.getElementById(‘chat_status’);

    I assume that’s the case. If so, this is global but…

    The problem is that you are making this assignment before the page loads. The node with the ‘chat_status’ id doesn’t actually exist yet. That’s why if you check the javascript console you will see ‘object expected’ or something like that.

    There are a couple of ways to fix this.

    One way is to do this:

    var chat_status; //declare only

    function init(){
    chat_status = document.getElementById(‘chat_status’);
    }

    And call init() before doing whatever else you need to do.

    Or, inside your function you could both declare and initialize your variable without the var keyword, like so:

    function getXmlHttpRequestObject() {
    chat_status = document.getElementById(‘chat_status’); //notice, no ‘var’

    This will make it global also.

    Or, you could do this every time:

    document.getElementById (‘chat_status’). innerHTML =

    Good luck.

  • Written by Jon about 2 years ago.

    I would suggest putting that inside of the function. So something like:

    function getXmlHttpRequestObject() {
    var ajax_request;
    var chat_status = document.getElementById(‘chat_status’);

    try{
    // Opera 8.0+, Firefox, Safari
    if(window.XMLHttpRequest && !window.ActiveXObject) {
    ajax_request = new XMLHttpRequest();
    chat_status.innerHTML = ‘XMLHttpRequest Object succesful!‘;
    }
    }catch (e){
    alert(“Unable to make a request.”);
    }
    // Internet Explorer Browsers
    try{
    ajax_request = new ActiveXObject(“Msxml2.XMLHTTP” || “Microsoft.XMLHTTP”);
    chat_status.innerHTML = ‘Msxml2 ActiveX Object succesful!‘;
    }catch (e){
    if(document.all) {
    chat_status.innerHTML = ‘Cound not create XmlHttpRequest Object. I highly recommend updating your browser!‘;
    }
    }

    return ajax_request;
    }

    window.onload = function() {
    alert(getXmlHttpRequestObject());
    }

    I also notice that you wasn’t doing any checking to see if it was only a Safari or Firefox web browser. Since in internet explorer 8 IE will actually use the XMLHttpRequest but from what I heard it isn’t very good. The reason why it wouldn’t load was probably because of the fact that that you had your script inside of the head tag. Since it’s inside of the head tag the elements inside of the body isn’t fully loaded thus giving you a error because the object can’t be found. Give that a try and see if that works any better.

    Hope this helps,
    Jon W

Comment Pages:

Leave a Comment

Cancel Reply

You must be logged in to post a comment.

RSS

Win Web Hosting Home Page

Cheap Windows Hosting

 

May 2012
M T W T F S S
« Apr    
 123456
78910111213
14151617181920
21222324252627
28293031  

Archives

  • May 2012 (61)
  • April 2012 (81)
  • March 2012 (82)
  • February 2012 (79)
  • January 2012 (92)
  • December 2011 (163)
  • November 2011 (205)
  • October 2011 (339)
  • September 2011 (367)
  • August 2011 (367)
  • July 2011 (322)
  • June 2011 (338)
  • May 2011 (358)
  • April 2011 (94)
  • March 2011 (40)
  • February 2011 (36)
  • January 2011 (41)
  • December 2010 (30)
  • November 2010 (40)
  • October 2010 (41)
  • September 2010 (52)
  • August 2010 (150)
  • July 2010 (165)
  • June 2010 (159)
  • May 2010 (238)
  • April 2010 (248)
  • March 2010 (202)
  • February 2010 (576)

Recent Posts

  • MVC TUTORIAL PART 7 : Adding Supplier Details Link
  • dot net course in pune. C# training in pune. asp.net training in pune
  • How To Get Black Silverlight
  • Hosting Classic ASP Applications on IIS 7.5
  • Shoothill Deep Zoom for Secret Cinema and Windows 7 Phone

Tags

.NET 2010 about AJAX Application Applications ASP.NET best Business cheap cloud Company Computing Control Create data Dedicated Development Free from help Host hosting India Introduction Linux Microsoft more need Page Part private Server servers service Services Silverlight Site Software Source Tutorial Using Video website Windows

Categories

  • ASP.NET
  • Cloud Hosting
  • Dedicated Servers
  • Linux Web Hosting
  • Microsoft AJAX
  • Microsoft Siverlight
  • Windows Web Hosting

Meta

  • Register
  • Log in
  • Valid XHTML
Powered by WordPress Web Design by SRS Solutions © 2012 Win Web Hosting