SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    NJ
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need to check if a function exists

    Hi,
    I am trying to figure out how I can check to see if a function, the name of which is stored inside the variable funct, can be tested to see whether or not it exists using the variable. I know that its easy if I actually know the function name, all you have to do is a simple if statement: if (window.somefunction), but rather than put the name of a function, i'm trying to see if it's at all possible to put a variable there in place of "somefunction" that has stored the dynamically created name of the function. Essentially, I am using a function to append dynamically created functions at the end of the javascript file. Here's the code:
    Code:
    function write(functionname,xval,yval,value){
    var name=functionname;
    var x=xval;
    var y=yval;
    var val=value;
    var funct=name+x+y;
    try{
       var fso, s;
       fso = new ActiveXObject("Scripting.FileSystemObject");
       s = fso.OpenTextFile("C:\\nblmnd.js" , 8, 1, -2);
    // ________________________________Start
       if (!window.funct){
       s.writeline("function " + funct + "(xval,yval){var x=xval; var y=yval; if (x==" + x + " && y==" + y + "){return " + val + "}}");
       }
    // __________________________________End
       s.Close();
    }
    catch(err){
      var strErr = 'Error:';
      strErr += '\nNumber:' + err.number;
      strErr += '\nDescription:' + err.description;
      document.write(strErr);
    }
    }
    So as you can see, in this case I am essentially saying in the if statement that "if the function whose name is stored inside the variable funct does not exist than do the following:", and of course it does not work. Well, it does kinda work, here's some of the output i get at the end of the original .js file:
    Code:
    function XY28(xval,yval){var x=xval; var y=yval; if (x==2 && y==8){return 1}}
    function XY18(xval,yval){var x=xval; var y=yval; if (x==1 && y==8){return 1}}
    function XY28(xval,yval){var x=xval; var y=yval; if (x==2 && y==8){return 1}}
    function XY18(xval,yval){var x=xval; var y=yval; if (x==1 && y==8){return 1}}
    function XY18(xval,yval){var x=xval; var y=yval; if (x==1 && y==8){return 1}}
    function XY19(xval,yval){var x=xval; var y=yval; if (x==1 && y==9){return 1}}
    function XY18(xval,yval){var x=xval; var y=yval; if (x==1 && y==8){return 1}}
    function XY18(xval,yval){var x=xval; var y=yval; if (x==1 && y==8){return 1}}
    function XY18(xval,yval){var x=xval; var y=yval; if (x==1 && y==8){return 1}}
    function XY19(xval,yval){var x=xval; var y=yval; if (x==1 && y==9){return 1}}
    function XY19(xval,yval){var x=xval; var y=yval; if (x==1 && y==9){return 1}}
    function XY19(xval,yval){var x=xval; var y=yval; if (x==1 && y==9){return 1}}
    But the whole point of the if statement is to filter out any function that already exists, as to not write duplicate functions. So essentially I only want the write() function to append functions that are absolutely unique. I know this is asking a lot, but if anyone can help me out, I would be truely greatful.

    Thanks in advance.

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    India
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Just a guess... Cant guarantee.

    Code:
    if (!eval('window.'+funct)){
    //Create new function...
    }
    I hope it works...

    Have a nice day

    Afru.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    NJ
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I have to admit, that was a really good try, but regretfully, it didn't work. I would guess the solution would be something along those lines. The problem is that the eval() function evaluates string to a numberical value. So what I need is a function that evaluates a string to a java method or object property. I tried googling out the answer, but to no avail. Who knows, maybe there's some old cryptic forgotten command that might help out, lol. Thanks for trying though.

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    India
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Check out this code... It really works....

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <META NAME="Generator" CONTENT="EditPlus">
    <META NAME="Author" CONTENT="">
    <META NAME="Keywords" CONTENT="">
    <META NAME="Description" CONTENT="">
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function hello123(){
    alert('hello');
    }
    
    var funct1="hello123";
    if(!eval("window."+funct1))
    alert("Function "+funct1+"() not found. You can create new");
    else
    alert("Function "+funct1+"() already exists.");
    
    var funct2="hahaha";
    if(!eval("window."+funct2))
    alert("Function "+funct2+"() not found. You can create it");
    else
    alert("Function "+funct2+"() already exists.");
    
    
    //-->
    </SCRIPT>
    </HEAD>
    
    <BODY BGCOLOR="#FFFFFF">
    
    </BODY>
    </HTML>
    Also check out the following code... to see what eval returns exactly.

    Code:
    alert(eval("window.hello123"));
    Please do post your results... I will be waiting...

    Afru.

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should always question any solution that uses eval(). It usually demonstrates a fundamental gap in a person's understanding of javascript. See if this will work for you:
    Code:
    function display_hello()
    {
    	alert("hello");
    }
    
    var name = "display_hello";
    if (window[name] && typeof window[name] == "function")
    {
    	alert("The function 'display_hello()' exists.");
    	display_hello();
    }
    All global variables and global functions(i.e. variables and functions defined outside any other funtions) are properties of the window object, and you can use array notation with the 'window' reference to access the properties using a string as the index value, e.g. window["display_hello"].

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    NJ
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking

    EUREKA!
    Thank you all for you help. Although the eval would probably have worked I decided to use the window[funct] instead. Either way, that wasn't even the problem, I tried basically every possibility that I could think of using both of your suggestions, and I finally dicided to force myself to acknowledge what I had preceived to be the problem from the begenning. I realized a while back that the problem is PROBABLY that the script simply needs to be reloaded in order for the function to be recognized as a part of the working script. The reason I didn't want to do it was because I thought that reloading the entire webpage essentially constantly would put a huge strain on the actual script. So I did it for the hell of it and it works like a dream with absolutely no slow down, which I fould somewhat strange but accepted without question. Here's the code:
    Code:
    function write(functionname,xval,yval,value){
    var name=functionname;
    var x=xval;
    var y=yval;
    var val=value;
    var funct=name+x+y;
    try{
       var fso, s;
       fso = new ActiveXObject("Scripting.FileSystemObject");
       s = fso.OpenTextFile("C:\\nblmnd.js" , 8, 1, -2);
    // ________________________________Start
    if (!window[funct]){
       s.writeline("function " + funct + "(xval,yval){var x=xval; var y=yval; if (x==" + x + " && y==" + y + "){return " + val + "}}");
       history.go(0)
       }
    // __________________________________End
       s.Close();
    }
    catch(err){
      var strErr = 'Error:';
      strErr += '\nNumber:' + err.number;
      strErr += '\nDescription:' + err.description;
      document.write(strErr);
    }
    }
    and here's the output:
    Code:
    function XY28(xval,yval){var x=xval; var y=yval; if (x==2 && y==8){return 1}}
    function XY18(xval,yval){var x=xval; var y=yval; if (x==1 && y==8){return 1}}
    function XY19(xval,yval){var x=xval; var y=yval; if (x==1 && y==9){return 1}}
    function XY110(xval,yval){var x=xval; var y=yval; if (x==1 && y==10){return 1}}
    function XY27(xval,yval){var x=xval; var y=yval; if (x==2 && y==7){return 1}}
    function XY26(xval,yval){var x=xval; var y=yval; if (x==2 && y==6){return 1}}
    function XY210(xval,yval){var x=xval; var y=yval; if (x==2 && y==10){return 1}}
    function XY310(xval,yval){var x=xval; var y=yval; if (x==3 && y==10){return 1}}
    function XY410(xval,yval){var x=xval; var y=yval; if (x==4 && y==10){return 1}}
    function XY49(xval,yval){var x=xval; var y=yval; if (x==4 && y==9){return 1}}
    Absolutedly unique wonderful output.

    Thanks again, really appreciate all your help

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As far as I can tell, you are writing functions to a text file if they don't exist in your script:

    1) Why would that require reloading the page?

    2) How would that make any functions in the text file part of your script?

  8. #8
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What's the point?

    That ActiveXObject is a security threat. It will only work in IE/Win if activeX is not disabled in the preferences.

    Code:
    var x=xval; var y=yval;
    Why do you want two pairs of identical variables?
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are trying to dynamically add an external .js file, then the problem is getting the page to parse the added <script> tags. The parsing problem arises because the extra set of <script> behaves as if it is added immediately after the current set of <script> tags, and you can't call a function that is defined in one set of script tags from another set of script tags that is higher on the page. You can call the function from a set of <script> tags that is lower on the page:

    Code:
    <html>
    <head><title></title>
    <script type="text/javascript" language="javascript">
    <!-- Hide from browsers without javascript
    
    function write(functionname,xval,yval,value){
    var name=functionname;
    var x=xval;
    var y=yval;
    var val=value;
    var funct=name+x+y;
    try{
    	var fso, s;
       fso = new ActiveXObject("Scripting.FileSystemObject");
       s = fso.OpenTextFile("C:\\My Documents\\html\\a_test.js" , 8, 1, -2);
    // ________________________________Start
       if (!window.funct){
       s.writeline("function " + funct + "(xval,yval){var x=xval; var y=yval; if (x==" + x + " && y==" + y + "){return " + val + "}}");
       }
    // __________________________________End
       s.Close();
    }
    catch(err){
      var strErr = 'Error:';
      strErr += '\nNumber:' + err.number;
      strErr += '\nDescription:' + err.description;
      document.write(strErr);
    }
    }
    
    write("g", 40, 50, 100);
    document.write("<script src='C:\\My Documents\\html\\a_test.js' type='text/javascript'></" + "script>");
    
    /* This works too:
    var script_tag = document.createElement("script");
    script_tag.setAttribute("type", "text/javascript");
    script_tag.setAttribute("src", "C:\\My Documents\\html\\a_test.js");
    var head = document.getElementsByTagName("head").item(0);
    head.appendChild(script_tag);
    */
    
    // End hiding -->
    </script>
    <script>alert(g4050(40,50))</script>
    </head>
    <body>
    
    <div>main page</div>
    
    </body>
    </html>

  10. #10
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    NJ
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You missed the point, I'm not adding functions to a text file or an external .js file, I'm using a function inside script[1] to append dymanically created functions to script[1], with emphasis on the fact that the script that contains the writing function is the script being written to. This is why, aparently, it needs to be reloaded as part of the webpage inorder for the newly written functions to be recognized.
    About the activex security thread issue, this script is not intended for online use, its for personal use for a program I'm making, I don't really mind having to choose allow this script to run once everytime norton reminds me.
    Finally, although you misunderstood my intentions with this script 7stud, your idea about dynamically adding a script in the html file using document.write is a good idea and i'm sure i'll find that to be useful in the future.

    Thanks all, again

  11. #11
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You missed the point, I'm not adding functions to a text file or an external .js file, I'm using a function inside script[1] to append dymanically created functions to script[1].
    I don't see how that is possible. The extension of the file you are writing to is .js. As far as I know, a .js file only executes when it is included in a .htm file, so a .js file is external by definition.

    If all you are trying to do is add dyanmically created functions to the currently executing script, what is all that ActiveXObject stuff for? Why not do something like this:

    Code:
    function create(functionname, x, y, value)
    {
    	window[functionname] = function(){if(x && y) return value;};
    }
    
    create("someFunction", 1, 2, 100);
    create("anotherFunction", 40, 50, 500);
    create("Bessy", "a", "b", "hello");
    
    alert(someFunction());
    alert(anotherFunction());
    alert(Bessy());
    Note: as Kravvitz pointed out, the anonymous function above is the equivalent of your function.

  12. #12
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    NJ
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    LOL, well...no offense, but if I knew that, I obviously would have done that to begin with rather than waste time, hehe . Wow, that just simplifies everything, also, I'm not sure why I didn't mention this in the begenning, but the .js file is linked to the webpage, its not a part of the webpage.
    Anyway, my current setup does work, it literally appends working functions, in the form of actual text, at the end of the .js file which is linked to the webpage that calls the script. But your suggestion is faster and more efficient by far.
    Thanks again

  13. #13
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure why I didn't mention this in the begenning, but the .js file is linked to the webpage, its not a part of the webpage.
    Usually, if you do something like this:

    <head>
    <script src="somefile.js" type="text/javascript"><script>
    </head>

    it's referred to as embedding an external .js file--external just meaning the script is in a different file than the .htm file that is loading in the browser. The result is that the exernal .js file is inserted between the script tags.

    Anyway, it does work, it literally appends working functions, in the form of actual text, at the end of the .js file which is linked to the webpage that calls the webpage.
    lol. Yes, I know. See my post #9, where I just altered your script slightly to get around having to reload the page. I'm not sure why you rejected it as a viable alternative.

    Anyway, I'm glad you got your problem solved.

  14. #14
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    NJ
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, initially I didn't reject it, I simply didn't completely understand that post, but I read it thoroughly now, and basically what you were implying on that post is that I am trying to add a script. I would like to show you the entire code to get rid of any further confusion on the matter, but considering how the .js file is close to 600 lines of code, that's probably not a good idea. Basically, to try to give a complete description, I have an html page that has the following code in the head tags:
    <script language=javascript type=text/javascript src="nblmnd.js"></script>
    and in the external file nblmnd.js, there is the following function:
    Code:
    function write(functionname,xval,yval,value){
    var name=functionname;
    var x=xval;
    var y=yval;
    var val=value;
    var funct=name+x+y;
    try{
       var fso, s;
       fso = new ActiveXObject("Scripting.FileSystemObject");
       s = fso.OpenTextFile("C:\\nblmnd.js" , 8, 1, -2);
    // ________________________________Start
    if (!window[funct]){
       s.writeline("function " + funct + "(xval,yval){var x=xval; var y=yval; if (x==" + x + " && y==" + y + "){return " + val + "}}");
       history.go(0)
       }
    // __________________________________End
       s.Close();
    }
    catch(err){
      var strErr = 'Error:';
      strErr += '\nNumber:' + err.number;
      strErr += '\nDescription:' + err.description;
      document.write(strErr);
    }
    }
    this function dynamically creates funtions that are textually appended to the end of the external .js file, the html file is untouched. But after appending the new functions, the script was apparently having a problem recognizing the newly created functions, and I got around this by refreshing the webpage after evertime a new function was added. Hopefully this clears up everything.
    Thanks for everything

  15. #15
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I see the problem you are talking about--it occurs when you try to put your output in the same file as the write() function. My earlier solution moved your write() function into the html file. Then, when the function executed it wrote the ouput to a separate .js file which was then used as the src attribute of some dynamically created <script> tags. When you dynamically create a script element(as well as an <iframe> element or an <img> element), the browser immediately tries to load the file in the src attribute, which is why it got around the reload problem.

  16. #16
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    India
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi stud,

    Your idea of window[functname] is really an acceptable one than eval. I just missed it. You know, years of programming put me into habit of using window.somevar rather than window[somevar]. Ok anyhow thanks a lot for bringing back memories of window[somevar].

    Have a nice day

    Afru.

  17. #17
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just missed it. You know, years of programming put me into habit of using window.somevar rather than window[somevar].
    It happens to everyone. In js, there is a way to use a string to get at almost any reference, e.g. window[], frames[], forms[], elements[], document.getElementById(), document.getElementsByName(), and document.getElementsByTagName() making eval() almost entirely superfluous.

  18. #18
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...hmmm, I see now that

    window[functionname] = function(){if(x && y) return value;};

    doesn't do what you want either--it doesn't write out the functions to the .js file. And, any document.write() statements in the .js file are going to write to the .htm file, so now I see why you went exploring with ActiveXObjects. I surrender:


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •