$cache[$key] = empty($arr) ? NULL : $arr; return $cache[$key]; } // 门户 获取需要在频道显示的栏目主题数据 function portal_channel_thread($fid) { global $forumlist; if (empty($fid)) return NULL; $orderby = array('tid' => 1); $page = 1; // 遍历所有在频道显示内容的栏目 $category_forumlist = channel_category($fid); $arrlist = array(); $forum_tids = array(); $tidlist = array(); if ($category_forumlist) { foreach ($category_forumlist as &$_forum) { // 频道显示数据 $arrlist['list'][$_forum['fid']] = array( 'fid' => $_forum['fid'], 'name' => $_forum['name'], 'rank' => $_forum['rank'], 'type' => $_forum['type'], 'url' => $_forum['url'], 'channel_new' => $_forum['channel_new'], ); $forum_thread = thread_tid__find(array('fid' => $_forum['fid']), $orderby, $page, $_forum['channel_new'], 'tid', array('tid')); // 最新信息按栏目分组 foreach ($forum_thread as $key => $_thread) { $forum_tids[$key] = $_thread; } unset($forum_thread); } $tidlist += $forum_tids; } unset($category_forumlist); // 获取属性对应的tid集合 list($flaglist, $flagtids) = flag_thread_by_fid($fid); empty($flagtids) || $tidlist += $flagtids; unset($flagtids); // 频道置顶 $stickylist = sticky_list_thread($fid); empty($stickylist) || $tidlist += $stickylist; // 在这之前合并所有二维数组 tid值为键/array('tid值' => tid值) $tidarr = arrlist_values($tidlist, 'tid'); // 在这之前使用$tidarr = array_merge($tidarr, $arr)前合并所有一维数组 tid/array(1,2,3) if (empty($tidarr)) { $arrlist['list'] = isset($arrlist['list']) ? array_multisort_key($arrlist['list'], 'rank', FALSE, 'fid') : array(); return $arrlist; } $tidarr = array_unique($tidarr); $pagesize = count($tidarr); // 遍历获取的所有tid主题 $threadlist = well_thread_find_asc($tidarr, $pagesize); // 遍历时为升序,翻转为降序 $threadlist = array_reverse($threadlist); foreach ($threadlist as &$_thread) { // 各栏目最新内容 isset($forum_tids[$_thread['tid']]) AND $arrlist['list'][$_thread['fid']]['news'][$_thread['tid']] = $_thread; // 全站置顶内容 isset($stickylist[$_thread['tid']]) AND $arrlist['sticky'][$_thread['tid']] = $_thread; // 首页属性主题 if (!empty($flaglist)) { foreach ($flaglist as $key => $val) { if (isset($val['tids']) && in_array($_thread['tid'], $val['tids'])) { $arrlist['flaglist'][$key][array_search($_thread['tid'], $val['tids'])] = $_thread; ksort($arrlist['flaglist'][$key]); $arrlist['flag'][$_thread['tid']] = $_thread; } } } } unset($threadlist); if (isset($arrlist['sticky'])) { $i = 0; foreach ($arrlist['sticky'] as &$val) { ++$i; $val['i'] = $i; } } if (isset($arrlist['flag'])) { $i = 0; foreach ($arrlist['flag'] as &$val) { ++$i; $val['i'] = $i; } } if (isset($arrlist['flaglist'])) { foreach ($arrlist['flaglist'] as &$val) { $i = 0; foreach ($val as &$v) { ++$i; $v['i'] = $i; } } } isset($arrlist['list']) AND $arrlist['list'] = array_multisort_key($arrlist['list'], 'rank', FALSE, 'fid'); return $arrlist; } ?>Equivalent of PHP's dirname(__FILE__) in JavaScript? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Equivalent of PHP's dirname(__FILE__) in JavaScript? - Stack Overflow

programmeradmin0浏览0评论

as I already mentioned in the title, I'm looking for a JS-function for getting the same result like I get with this PHP code:

dirname(dirname(__FILE__))

Thanks in advance!

as I already mentioned in the title, I'm looking for a JS-function for getting the same result like I get with this PHP code:

dirname(dirname(__FILE__))

Thanks in advance!

Share Improve this question asked Nov 3, 2012 at 5:04 Mr. B.Mr. B. 8,69716 gold badges74 silver badges119 bronze badges 5
  • 1 What do you want to get the second parent directory of? The HTML file? Where the javascript code is? A file on the user's filesystem? And why? – Ian Commented Nov 3, 2012 at 5:07
  • 2 stackoverflow.com/questions/2161159/get-script-path – clyfish Commented Nov 3, 2012 at 5:08
  • 1 It isn't available... at least not in the sense like it is in PHP. – Brad Commented Nov 3, 2012 at 5:09
  • @lan: I would like to get the path of the file, because I need the variable "root_dir". In PHP I did it like this: define("ROOT_DIR", dirname(dirname(FILE)) . DIRECTORY_SEPARATOR); – Mr. B. Commented Nov 3, 2012 at 5:10
  • Okay, may I ask why you need it though? – Ian Commented Nov 3, 2012 at 5:26
Add a comment  | 

7 Answers 7

Reset to default 9

I don't think it is possible because php dirname operates on apache server on local machine. It has access to the filesystem. But javascript operates on browser layer which can't operate with filesystem. I think so you should use ajax and proccess result how you need it. I think so its best solution for you.

I needed a solution to write code like this:

$("#div").load(ROOT_URL + "my/path/to/script.php");

Solution: a PHP script generates one JS-file of all needed JS-files and adds the ROOT_URL to the top of the generated file:

$js = 'ROOT_URL = "' . ROOT_URL . '"; ' . $js;
file_put_contents("file.js", $js);

Now I'm able to use the ROOT_URL (set in a PHP config-file) in JS-code as well. I hope I could help.

You can have PHP output the script. Yes, that's right, you probably can't make php process js files (unless you are in full control of the server). But it doesn't matter. Just make sure that the MIME type is correct, both in the headers PHP returns and the script tag. That way, you can have PHP insert the any values you want in the script, including it's own path.

In script.php:

header("Content-type: text/javascript");
echo 'var myvar = '.$something;
//where $something can be $_SERVER['REQUEST_URI'], __FILE__ or whatever you need.
//You could even use information from session variables, or query the database.
//In fact, this way you can have GET parameters in your javascript.
//Make sure you are not creating a vulnerability with the exposed information.

//Then put the rest of the script as usual. You could even include it*.

*: include

In HTML:

<script type="text/javascript" src="script.php"></script>

Yes, I know I'm repeating the MIME type, do it this way to maximize browser compatibility.

There's no analogue of __FILE__ in browser Javascript; the code does not have direct access to the URL from which it was loaded. But with certain assumptions you can figure it out, as in the answer here.

Once you have the URL of the script (I assume in a variable called scriptURL below) you can set about finding the grandparent URL. This can get tricky with URLs, so it's probably safest to let the URL-savvy bits of Javascript parse the URL for you and get just the pathname component before you start with the string-munging:

var a = document.createElement('a')
a.href = scriptURL
var scriptPath = a.pathname

Then it's unfortunately down to string manipulation; here's one somewhat clunky solution:

var components = scriptPath.split(/\//)
while (components.length > 0 && !components[components.length-1])
    components.length -= 1;
var twoDirsUp = components.slice(0,components.length-2).join('/')

And then you can convert the result back into a full URL using the anchor element trick in reverse:

a.pathname = twoDirsUp;
var grandParentUrl = a.href

Why not load what you want from absolute URL?

If you have inse your block of codes: /my/script/to/load.js browser will load the correct file if you are in yoursite.com or whatever like yoursite.com/a/b/c/d/e/f

A little off topic, but if you just want to get the similar of dirname($_SERVER['REQUEST_URI']) for javascript, you can do

window.location.href.substr(0, window.location.href.length - window.location.href.split('/').pop().length)

I use something like that to free from the paths in javascript

 var __DIR__ = window.location.pathname.match('(.*\/).*')[1] + 'NameOfThisFolder';

first

window.location.pathname.match('(.*\/).*')[1]

return the current path without the file name or other stuff.

rootFolder/folder1/folder2/

then I add the name of this folder ('NameOfThisFolder').

In this way, I can make for instance ajax request in current page from a page that was called in turn from an ajax request without worry about the path

发布评论

评论列表(0)

  1. 暂无评论