使用JavaScript获取服务器路径时跨浏览器安全吗?对于Joomla模板,我有大量的JavaScript文件将通过SCRIPT标签包含在内;这些文件需要服务器路径,例如站点根。下面是一些JS代码,我发现它获取服务器的路径:通过JavaScript与PHP获取服务器路径
var hash = self.location.hash // Sets or retrieves the subsection of the href property that follows the number sign (#).
var host = self.location.host // Sets or retrieves the hostname and port number of the location or URL.
var hostname = self.location.hostname // Sets or retrieves the host name part of the location or URL.
var href = self.location.href // Sets or retrieves the entire URL as a string.
var pathname = self.location.pathname // Sets or retrieves the path and file name associated with a URL.
var port = self.location.port // Sets or retrieves the port number associated with a URL.
var protocol = self.location.protocol // Sets or retrieves the protocol portion of a URL.
alert('hash: ' + hash + '\n' +
'host: ' + host + '\n' +
'hostname: ' + hostname + '\n' +
'href: ' + href + '\n' +
'pathname: ' + pathname + '\n' +
'port: ' + port + '\n' +
'protocol: ' + protocol);
以上的JavaScript会返回此:
hash: #panel-1
host: localhost:8090
hostname: localhost
href: http://localhost:8090/joomla/#panel-1
pathname: /joomla/
port: 8090
protocol: http
在Joomla网站将在许多浏览器,平台和设备上运行。上述JS代码是否适合这些场景,还是使用PHP来获取服务器路径更好?谢谢。
啊......不知道。这是有道理的,所以PHP是更可靠的方法。谢谢。 – Alex 2011-02-08 14:15:23