Javascript 如何将当前域传递给生成js的php脚本?
我有:Javascript 如何将当前域传递给生成js的php脚本?,javascript,php,Javascript,Php,我有: 域_A中的页面test.html 域_B中的php脚本生成器.php 我有以下需要: 在test.html中包含由generator.php生成的javascript文件 根据requestant域生成此文件(因此generator.php必须知道requestant域) test.html文件可以加载到我可能不知道的域中 我提出了两个想法: 1) 使用$\u服务器['HTTP\u REFERER'] 在test.html中: <script type="text/
- 域_A中的页面test.html
- 域_B中的php脚本生成器.php
1) 使用$\u服务器['HTTP\u REFERER'] 在test.html中:
<script type="text/javascript" src="DOMAIN_B/generator.php">
<script>
var js = document.createElement("script");
js.type = "text/javascript";
js.src = "DOMAIN_B/generator.php?requestant_domain="+window.location.hostname;
document.body.appendChild(js);
</script>
在generator.php中:
if( $_SERVER['HTTP_REFERER']=="DOMAIN_A" ) echo "<some js code>";
echo "<some js code according to ".$_GET['requestant_domain'].">";
if($_服务器['HTTP\u REFERER']==“域A”)echo”“;
不幸的是,变量$\u SERVER['HTTP\u REFERER']不可靠-来源:
2) 包括通过请求方域的javascript 在test.html中:
<script type="text/javascript" src="DOMAIN_B/generator.php">
<script>
var js = document.createElement("script");
js.type = "text/javascript";
js.src = "DOMAIN_B/generator.php?requestant_domain="+window.location.hostname;
document.body.appendChild(js);
</script>
var js=document.createElement(“脚本”);
js.type=“text/javascript”;
js.src=“DOMAIN_B/generator.php?requestant_DOMAIN=“+window.location.hostname;
document.body.appendChild(js);
在generator.php中:
if( $_SERVER['HTTP_REFERER']=="DOMAIN_A" ) echo "<some js code>";
echo "<some js code according to ".$_GET['requestant_domain'].">";
echo”“;
你认为什么可能是最好的解决方案
你有其他的建议方式吗?是的,在某种形式上,你想要明确地传递某种id(域名或其他),如2所示。是@deceze,你是对的;这可能是任何争论。