Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

JavaScript variable scope

  • 10-04-2007 9:27pm
    #1
    Moderators, Education Moderators Posts: 2,432 Mod ✭✭✭✭


    I'm having a few troubles with a firefox extension I'm developing.

    I have one function to download an XML file, and another to parse the XML file.
    function getXML()
    {
    	
        XMLDOC = "";
        var req = new XMLHttpRequest();
        req.open( 'GET', 'http://www.xxx.xml', false );
        req.send( null );
        if( req.status == 200 )
    
          XMLDOC = req.responseText;
        
    }
    

    and
    function getOther(){
        //Begining XML Parsing
        // initiate new DOMParser
        var parser = new DOMParser();
        // Load XMLDOC into a variable so it can be parsed
        var doc = parser.parseFromString( XMLDOC, "text/xml" );
    
    etcetcetc
    

    So when we run the getXML(), the XML file gets loaded into the XMLDOC variable. However when I run the getOther() function, Javascript complains that XMLDOC hasn't been defined (Even though it has been defined an initialised with values) in the above function.

    Theres probably something really trivial that I'm missing here


Comments

  • Registered Users, Registered Users 2 Posts: 11,989 ✭✭✭✭Giblet


    Shouldn't XMLDOC have var in front of it?


  • Moderators, Education Moderators Posts: 2,432 Mod ✭✭✭✭Peteee


    Dosent matter, it complains about the same thing.


  • Registered Users, Registered Users 2 Posts: 26,584 ✭✭✭✭Creamy Goodness


    i'm not great on the old javascript but aren't variables declared inside functions only able to be used inside that function?

    to use a variable in other function you'll need to get the getXML() function to return the value you want and then pass it as a parameter into the getOther() function.


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Yeah, what Creamo said. Shouldn't XMLDOC be declared outside of getXML()?

    i.e.
    var XMLDOC = "";
    
    function getXML()
    {
       ...
    }
    function getOther()
    {
       ...
    }
    


  • Closed Accounts Posts: 884 ✭✭✭NutJob


    For it to work you need to declare XMLDOC somewhere

    Then walk through the code with firebug and if theres anything else it'll highlight it


  • Advertisement
  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    The problem isn't with the declaration of the XMLDoc variable, it's with the fact that your function doesn't return a value and that the function getXML is not called before XMLDOC is used. If you try this instead it should work:

    function getXML()
    {

    XMLDOC = "";
    var req = new XMLHttpRequest();
    req.open( 'GET', 'http://www.xxx.xml', false );
    req.send( null );
    if( req.status == 200 )

    XMLDOC = req.responseText;
    return XMLDOC;
    }

    Code:
    function getOther(){
    //Begining XML Parsing
    // initiate new DOMParser
    var parser = new DOMParser();
    // Load XMLDOC into a variable so it can be parsed
    var doc = parser.parseFromString( getXML(), "text/xml" );

    etcetcetc

    Otherwise you need to declare XMLDOC as a variable outside of the getXML and getOther functions and call getXML from inside getOther as follows:

    var XMLDOC = "";
    function getXML()
    {

    XMLDOC = "";
    var req = new XMLHttpRequest();
    req.open( 'GET', 'http://www.xxx.xml', false );
    req.send( null );
    if( req.status == 200 )

    XMLDOC = req.responseText;

    }

    Code:
    function getOther(){
    //Begining XML Parsing
    // initiate new DOMParser
    var parser = new DOMParser();
    // Load XMLDOC into a variable so it can be parsed
    getXML();
    var doc = parser.parseFromString( XMLDOC, "text/xml" );

    etcetcetc

    -RD


  • Moderators, Education Moderators Posts: 2,432 Mod ✭✭✭✭Peteee


    Cremo wrote:
    i'm not great on the old javascript but aren't variables declared inside functions only able to be used inside that function?

    to use a variable in other function you'll need to get the getXML() function to return the value you want and then pass it as a parameter into the getOther() function.

    As far as I know if there is no 'var' in front of it then you can access it from anywhere.

    The getXML() is called before getOther() so it should be initialised before getXML() is called.
    Shouldn't XMLDOC be declared outside of getXML()?

    If I declare it as blank, then it will get wiped the next time the function runs. I want the contents of XMLDOC to be stored.

    Since its a HTTP request I dont want to wait on the function everytime the script is run (i.e. every time a page is refreshed, I think JavaScript throws up an error about this too)

    None of these solutions seems to have worked unfortunatly


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Peteee wrote:
    If I declare it as blank, then it will get wiped the next time the function runs. I want the contents of XMLDOC to be stored.

    Hmmm, what's the flow of the code here, are you trying to persist XMLDOC across a GET or a POST?


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    Javascript isn't designed to persist variables across multiple pages or a get / post. Javascript is client browser based only and designed specifically to prevent the storing or writing of data to a client machine (though you can write data to cookies which is a possible work around)


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Javascript isn't designed to persist variables across multiple pages or a get / post. Javascript is client browser based only and designed specifically to prevent the storing or writing of data to a client machine (though you can write data to cookies which is a possible work around)

    Yeah, we know.


  • Advertisement
  • Moderators, Education Moderators Posts: 2,432 Mod ✭✭✭✭Peteee


    Javascript isn't designed to persist variables across multiple pages or a get / post. Javascript is client browser based only and designed specifically to prevent the storing or writing of data to a client machine (though you can write data to cookies which is a possible work around)

    Yeah I figured that out :) I'm now writing the values to a temporary file in the users temp directory (Firefox allows this)

    So I have the getXML() saving a file, and getCurrency() opening that file and reading them off into variables etc using(Huzzah!).

    Another problem, since JavaScript dosent persist variables I need to load the values each time the script is run (i.e. on a new page load).

    So I try to call the getOther() function (each time a page loads), but it throws a 'uncaught exception: Permission denied to get property UnNamedClass.class'

    Googling this seems to imply some sort of cross-site-scripting vulnerability?


  • Moderators, Education Moderators Posts: 2,432 Mod ✭✭✭✭Peteee


    Right, you can probably ignore all this. I just found out firefox extensions have a preferences 'thingy' for just this type of thing.

    Should have researched better rather then trying to reinvent the wheel!

    Thanks for all your help regardless!


Advertisement