确定文件调用功能的目录级别

Determine directory level of file calling function

本文关键字:功能 文件 调用      更新时间:2023-09-26

如果iframe中有一个函数,该函数会加载一个文件,该文件会尝试从调用该函数的iframe的"base href"中加载。

function load_it(){
    top.iframe_1.location='sub_1/file_1.php'
}
上面的

只在从首页调用时才会起作用。如果从/sub_1/调用,它会尝试加载/sub_1/sub_1/file_1.php如果从/sub_1/sub_2/调用,它会尝试加载/sub_1/sub_2/sub_1/file_1.php

所以我需要前缀",'../'或'…/…/'取决于调用页的深度。

ie: ../../sub_1/file_1.php

如何在load_it函数中确定所需的子目录级别?

使用绝对路径代替:

'/sub_1/file_1.php'