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

I have this as my current header.
print start_html(-head=>[Link({-rel=>'StyleSheet', -href=>'main.css', +-type=>'text/css'})], -title=>"Welcome to my site!", -meta=>{'robots' +=>'nofollow'});
I need to add this to it, too, but I don't see how I can do that using my current header tag. The no right click JS needs to be in the header section.
script language="Javascript1.2"> // (C) 2003 CodeLifter.com // Source: CodeLifter.com // Do not remove this header // Set the message for the alert box am = "This function is disabled!"; // do not edit below this line // =========================== bV = parseInt(navigator.appVersion) bNS = navigator.appName=="Netscape" bIE = navigator.appName=="Microsoft Internet Explorer" function nrc(e) { if (bNS && e.which > 1){ alert(am) return false } else if (bIE && (event.button >1)) { alert(am) return false; } } document.onmousedown = nrc; if (document.layers) window.captureEvents(Event.MOUSEDOWN); if (bNS && bV<5) window.onmousedown = nrc; </script>

Replies are listed 'Best First'.
Re: how to print CGI header
by Corion (Patriarch) on Feb 22, 2006 at 16:10 UTC

    Looking through the CGI documentation, especially the section on Support for JavaScript, the following should do what you want:

    my $js = 'alert("Hello World");'; # replace this with your JS code print start_html(-head=>[Link({-rel=>'StyleSheet', -href=>'main.css', +-type=>'text/css'}), script($js)], -title=>"Welcome to my site!", -me +ta=>{'robots' =>'nofollow'});

    But you could also consider using one of the many templating systems instead of creating your HTML by writing code. HTML::Template is a very simple templating system, and Template Toolkit has much more options.

    You should also be aware that disabling the right-click does not serve any purpose other than to annoy the visitors to your website - the context menu offers many options, like navigation, cut'n'paste or inspection of the website addresses. It's easy to disable any JavaScript code that tries to intercept right-clicks.

Re: how to print CGI header
by marto (Cardinal) on Feb 22, 2006 at 16:08 UTC
    Hi Anonymous Monk

    Have a read at the CGI documentation, under the section titled CREATING THE HTML DOCUMENT HEADER:
    print $q->start_html(-title=>'The Riddle of the Sphinx', -script=>{-language=>'JAVASCRIPT', -src=>'/javascript/sphinx.js'} );
    This will not stop people who have their browsers setup not to execute JavaScript from doing a "Right click" -> "View Source", or using "View" -> "Page source" on the menu.

    Martin
Problems with JavaScript from copy/paste sites
by dorward (Curate) on Feb 22, 2006 at 17:18 UTC

    You've already recieved some good advice about how to include the JavaScript, but there are a few points I'll raise as an aside.

    1. The language attribute is deprecated and the type attribute is required. This change happened in HTML 4, which is about a decade old. There is no reason to be using HTML 3.2 these days.
    2. Using the navigater.appName to identify the browser is very fragile and should be replaced by object detection.
    3. The only thing that attempting to block the context menu will achieve is to annoy users who use the context menu (and note that "preventing users from accessing the context menu" and "preventing users from viewing source code" are not included in the above!). You may find my comments on Protecting images and source code on the web from thieves useful.