Client-side PerlScript Part 2
Results 1 to 6 of 6

Thread: Client-side PerlScript Part 2

  1. #1
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,403

    Client-side PerlScript Part 2

    Sigh.. I got another problem And there isn't a lot of info about perlscript...
    Consider the following code:
    Code:
    <HTML>
    <HEAD>
    <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
    <SCRIPT LANGUAGE=PerlScript>
    $doc = $window->document;
    
    sub mydynbody {
    	$objMyBody = $doc->getElementsByTagName("BODY")->item(0);
    	
    	$objButton = $doc->createElement("INPUT");
    	$objButton->setAttribute("id","mybutton");
    	$objButton->setAttribute("value", "Go");
    	$objButton->setAttribute("type", "button");
    	$objButton->setAttribute("onclick", "klik()"); 
    	
    	$objMyBody->appendChild($objButton);
    		
    }
    
    sub klik {
    	$doc->write("<H1>Klik!</H1>");
    }
    
    sub dbg {
    	$window->alert($doc->body->outerHTML);
    }
    
    1;
    </SCRIPT>
    <TITLE>test</TITLE>
    </HEAD>
    <BODY onLoad="mydynbody()">
    <INPUT TYPE=button VALUE="Debug" onclick="dbg()">
    </BODY>
    </HTML>
    The Debug button works as it should. The dynamicly created klik button doesn't. It shows up but the onclick event doesn't fire up the klik() sub. Digging around I found this article. It seems IE doesn't like it done this way. They've solved this by using new Function("klik()"); in JavaScript.
    So what is the PerlScript equivalant of JavaScript's new Function?
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  2. #2
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,403
    I did some more digging. It turns out JavaScript has the same problem so it isn't limited to PerlScript.

    Found 2 very interesting articles here and here.
    They're suggesting to use addEventListener (which isn't supported by IE) or attachEvent (which is supported by IE only).
    So I looked up MSDN: attachEvent.
    Seems they're passing a reference to a function ( getRef ).

    Tried to change my code using this:
    Code:
      $coderef= sub{ 
                 $window->document->write("Ref klik")
      };
      
      $objButton->attachEvent("onclick", $coderef );
    Didn't work
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  3. #3
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,403
    Javascript:

    var oButton = document.getElementByID("mybutton");
    oButton.setAttribute("onclick" , "myfunction"); // works on all browsers except IE
    oButton.onclick=myfunction; // should work on all browsers including IE
    oButton.attachEvent("onclick", myfunction) // IE only

    PerlScript (only works in IE so I cannot try another browser):

    $oButton = $window->document->getElementByID("mybutton");
    $oButton->setAttribute("onclick", "myfunction"); # doesn't work
    $oButton->{onclick}=klik; # doesn't work
    $oButton->attachEvent("onclick", klik); # doesn't work
    $oButton->attachEvent("onclick", \&klik ); #doesn't work

    I'm running out of options
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  4. #4
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,403
    Damn. Looks like PerlScript has a bug or something. I converted my perlscript example to javascript.. Both objButton.onclick=klik AND objButton.setAttribute("onclick", klik) work in IE.

    I wonder why javascript: objButton.setAttribute works and perlscript:$objButton->setAttribute doesn't... I think it's time to send an email to ActiveState ....
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  5. #5
    Member
    Join Date
    Apr 2005
    Posts
    41
    May I ask which parser you are using?


    [edit]
    Nevermind I just saw the first one of these you posted.
    Still using ActivePerl 5.8, I assume!?
    [/edit]

    Im going to try a few things out for you ...... after my nap I will get back to you on this - assuming you are still having difficulties ...........
    They can steal all my property and belongings, curtail all my rights and privileges, incarcerate me, beat me and even kill me. They then, will only have my dead body, NOT my obedience.

  6. #6
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,403
    Yes, I'm still using ActivePerl 5.8..

    No rush.. I converted my entire script to JavaScript.
    Which already does most of what I want to do..

    But if you have any perlscript ideas I'd be happy to hear them
    I'm still considering this a bug..
    Oliver's Law:
    Experience is something you don't get until just after you need it.

Posting Permissions

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

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides