Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Perl & JavaScript

by akm2 (Scribe)
on Mar 09, 2001 at 02:37 UTC ( [id://63117]=perlquestion: print w/replies, xml ) Need Help??

akm2 has asked for the wisdom of the Perl Monks concerning the following question:

I am building a website fully controled by PERL. Some functions of the site may require JavaScript. Anybody know how to use perl to find out if a browser is JavaScript enabled using PERL?

Replies are listed 'Best First'.
Re: Perl & JavaScript
by Adam (Vicar) on Mar 09, 2001 at 02:43 UTC
    Not without getting the user to submit a cgi-form. Then you can put something like:
    <script language="javascript"> // <!-- document.write("<input type='hidden' name='js' value='1'>"); // --> </script>
    Then look for that hidden field in your code. If it is there then they have javascript enabled.
    use strict; use CGI; my $q = CGI->new(); if( $q->param('js') ) { # They have javascript! } else { # they don't. }
Re: Perl & JavaScript
by arturo (Vicar) on Mar 09, 2001 at 02:45 UTC

    There's no really simple way. The best way to go, in my opinion, is to assume that users won't have Javascript (or that they have it disabled) and make your site as functional as possible.

    That said, you might try some sort of cookie magic; Javascript is supposed to be able to set cookies, so you could put some Javascript in the login page that will set a cookie indicating that Javascript is enabled. (You'll need a login page for that ... but that might be as simple as the site's index page) Then have your Perl scripts check to see if that cookie value is set, and those scripts can then act on that information.

    <update> here's some code

    <head> <script type='text/javascript'> <!-- document.cookie='javascript=1; expires= ' +nextyeartoGMTString(); // that's a durable cookie! // --> </script>

    and then something like Adam's approach'll do ya for reading the cookies. </update>

    This is not foolproof, though : not every user will have cookies turned on, either.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Perl & JavaScript
by gryphon (Abbot) on Mar 09, 2001 at 02:58 UTC

    Greetings,

    This isn't a very creative solution, but it works. (Sort of.) On the "enterance page" to your site, or where ever you want to test for JavaScript, give the user an auto-jump page just prior to their actual CGI destination.

    <HTML><HEAD><TITLE>Something Interesting</TITLE> <SCRIPT language="JavaScript"><!-- document.location.href="/cgi-bin/destination.cgi?hasjs=1"; // --></SCRIPT> </HEAD> <NOSCRIPT> <Meta HTTP-EQUIV="Refresh" CONTENT="0;URL=/cgi-bin/destination.cgi?has +js=0"> </NOSCRIPT> </HTML>

    Couple things bad about this. First of all, it forces the user to make an additional HTTP hop before getting to destination.cgi or where ever. The other bad thing is that the "?hasjs=" thing shows up in the URL. That annoys me.

    Another idea might be to attempt to write a cookie with JavaScript when the user first gets to your site, then read that cookie with each Perl CGI. If Perl can read the cookie, then you know JavaScript was at least enabled back when you set the cookie (few seconds, couple minutes). Unfortunately, this solution relies on the user allowing the cookie, which some users still have problems with.

    -Gryphon.

Re: Perl & JavaScript
by gryphon (Abbot) on Mar 09, 2001 at 03:17 UTC

    Greetings akm2,

    I just figured something else out which I think is a better solution than my previous ideas. In each page, add a JavaScript function to run at the completed page load that will tack on the "?hasjs=1" thing to each link's URL.

    <HTML><HEAD><TITLE>Something Even More Interesting</TITLE> <SCRIPT language="JavaScript"><!-- function markForJS() { for (var x = 0; x < document.links.length; x++) { document.links[x].href = document.links[x].href + '?hasjs=1'; } } // --></SCRIPT> </HEAD><BODY bgcolor="#ffffff" onLoad="markForJS()"> <A href="script-1.cgi">Link 0</A><BR> <A href="script-2.cgi">Link 1</A><BR> <A href="script-3.cgi">Link 2</A><BR> <A href="script-4.cgi">Link 3</A><BR> <A href="script-5.cgi">Link 4</A> </BODY></HTML>

    Now all you have to do is read param('hasjs') in your CGIs. Perhaps this, combined with some other ideas will get you a complete solution that will solve your problem. Good luck!

    -Gryphon.

      Don't forget the hard working JavaScript programmer, you don't wanna mess up his stuff ;-)
      <HTML><HEAD><TITLE>All Hail The JavaScript Perl Programmer</TITLE> <SCRIPT language="JavaScript"> <!--// function markForJS() { for (var x = 0; x < document.links.length; x++) { d_href = document.links[x].href; // if there are no javascript links // add the cgi stuff, otherwise pee into the wind if((d_href.indexOf("javascript:")+0)==-1) { document.links[x].href = document.links[x].href + '?hasjs= +1'; } } } // --> </SCRIPT> </HEAD><BODY bgcolor="#ffffff" onLoad="markForJS()"> <A href="script-1.cgi">Link 0</A><BR> <A href="script-2.cgi">Link 1</A><BR> <A href="script-5.cgi">Link 4</A><BR> <A href="javascript:alert('chorg!');">Link javascript</A> <A href="javascript:alert('zort!');">Link javascript2</A> </BODY></HTML>

       
      ___crazyinsomniac_______________________________________
      Disclaimer: Don't blame. It came from inside the void

      perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

      Great idea, gryphon.

      To make this more robust, we would want to check each link for a value in  document.links[x].search (i.e. the query string). Then we would know whether to glue the hasjs=1 bit on with a question mark or an ampersand.

      Documentation on search.

Re: Perl & JavaScript
by rrwo (Friar) on Mar 11, 2001 at 03:38 UTC

    A few ways (without giving detailed JavaScript code):

    • When a user first starts a session on your web site, give a small referrer page that takes one to a nan-JavaScript site, but have a JavaScript event for on-load that overrides it
    • Use JavaScript to send a cookie that says "JavaScript=on" (I've seen some web sites do this; I think Netscape's home site used to do this.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://63117]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-20 04:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found