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

javascript problem

Options
  • 14-05-2007 10:51am
    #1
    Registered Users Posts: 872 ✭✭✭


    Hi I have a function like so:
    function showEstablishmentInfo((establishmentID){
       var mark = "marker"+establishmentID;
       mark.openInfoWindowTabsHtml(infoTabs);
    }
    

    For some reason the mark value isnt getting set properly. I did it as a switch statement below and it'w working fine but this isnt practical as i could have hundreds of case statements.
    function showEstablishmentInfo(establishmentID){
    	switch(establishmentID)
    	{
    		case 1:marker1.openInfoWindowTabsHtml(infoTabs);
    		break;
    		case 2:marker2.openInfoWindowTabsHtml(infoTabs);
    		break;
    	}
    }
    

    Any ideas ?


Comments

  • Registered Users Posts: 273 ✭✭stipey


    I would imagine that in the first snippet of code you are setting the mark variable to the string "marker1" or "marker2".

    While this may be the control you wish to work with, it doesn't give you a handle on the actual object - you just have a string containing its name.

    There is a JS function that allows you to build a javascript statment dynamically in a string and then execute it - but the name of it escapes me at present. You could use this to build the string that assigns the handle to the mark variable.

    (Sorry I can't take the time to find out the name of this function... up to my eyes with... um... ambitous [read: ridiculous] deadlines)


  • Registered Users Posts: 3,594 ✭✭✭forbairt


    <script type="text/javascript">

    var marker1 = "bob";

    function test(testID)
    {
    var mark = eval("marker"+testID);

    alert(mark);
    }

    test(1);

    </script>

    eval() I think is what you're after .. no time to play with it at moment though ...


  • Registered Users Posts: 21,257 ✭✭✭✭Eoin


    Try this to reference the object:

    document.getElementById(marker + establishmentID)


  • Registered Users Posts: 872 ✭✭✭grahamor


    The eval() thing worked perfectly. Thanks

    Now, if marker0 is an object inside a div inside an iframe how would i reference it from the parent page ?

    document.all.iFrameName.divName.marker0 ??? (this doesnt work by the way)


  • Registered Users Posts: 273 ✭✭stipey


    Again, I'm a little rusty on the specifics here - I've used almost no JavaScript over the past 2 years.

    You should be able to get a handle on the IFRAME (if you give it an ID value) using document.getElementById("iframename")

    Once you have this, I think you can get a handle on the IFRAME will have a document object. You can use this (in combination with the eval() function to get a handle on your marker object.

    So... and I'm paraphrasing here.... all spelling is only an approximation and this is untested... but it should give the gist.
    <html>
      :
      :
      <body>
        <iframe id="iFrame1" src="sourcepage.html"></iframe>
      </body>
    </html>
    
       var innerFrame = document.getElementById("iFrame1");
    
       var innerMarker = eval(innerFrame + ".document.marker" + establishmentID);
    
       // Then use this handle to call the function
    


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


    grahamor wrote:
    The eval() thing worked perfectly. Thanks

    Now, if marker0 is an object inside a div inside an iframe how would i reference it from the parent page ?

    document.all.iFrameName.divName.marker0 ??? (this doesnt work by the way)

    Don't ever use document.all unless you know for sure that the only browser your clients will use is internet explorer. The DOM approach is to use document.getElementById() and allows you to directly reference any tag on the page (provided it has an id attibute set) without needing to know all the parent tags of it. In the above exapmple you used eval however as mentionned in a previous post document.getElementById would be an easier approach. By using it you can set it's behavior <u>and</u> reference it.

    Above therefore you would be referencing :

    the_div = "marker" + id;
    document.getElementById(the_div).innerHTML = "This is a test to show how to reference a tag using the DOM!!";

    Hope that helps
    -RD


  • Closed Accounts Posts: 50 ✭✭Farouk.Bulsara


    grahamor wrote:
    The eval() thing worked perfectly. Thanks

    Now, if marker0 is an object inside a div inside an iframe how would i reference it from the parent page ?

    document.all.iFrameName.divName.marker0 ??? (this doesnt work by the way)

    Dude, you need to know the difference between the "." operator and the use of "[ ]" to lookup the properties of an object.

    The eval function is ugly and clumsy (and not necessary)!

    Suppose you have a string variable, marker, that evaluates to some value, like "marker0" or "marker1" etc. You can access the object using "[ ]" thus:

    document.all.iFrameName.divName[marker]

    Or if you have a numeric variable, m, that evaluates to 0 or 1, etc, then you can access the object thus:

    document.all.iFrameName.divName["marker" + m]

    The reason why "." fails is that the bit after the "." MUST correspond to a named value of the object.

    Example:

    var person = new Object();
    person.name = "John";
    person.address_1 = " .... ";
    person.address_2 = " .... ";

    correct:

    var a = "address_"
    var i = 1;
    alert(person.address_1); // correct because "address_1" is a property
    alert(person["address_1"]); // correct
    alert(person[a+i]) // correct, because a+i evaluates to a property
    alert(person["address_" + i ]); // assuming i evaluates to 1 or 2

    incorrect:

    var j = 1;
    var k = "address_1";

    alert(person.k) // "k" is not a property of "person"
    alert(person[address_1 + j]) // incorrect - address_1 is undefined

    Fred


  • Registered Users Posts: 872 ✭✭✭grahamor


    Thanks for the tips, i guess i really dont know some javascript basics !!

    This is all for google maps but i decided to blank the iframe completly and load all the stuff into a div.

    Thanks again for the replies


Advertisement