在索引文件中使用js [英] use of js in index file

查看:109
本文介绍了在索引文件中使用js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否将javascript放在index.php的开头,以便它是在网页上加载的第一个代码?

香港专业教育学院尝试过,但是该网站根本无法加载.我需要使用此代码来找出用户是否单击了链接,以从移动网站上在完整站点上查看完整文章.

Can you put javascript at the beginning of the index.php so that it is the first code that loads on a webpage?

Ive tried, however the website won''t load at all. I need to use this code to find out if the user is clicking a link to full an article on the full site from the mobile site.

if ( document.referrer.search('http://oursite.com')== 0 )
{
   include "index2.php";
}
else
{
   include "mobiledetection.php";
   /* plus the other code already in the index file*/
}



index2.php是之前索引文件中的代码.
香港专业教育学院试图使用没有JavaScript的document.referrer函数,并在外部js中尝试它,但可以工作



The index2.php being the code that was in the index file before.
Ive attempted to use the document.referrer function without javascript, and try it in an external js but either works

推荐答案

PHP. document.referrer是Javascript,include是PHP.

由于Javascript是在客户端上发生的,因此您需要显示一个包含Javascript代码的页面并重定向到另一个页面以获得所需的效果,或者在PHP中完成所有操作,然后可以使用include命令:
It looks like you''re mixing up Javascript and PHP. document.referrer is Javascript and include is PHP.

Since Javascript happens on the client, you would need to either display a page containing your Javascript code and redirect to another to get the effect you want, or do it all in PHP where you can use the include command:
<?php
// yes, this is how HTTP_REFERER is spelled
if(isset(


_SERVER [' HTTP_REFERER']) && strpos(
_SERVER['HTTP_REFERER']) && strpos(


_SERVER [' HTTP_REFERER'],' http://oursite.com')=== 0){ 包括" ; } 其他 { 包括" ; } ?>
_SERVER['HTTP_REFERER'],'http://oursite.com') === 0) { include "index2.php"; } else { include "mobiledetection.php"; } ?>



您还应该注意,引荐来源网址不可靠-浏览器不必发送引荐来源网址,因此如果您不知道访问者来自何处,则应该决定该怎么做.

为了进行测试,您可以使用Web Developer FireFox插件禁用发送引荐来源信息的详细信息.



You should also note that the referrer is not reliable - the browser does not have to send it, so you should decide what to do if you don''t know where the visitor has come from.

For testing, you can disable sending referrer details using the Web Developer FireFox add-on.


这篇关于在索引文件中使用js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆