CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [perlFun] Saving variable result of a javascript being executed in Perl CGI: Edit Log



Zhris
Enthusiast

Jun 11, 2015, 9:19 PM


Views: 2426
Re: [perlFun] Saving variable result of a javascript being executed in Perl CGI

Hi,

Thats pretty much correct if you are referring to the url being passed in via the form, otherwise remember everything after the # of the actual incoming url will have been stripped off. Note that URI::Query should be used with query parameters, but the # that is of concern is actually in the path, therefore you should use URI::Escape directly. I would suggest using URI to parse the url and handle everything for you, but it would assume everything after the hash is a fragment and wouldn't recognize the # as being part of the path, however if you can safely assume fragments shouldn't occur then you could use a substitution beforehand:


Code
my $url = $q->param( 'url' ) // ''; 
$url =~ s/#/%23/g;
my $uri = URI->new( $url );
print $uri->as_string;


I don't fully understand your overall problem, but if an external website is calling your website via "http://<site>/#/<info>/", they need to escape that # in the path their end, or they are expecting you to handle it client side. Another suggestion is to hit a landing page which reads the fragment client side, escapes it, puts it in a query parameter i.e. ?val=..., then redirects to your cgi which retrieves it i.e. param('val'):

http://demo.massweb.co.uk/serversidefragment.pl#<stuff>

Code
use strict; 
use warnings FATAL => qw/all/;
use CGI::Carp qw/fatalsToBrowser/;
use CGI;
use Template;

my $cgi = CGI->new;

my $template = Template->new or die $Template::ERROR;

my $val = $cgi->param( 'val' );

my $placeholders = ( defined $val ) ? { view => 'main', val => $val } : { view => 'landing' } ;

print $cgi->header;

$template->process( \*DATA, $placeholders ) or die $template->error;

__DATA__
[%- IF view == 'landing' -%]
<script type="text/javascript">
var val = encodeURIComponent( window.location.hash );
var main_url = 'serversidefragment.pl?val=' + val;
window.location.href = main_url;
</script>
[%- ELSIF view == 'main' -%]
<html>
<head>
<title>...</title>
</head>
<body>
<p>[% val | html_entity %]</p>
</body>
</html>
[%- END -%]


Chris


(This post was edited by Zhris on Jun 11, 2015, 11:42 PM)


Edit Log:
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 9:29 PM
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 9:30 PM
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 9:35 PM
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 9:37 PM
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 9:38 PM
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 9:41 PM
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 9:52 PM
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 9:56 PM
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 9:57 PM
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 9:58 PM
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 10:42 PM
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 10:44 PM
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 10:47 PM
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 10:48 PM
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 10:49 PM
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 10:58 PM
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 11:16 PM
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 11:22 PM
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 11:30 PM
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 11:38 PM
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 11:41 PM
Post edited by Zhris (Enthusiast) on Jun 11, 2015, 11:42 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives