javascript - 使用 Javascript 访问 $_GET 并更新
问题描述
这对我来说有点难以感谢一些帮助。
我将此代码提供给客户放置在他们的网站上,将数据传递给我并加载动态内容:
<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>
由于很多客户没有 php,我必须使用 Javascript 来捕获来自 $_GET 的电子邮件。所以我使用这样的代码:
<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>
如何更新我的原始代码,替换为内容的 javascript 输出(使用上面的 JS 捕获电子邮件)并给他们代码?我想给他们一段代码。不确定如何在脚本之间传递电子邮件的值。
这是我接收数据的代码(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 } ?>
开放的想法
谢谢
解决方案
推荐阅读
- android - Travis CI - 多项目结构
- swift - UIStatusBarStyle 不会改变
- python - 快速验证生成的字符串(+数学)
- mysql - Mysql left join 从结果中删除重复值
- laravel - VueJS + element.eleme.io 如何为 el-select 设置默认选项值
- ios - 将 [String:AnyObject] 类型的字典分配给 [String: String] 给出零错误
- ruby-on-rails - ActiveAdmin 和动态 Rails 命名空间
- core-data - 核心数据 NSManagedObject 转换为 json 格式
- python - 无法在 codechef 上解决 Python 中的 NZEC 错误,尽管它在 IDE 上给出了正确的输出
- ios - iOS - 大标题隐藏了我的身体顶部