1

我想确定用户从哪个网站提交了联系表格。

联系表格是一个包含文件 (.shtml),该文件正在两个不同的网站上使用。由于它只有一个文件(包含),javascriptif...else语句或switch语句会这样做吗?如果是这样,它会怎么写?

我是 JavaScript 新手,不需要写太多东西,所以希望能得到语法方面的帮助。

我考虑过在一个包括联系表单上添加两个隐藏字段,其中包含两个不同站点的 ID:

html

<input type="hidden" data-id="new_student_signup" name="student_signup" value="" />
<input type="hidden" data-id="existing_student_signup" name="existing_student_signup" value="" />

js

if(data-id="new_student_signup"){

  // this user came from the new student website

   code if this condition is true
 }

else if(data-id="existing_student_signup"){

  // this user came from the existing student website

   code if this condition is true

 }

或者我应该为另一个网站创建一个新的包含文件并将隐藏字段添加到该文件而不是在一个包含上使用 javascript?

我承认,对于像我这样的 JavaScript 新手来说,这似乎是最容易的,但我确实喜欢学习更多 JavaScript 的想法。

4

2 回答 2

1

您可以通过检查使用服务器端脚本来执行此操作HTTP_REFERRER

PHP 示例

<?php
if(!empty($_SERVER['HTTP_REFERRER']) && $_SERVER['HTTP_REFERRER'] == 'http://google.com/')
{
// proceed accordingly
}

您可能希望根据域名或 URL 中更具体的数据而不是整个 HTTP 引荐来源网址来区分引荐来源网址。在这种情况下,请查看http://php.net/parse_url


您也可以使用 JavaScript 来访问document.referrer属性。

于 2012-02-01T11:51:32.897 回答
0

您可以通过检查document.url 属性并将其放入隐藏的输入字段来在 javascript 中执行此操作。但是,我认为您最好只在服务器端编程代码中检查这一点。您只需检查您所在的当前服务器并使用它来确定它是哪种形式。您可以通过获取SERVER_NAME服务器变量来执行此操作,这将告诉您当前所在的域。

于 2012-02-01T15:15:05.687 回答