JavaScript - Simple Perl To Javascript Code
Hi everyone, I've been coding Perl for quite some time, but I'm new to Javascript and can't quite figure this out.
I want to call a Javascript function that is sent to the browser via a perl script. When I hard code the string "Fargo" into the code it works just fine, when I pass the word Fargo via a variable the script will not call the function what so ever. Is it possible to call a javascript function via a Perl script with Perl providing all the necessary data? Here is my code: #!/usr/local/bin/perl use CGI qw(:all); use CGI::Carp qw(fatalsToBrowser); use Cwd; print header; print "top<br><br>"; $data = "fargo"; print <<html; <html><head></head>my heading is here<br><body></body> <script type="text/javascript"> function testz(inbound) { document.write("im in ", inbound); } //the script will only work if I uncomment the line below and comment out two lines below //var data1="fargo"; var data1 = $data; testz(data1); </script> </html> html Thanks to everyone in advance for your help. Similar TutorialsHello, I have a perl code and I am animating using js. Can anyone explain how far they are compactible. Here is one of my problem... code: print "Qword(l);"; print "function Qword(l){"; print "if(l<5){"; print "$j=l;"; print "alert($j);"; print "l++;"; print "Qword(l);"; print "}"; print "}"; This is going into a infinite loop, this code comes from a perl fn I have declared $j. I changed it, still j is not incremented. print "function Qword(l){"; print "if(l<5){"; $j++; print "alert($j);"; print "l++;"; print "Qword(l);"; print "}"; print "}" Can anyone help me please.. Thank you, jv123 I have a Perl script that contains a variety of inputs including a HTML <SELECT> and RADIO buttons. I would like to alter the presentation of a RADIO button choice with a DISABLED, based upon the earlier selection of from a <SELECT> drop down menu. In Perl I attempted to capture the selection from the <SELECT> and use that value in a IF ELSE block, but this does not work. I have been told that this can be done with a Javascript. I did a little bit of coding with Javascript around five years ago, so I am a rusty newbie. Can anyone give me a hand on how this should be constructed? The more detailed your examples or suggestions can be the easier it will be for me to grasp the solution. Hi, I'm still in the early stages of learning Javascript so I'm not sure how (or if it's possible) to do this. I know HTML, CSS, etc... just not too familiar with Javascript. Here's the deal.... I run a website that features a chat room written in Perl for CGI. The chat room will automatically email a user their password if it's forgotten but there is one issue.....it does not have the "Forgot Password?" link. To achieve the Lost Password page, the user must enter an invalid password FIRST on the login screen, otherwise they cannot get to that option and are often requesting assistance. What I had in mind was an easier way for the user to get their password since new novice members are not sure how to get to the lost password page. I'm not trying to edit the Perl code for the chat, but I wanted to build a "Forgot Password?" page using a Javascript code that will automatically take their username from the "username" login field and direct them to the lost password page which would be "chat2.cgi?action=send_pwd&name=..." I'm sorry if this is confusing, I'm not sure that I am explaining this right. When the user enters an invalid password, it takes them to the lost password page which contains a link "lost password" and will automatically email them their login details when clicked on. When they click that link, the URL contains the string "chat2.cgi?action=send_pwd&name=username" What I would like to do is put a link on the login page (like most normal chat rooms have) that says "Forgot Password?". When clicked on, I want it to take them to a page where all they have to do is enter their username in a form and click Submit, then have a Javascript code automatically extract their username from that field and insert their username automatically to the end of that URL (chat2.cgi?action=send_pwd&name=...) where ... would be the name extracted from that form field. This would result in the browser taking them to that URL which would trigger the chat to automatically email the password. Can I do this with Javascript?? If so, any help would be greatly appreciated! hello all, I am new to javascript, i just wanted to know how can i send a array from perl to javascript function.... if anybody having any idea about this please reply me..thanks in adbvance I would like to compare my work to the work of others, can you please solve this and post your result? Declare an 8 x 8 matrix and an array of length 22. 1.- Populate the matrix. 2.- Copy the elements of the first row of the matrix, the anti-diagonal, and the last row of the matrix into the array, to form a Z shape. 3.- Sort the array. 4.- Assuming that the data in the arrays are grades, compute the average of the grades stored in the even locations of the array. 5.- Copy the array back into the matrix.(back into the Z) 6.- Print out the matrix values. I have a javascript code, which initiates via "onClick" when a user clicks on a checkbox. I downloaded the sample code, but the problem is is that I need the javascript to be initiated automatically, not when a user clicks on a checkbox. I would greatly appreciate it if anyone could help me resolve this issue. Below is a sample of the code I'm using. Thanks in advance. Code: <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <!-- iPad/iPhone specific css below, add after your main css > <link rel="stylesheet" media="only screen and (max-device-width: 1024px)" href="ipad.css" type="text/css" /> <link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="iphone.css" type="text/css" /> --> <!-- If you application is targeting iOS BEFORE 4.0 you MUST put json2.js from http://www.JSON.org/json2.js into your www directory and include it here --> <script type="text/javascript" charset="utf-8" src="phonegap.0.9.6.min.js"></script> <script type="text/javascript" charset="utf-8" src="SAiOSAdPlugin.js"></script> <script type="text/javascript" charset="utf-8"> // If you want to prevent dragging, uncomment this section /* function preventBehavior(e) { e.preventDefault(); }; document.addEventListener("touchmove", preventBehavior, false); */ var gLastAdLoadedDate = null; var gTotalAdsLoaded = 0; var gTimerId = null; function onBodyLoad() { document.addEventListener("deviceready", onDeviceReady,false); } function onOrientationChange() { //alert(window.orientation); } /* When this function is called, PhoneGap has been initialized and is ready to roll */ function onDeviceReady() { // listen for orientation changes window.addEventListener("orientationchange", window.plugins.iAdPlugin.orientationChanged, false); // listen for the "iAdBannerViewDidLoadAdEvent" that is sent by the iAdPlugin document.addEventListener("iAdBannerViewDidLoadAdEvent", iAdBannerViewDidLoadAdEventHandler, false); // listen for the "iAdBannerViewDidFailToReceiveAdWithErrorEvent" that is sent by the iAdPlugin document.addEventListener("iAdBannerViewDidFailToReceiveAdWithErrorEvent", iAdBannerViewDidFailToReceiveAdWithErrorEventHandler, false); var adAtBottom = false; setTimeout(function() { window.plugins.iAdPlugin.prepare(adAtBottom); // by default, ad is at Top }, 1000); } function iAdBannerViewDidFailToReceiveAdWithErrorEventHandler(evt) { alert(evt.error); window.plugins.iAdPlugin.showAd(false); var elem = document.getElementById("showAd"); elem.checked = false; } function iAdBannerViewDidLoadAdEventHandler(evt) { // if we got this event, a new ad is loaded var elem = document.getElementById("lastAdLoaded"); gLastAdLoadedDate = new Date(); elem.innerHTML = gLastAdLoadedDate.toLocaleString(); elem = document.getElementById("showAd"); elem.disabled = false; elem.checked = true; window.plugins.iAdPlugin.showAd(true); gTotalAdsLoaded++; elem = document.getElementById("totalAdsLoaded"); elem.innerHTML = gTotalAdsLoaded.toString(); if (gTimerId) { clearInterval(gTimerId); } gTimerId = setInterval(lastAdLoadedInterval, 1000); } function lastAdLoadedInterval() { var now = (new Date()).getTime(); var diff = now - gLastAdLoadedDate.getTime(); var elem = document.getElementById("lastAdLoaded"); var ms_in_a_year = 31449600000; /* 1000ms x 60s x 60m x 24hrs x 7d x 52w */ var ms_in_a_week = 604800000; /* 1000ms x 60s x 60m x 24hrs * 7d */ var ms_in_a_day = 86400000; /* 1000ms x 60s x 60m x 24hrs */ var ms_in_an_hour = 3600000; /* 1000ms x 60s x 60m */ var ms_in_a_minute = 60000; /* 1000ms x 60s */ var ms_in_a_second = 1000; var milliseconds = Math.floor(diff); var seconds = Math.floor(milliseconds / ms_in_a_second) % 60; var minutes = Math.floor(milliseconds / ms_in_a_minute) % 60; var hours = Math.floor(milliseconds / ms_in_an_hour) % 24; var days = Math.floor(milliseconds / ms_in_a_day) % 7; var weeks = Math.floor(milliseconds / ms_in_a_week) % 52; var years = Math.floor(milliseconds / ms_in_a_year); var caption = seconds + "s ago"; if (minutes > 0) { caption = minutes + "m " + caption; } if (hours > 0) { caption = hours + "h " + caption; } if (days > 0) { caption = days + "d " + caption; } if (weeks > 0) { caption = weeks + "w " + caption; } if (years > 0) { caption = years + "yr " + caption; } elem.innerHTML = caption; } function showAdClicked(evt) { window.plugins.iAdPlugin.showAd(evt.checked); } </script> </head> <body style="margin:0; padding:0;border:1px solid blue;" onload="onBodyLoad()"> <span style="position:absolute; top:0;"> This is some text at the top of the webview. </span> <br /><br /> <form> <input type="checkbox" id="showAd" name="showAd" disabled="disabled" onclick="showAdClicked(this);">Show iAd</input><br /> <br /> <span>New Ad Loaded: <span id="lastAdLoaded">Waiting.</span></span><br /> <span>Total Ads Loaded: <span id="totalAdsLoaded">None.</span></span> </form> <br /> <span style="position:absolute;bottom:0"> This is some text at the bottom of the webview. </span> </body> </html> The javascript (SoiOSADPLUGIN.JS) that is referenceD in this html page i also pasted below he Code: /** * Constructor */ function SAiOSAdPlugin() { } /** * show - true to show the ad, false to hide the ad */ SAiOSAdPlugin.prototype.orientationChanged = function() { PhoneGap.exec("SAiOSAdPlugin.orientationChanged", window.orientation); } /** * show - true to show the ad, false to hide the ad */ SAiOSAdPlugin.prototype.showAd = function(show) { PhoneGap.exec("SAiOSAdPlugin.showAd", show); } /** * atBottom - true to put the ad at the bottom, false to put the ad at the top */ SAiOSAdPlugin.prototype.prepare = function(atBottom) { if (!atBottom) { atBottom = false; } PhoneGap.exec("SAiOSAdPlugin.prepare", atBottom); } /** * Install function */ SAiOSAdPlugin.install = function() { if ( !window.plugins ) window.plugins = {}; if ( !window.plugins.iAdPlugin ) window.plugins.iAdPlugin = new SAiOSAdPlugin(); } /** * Add to PhoneGap constructor */ PhoneGap.addConstructor(SAiOSAdPlugin.install); I get the code to work when I click the checkbox, and it satisfies my needs, but having a checkbox toggle is not exactly ideal for this function. Is there anyway for me to have the "Onclick" function in this code changed to onload? I am creating an html document with perl. I wrote an event mouseover and mouseout to happen for every perl variable (over a loop). But looks like the event controls all the variables at the same time. How do I write the event only once but enable it to individually be applied for each item: this is what I have currently but this html when displayed, does not let me control the event separately for each $key. Even though the buttons do get created for each $key, clicking on one, controls the $stats_data{$out}{$key} of all. I even tried passing the id to the show/hide script, but no luck. Some help would be greatly appreciated. thanks Code: <header><script> function hideit() { var x1 = document.getElementsByTagName("ol").item(0); var x = document.getElementsByTagName("ol"); for (var i = 0; i < x.length; i++) x[i].style.display="none"; } function showit() { var x1 = document.getElementsByTagName("ol").item(0); var x=document.getElementsByTagName("ol"); for (var i = 0; i < x.length; i++) x[i].style.display=""; } </script><header> <body> foreach $key (keys %{$stats_data{$out}}) { print $indexfd ("<input onclick=\"showit()\" type=\"button\" id=\"<?perl echo $key; ?>\" value=\"showit\"><br><input onclick=\"hideit()\" type=\"button\" id=\"<?perl echo $key; ?>\" value=\"hideit\"><br><b><ol id=$key>$stats_data{$out}{$key}<ol id=$key> </b><br>"); }</body> Hello, I am trying to retrieve a cookie I stored with Perl through javascript. Based on the contents of the cookie, I want the javascript to print/not print an extra link on my page. Below is the code, in Perl, I am using to store the cookie. In the Perl forum I was told to post in here for the javascript help. The basic idea is that if the cookie has a "Role" of "Super," the ADMIN link displays on the page. If the role doesn't have "Super," the link is hidden. Any help you could offer would be greatly appreciated. Thanks a lot. Code: #!/usr/local/bin/perl # This page was created and worked on by 2/C Collard, 2/C Waymouth, 2/C Troisi, and 2/C Cunha #source: http://forums.speedguide.net/showthread.php?t=190821 # PERL MODULES WE WILL BE USING use DBI; use DBD::mysql; use CGI qw( :standard ); use CGI::Carp qw(fatalsToBrowser); #print "Content-type: text/html \n\n"; $userPassed = param("userID"); # CONFIG VARIABLES $platform = ""; $database = ""; $host = ""; $port = ""; $tablename = ""; $user = ""; $pw = ""; # DATA SOURCE NAME $dsn = "dbi:$platform:$database:$host:$port"; # PERL DBI CONNECT $connect = DBI->connect($dsn, $user, $pw) or die "Connection Error: $DBI::errstr\n"; # PREPARE THE QUERY my $query = "SELECT * FROM $tablename WHERE User = '$userPassed'"; my $query_handle = $connect->prepare($query); #print ($query); # EXECUTE THE QUERY $query_handle->execute(); while (@row = $query_handle->fetchrow_array) { $roleIn = "$row[2]"; #print ("$roleIN"); } $query = new CGI; # create a new CGI object $cookie = $query->cookie ( -name => 'Role', -value => '$roleIn', -path => '/', -expires => '+60m'); $location = ''; print $query->header(-cookie=>$cookie); print qq{<meta http-equiv="REFRESH" content="0;URL=http://www.togoto.com">\n}; # HTTP HEADER #print( header() ); #print ( start_html() ); # Print XHTML footer #print ( end_html() ); I imagine this would be very simple for someone who knows javascript. I want to have three fields. First field is "posted speed limit", second field is "actual speed" and third field will be the output field. All the script needs to do it subtract the posted speed from the actual speed and add a ZERO to the end; which is the amount of the speeding ticket. The minimum fine is $100, however. So, 5 miles over the speed limit would be $100 (minimum value) 15 miles over the speed limit would be $150 (add a zero) 35 miles over the speed limit would be $350. etc. I know very little Javascript, if anyone could help me out with this, I'd appreciate it. Thanks, Sean The code I commeted out dosen't work I want to check if its null and if so alert them. { // PART 2: YOUR CODE STARTS AFTER THIS LINE correctAnswer = Math.floor((Math.random() * 100) + 1); // For testing purposes. alert("Testing purposes, correct answer is: " + correctAnswer); var number = 0; //// Calculations //if (isNaN(number) = true) { // alert("Thats not a number"); //} //document.write(number); while (number != correctAnswer) { number = prompt("Wrong Number, guess again."); number = Number(number); if (number < correctAnswer) { document.writeln("Small guess: " + number); } if (number > correctAnswer) { document.writeln("Large guess: " + number); } if (number == correctAnswer) { document.writeln("Correct guess: " + number); } } } Here's the basic concept, its somewhat of a counter, and I think javascripts the way to go... Basically I'm looking to generate a running number... x + 11 = print the number The tricky part is, I need it to never stop, somewhat like a timer (example http://www.hashemian.com/tools/javascript-countdown.htm) but not a timer... Think the tally mark on McDonald's 99 billion served.. Any suggestions or ideas? Hi... i have this code: <script type='text/javascript'> cookie_name = GetCookie("href_location"); if(cookie_name){ alert("Cookie found, redirecting to stored cookie."); document.location.href=cookie_name; } </script> I will like to create a iframe using the info from the "cookie_name" value <iframe name="FRAME1" src="cookie_name" width="350" height="320" frameborder="0" scrolling="auto"></iframe> I have a script that set a cookie with the value of a chosed website, what i need now is to create an iframe with that value. Any help will be apriciated. Sorry for my poor enlish I just started learning JavaScript. I'm sure it's something really simple but I cannot figure out what is missing from my code. No matter what year I enter into the text box, I keep getting "The year you entered, 2080393, is a leap year." It's not returning false...ever. Can someone please tell me what I'm doing wrong so I can stop pulling my hair out? Thanks! <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>Is It A Leap Year?</title> <script type="text/javascript"> /* <![CDATA[ */ function isLeapYear(year) { year = parseInt(year); var leapYear = true; var remainder = 0; if(isDivisibleBy(year, 4)) { leapYear = true; } if(isDivisibleBy(year, 100)) { leapYear = false; } if(isDivisibleBy(year, 400)) { leapYear = true; } if(leapYear) { window.alert("The year you entered, " + year + ", is a leap year."); } else { window.alert("The year you entered, " + year + ", is not a leap year."); } } function isDivisibleBy(year, divisor) { var remainder = 0; remainder = year%divisor; if(remainder == 0) { return true; } else { return false; } } /* ]]> */ </script> </head> <body> <form action="" id="GetLeapYear"> <p><strong>Is It a Leap Year?</strong></p> <p><strong>Enter a valid 4-digit year below to find out!</strong><br /></p> <p> <input type="text" name="year" size="20" style="color: black; border-style: solid; border-color: inherit; border-width: medium; background-color: Transparent" value="" /> <input type="button" value="Leap Year?" onclick="isLeapYear(document.getElementById('GetLeapYear').year.value);" /> </p> </form> </body> index.html Code: <html> <head> <title>:: wtmp</title> </head> <body> <p>Welcome to my page</p> <script type="text/javascript" src="file.js"></script> </body> </html> file.js Code: function one(p1, p2) { var j_text=p1+" "+p2; return j_text; } function two() { var rslt=one("Hi", "there!"); } document.write(rslt); var mainscrpt=one("Hello", "world!"); window.alert(mainscrpt); Simple random pic script that I found on the some other forum (I forget what it was) [CODE]var aryimages = new Array('images/pic/01.jpg', 'images/pic/37.jpg', 'images/pic/02.jpg', 'images/pic/family/08.jpg','images/pic/08.jpg', 'images/pic/food/03.jpg'); randompic.src = aryimages[Math.floor(Math.random() * aryimages.length)]; [CODE] code anchors to HTML markup [CODE]<img name="randompic" id="bg" />[CODE] it is working perfectly for Safari and Chrome. Nothing is showing for FF. Any suggestions appreciated. I'm getting a runtime error on the code in red. Can anyone see anything that is wrong with it? These functions are called by an onBlur by the way. <script type="text/javascript"> //THIS GENERATES QUANTITY OF MATERIALS function generate3() { var tot3 = document.ComplianceForm.materialamount.value; var tbl3 = document.getElementById("quantitymaterials"); if (tot3 > 28) { alert("16 is total amount of materials for now."); } else { for(var i =1; i<=tot3; i++) { tbl3.innerHTML = tbl3.innerHTML + '<tr><td colspan=\"2\"><hr /></td></tr><tr><td>'+i+') Quantity: <input type=\"text\" name=\"qty'+i+'\" size=\"3\"\/></td><td>Description: <input type=\"text\" name=\"material'+i+'\" /></td></tr><tr><td colspan=\'2\' \">Your Truck<input type=\"checkbox\" \/> Other truck<input type=\"checkbox\" \/> Cash<input type=\"checkbox\" \/> Credit Card<input type=\"checkbox\" \/> P.O.<input type=\"checkbox\" \/></td></tr>'; } tbl3.innerHTML = tbl3.innerHTML + '<br>'; } } function remove3() { var tot3 = document.ComplianceForm.materialamount.value; var tbl3 = document.getElementById("quantitymaterials"); for(var i =1;i<=tot3; i++) { tbl3.innerHTML = ""; document.ComplianceForm.materialamount.value = ""; tbl3.innerHTML = ""; } } </script> I'm pretty new to Javascript so don't lay into me too hard. haha thanks. -Ty Hello, What I need is a simple service area, zip code validation form that redirects to a certain URL when a valid zip code is submitted and a different URL when an invalid zip code is submitted. I found a form script example that works well with only a single zip code. My problem is I can't figure out how to modify it so that multiple zip codes are valid. Here is the head part. 60016 is one of about 50 valid zip codes I need the form to accept as valid - [code] <script> var correctCode = "60016"; function validateCode() { var code = document.getElementById("codeTextBox").value; if (code == correctCode) { window.location.href = "/ggc/test1"; } else { window.location.href = "/ggc/test2"; } } </script> [code] Here is the body part - [code] Please enter your zip code: <input type="text" name="codeTextBox" id="codeTextBox" /> <input type="submit" name="Submit" value="Submit" onclick="validateCode()" /> [code] So frustrating that lately I've been having trouble with the simplest of code, and this is about as simple as it gets. Matched it up with every similiar example I can find both in books and on the net, and it seems to be perfectly valid code, but when I run it, nothing. :S Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script language="javascript" type="text/javascript"> function favouriteColour() { var index = document.getElementById("select1").options[document.getElementById("select1").selectedIndex].value; document.getElementById = "Your favourite colour is " + index; } </script> </head> <body> <select id="select1"> <option></option> <option id="black" onchange="favouriteColour()">Black</option> <option id="white" onchange="favouriteColour()">White</option> <div id="favcolour"></div> <option> </select> </body> </html> Thanks in advance. |