eval workaround in Safari

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • princeHector
    New Member
    • Nov 2007
    • 5

    eval workaround in Safari

    Hello ppl,

    I am new to working with the Safari browser supported in Mac OS, I am trying to perform eval of a Javascript(stri ng) which is working in IE / Firefox but not working in Safari. I am basically trying to create an instance of an object with eval.

    For ex:
    eval( "var x = new ImageLoader( );" ) ;

    any leads or suggestions on this issue would be great.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    hi ...

    could you tell why you use eval for that? may be we don't have to workaround a browser-specific thing ... but the use of eval at all :)

    kind regards

    Comment

    • princeHector
      New Member
      • Nov 2007
      • 5

      #3
      hey,

      i dont know wat object i am going to make of before hand. I mean i get the Object name from a variable, then concatenate the new operator and braces with it.

      something like this

      "var x = new " + className + "();"

      this className will vary...that is why i want to use eval here. But safari doesnt seem to support eval. Any ideas ???

      Thanks

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        i see ... hmmm ... i didn't know that eval-issue with safari so i'll try it this evening ... but you could use the following code at first and give it a try:

        [CODE=javascript]function create_class(cl ass) {
        return new Function('retur n new ' + class)();
        };

        var x = create_class(cl ass_name);[/CODE]
        kind regards

        Comment

        • princeHector
          New Member
          • Nov 2007
          • 5

          #5
          ok will try this...you just try that out in safari and let me know...

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5388

            #6
            hi ...

            i tested the following in my safari 3 - addressbar:

            Code:
            javascript:eval( "var x = new Array;" ) ; alert(x instanceof Array);
            the alert said 'true' :) so it works ... as i expected ... so could you tell what particular error you get when using your eval with safari? (i simply used Array instead of an ImageLoader-class ... for testing purposes)

            kind regards

            Comment

            • princeHector
              New Member
              • Nov 2007
              • 5

              #7
              hey

              eval seems to be working...my problem is that i have 5 classes that could be instantiated and i dunno which one i will have to instantiate.... that is why i am using eval to instiate the classes...is there any difference between IE / Firefox and Safari in how they evaluate the script...scope or something could be an issue ???

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5388

                #8
                Originally posted by princeHector
                hey

                eval seems to be working...my problem is that i have 5 classes that could be instantiated and i dunno which one i will have to instantiate.... that is why i am using eval to instiate the classes...is there any difference between IE / Firefox and Safari in how they evaluate the script...scope or something could be an issue ???
                there shouldn't be differences between safari and FF as far as i know ... all ajax-json-evals i know work the same ... so in case the className you pass to it and the class itself is ok and available to javascript at runtime it should work ... again: what error do you get in safari?

                kind regards

                Comment

                • princeHector
                  New Member
                  • Nov 2007
                  • 5

                  #9
                  Hey,

                  I am sorry eval s working in Safari...I tried out a few examples....the issue here has turned out in a different manner...I load javascripts dynamically which contains the classes...now the isue is that there is no onload / on readystatechang e that is fired when a tag is loaded and ready...that i why my classes were undefined because i initialize before these scripts in these tags get loaded...so am trying to find some way of doing that now...any clues on that?

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5388

                    #10
                    hmmm ... please give an example code for that problem ... you have to ensure that the classes are ready to use before you want to create an instance of one of them ... but i don't have a clue on how you actually have it done ... so it would be better to see it or have a detailed description step by step regarding your current code ...

                    kind regards

                    Comment

                    Working...