如何在HTML文件中通过javascript获得当前网页的绝对路径和相对路径

如何在HTML文件中通过javascript获得当前网页的绝对路径和相对路径,第1张

绝对路径

<!DOCTYPE html>

<html>

<body>

<script>

documentwrite(locationhref);//输出当前页面完整URL

</script>

</body>

</html>

相对路径:

<!DOCTYPE html>

<html>

<body>

<script>

documentwrite(locationpathname);//返回当前URL的路径名

</script>

</body>

</html>

1、设置或获取整个

URL

为字符串:

windowlocationhref

2、设置或获取与

URL

关联的端口号码:

windowlocationport

3、设置或获取

URL

的协议部分

windowlocationprotocol

4、设置或获取

href

属性中跟在问号后面的部分

windowlocationsearch

5、获取变量的值(截取等号后面的部分)

复制代码

代码如下:

var

url

=

windowlocationsearch;

//

alert(urllength);

//

alert(urllastIndexOf('='));

var

loc

=

urlsubstring(urllastIndexOf('=')+1,

urllength);

6、设置或获取

URL

的协议部分:

windowlocationprotocol

7、设置或获取

href

属性中在井号“#”后面的分段:

windowlocationhash

8、设置或获取

location

URL

hostname

port

号码:

windowlocationhost

如果通过servelet来读取上传文件路径,则代码如下,我已经在项目中用过了:

DiskFileItemFactory difif = new DiskFileItemFactory();

dififsetSizeThreshold(321024);dififsetRepository(new File(tmpDir));        //

用以上工厂实例化上传组件ServletFileUpload sfu = new ServletFileUpload(difif);

sfusetSizeMax(MAX_SIZE);

List fileList = sfuparseRequest(request);

if ( fileList == null || fileListsize() == 0) {                throw new Exception("

导入文件不存在,请检

查");} else {                File upLoadFile = null;                Iterator fileIterator = fileListiterator();

//得到所有上传的文

件                while (fileIteratorhasNext()) {                    FileItem fileItem = (FileItem)fileIteratornext();                    //

取得上传文件

名                    if (fileItemgetContentType() == null) {                    } else {

                        //

此为路

径                        String path = fileItemgetName();                        String t_ext = pathsubstring(pathlastIndexOf("") + 1);                                                //

根据系统时间生成上传后保存的文件

名                        String prefix = StringvalueOf(SystemcurrentTimeMillis());                        //

保存最终的文件完整路径,保存在工程根目录下的Upload目录

下                        String u_name = tmpDir + Fileseparator + prefix + "" + t_ext;                                                //

开始保存文

件                        upLoadFile = new File(u_name);                        fileItemwrite(upLoadFile);                    }                }            }

以上就是关于如何在HTML文件中通过javascript获得当前网页的绝对路径和相对路径全部的内容,包括:如何在HTML文件中通过javascript获得当前网页的绝对路径和相对路径、js获取当前页面的url网址信息、java web项目java后台获取前台js上传文件的路径等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/10148088.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存