This is a bit tough for me to do appreciate some help.
I give this code to clients to place on their site, pass data to me and load dynamic content:
<script type="text/javascript" src="https://example.com/countdown_timer_evergreen.php"
data-launch_owner_email_hashed="94bd214b329301668349352de430bb6d"
data-launch_id="43" data-email="<?php echo $_GET['email'];?>">
</script>
Since a lot of clients don't have php, I have to use Javascript to capture the email from $_GET. So I use such code:
<script>
var getUrlParameter = function getUrlParameter(sParam)
{
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++)
{
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam)
{
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
}
var email = getUrlParameter('email');
</script>
How can I update my original code, replace with the javascript output of the content (capture the email with JS above) and give them the code? I want to give them 1 piece of code. Not sure how to pass the value of email between scripts.
And this is my code that receives the data (countdown_timer_evergreen.php)
<?php
//Allow cross-origin requests
header('Access-control-allow-origin: *');
$action = isset($_GET['action'])?$_GET['action']:null;
switch ($action){
case 'load-template':
DoLoadTemplate();
break;
default:
DoDefault();
}
exit;
function DoDefault(){
header('Content-type: text/javascript');
?>
(function(window){
var currentScript = document.currentScript;
var apiUrl = currentScript.src;
if (!('jQuery' in window)){
loadJQuery(initialize);
} else {
initialize();
}
function loadJQuery(cb){
var script = document.createElement('script');
script.src = 'https://code.jquery.com/jquery-3.3.1.min.js';
script.type = 'text/javascript';
script.addEventListener('load', cb);
document.getElementsByTagName('head')[0].appendChild(script);
}
function initialize(){
var $currentScript = $(currentScript);
var params = $.param({
action: 'load-template'
, launch_owner_email_hashed: $currentScript.data('launch_owner_email_hashed')
, launch_id: $currentScript.data('launch_id')
, email: $currentScript.data('email')
});
console.log(params);
$.get(apiUrl, params).then(function(html){
var div = $('<div>').html(html);
$currentScript.after(div);
});
}
}(this));
<?php
}
function DoLoadTemplate()
{
header('Content-type: text/html; charset=utf-8');
$launch_owner_email_hashed = htmlspecialchars($_GET['launch_owner_email_hashed']);
$launch_id = htmlspecialchars($_GET['launch_id']);
$email = htmlspecialchars($_GET['email']);
if($_SERVER['SERVER_ADDR']=="::1")
{
$root = "http://local.moosh.com/eg-launch-timer";
}
else
{
$root = "https://moosh.com/eg-launch-timer";
}
?> dd
<iframe src="<?php echo $root.'/'.$launch_owner_email_hashed.'/'.$launch_id.'/'.$email;?>" style="width:100%; height:100%; border:none;">
</iframe>
<?php } ?>
Open to ideas
Thanks
</div>