使用Javascript访问$ _GET并更新<script data-email =“”> </script>

时间:2018-05-01 04:11:49

标签: javascript php attributes

对于我来说,感谢一些帮助有点困难。

我将此代码提供给客户以放置在他们的网站上,将数据传递给我并加载动态内容:

<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 } ?>

开放的想法

由于

0 个答案:

没有答案