在joomla php中,我可以使用$this->baseurl来获得基本路径,但我想在jquery中获得基本路径。
基本路径可以是以下示例中的任意一个:
http://www.example.com/
http://localhost/example
http://www.example.com/sub/example
例子也可能改变。
在joomla php中,我可以使用$this->baseurl来获得基本路径,但我想在jquery中获得基本路径。
基本路径可以是以下示例中的任意一个:
http://www.example.com/
http://localhost/example
http://www.example.com/sub/example
例子也可能改变。
当前回答
我只是在同一个舞台上,这个解决方案对我来说很管用
在视图中
<?php
$document = JFactory::getDocument();
$document->addScriptDeclaration('var base = \''.JURI::base().'\'');
$document->addScript('components/com_name/js/filter.js');
?>
在js文件中,你将base作为一个变量访问,例如在你的场景中:
console.log(base) // will print
// http://www.example.com/
// http://localhost/example
// http://www.example.com/sub/example
我不记得我把这个信息给信用,如果我找到它,我会编辑答案
其他回答
最简单的方法,以JavaScript获得基础url
window.location.origin
window.location.origin+"/"+window.location.pathname.split('/')[1]+"/"+page+"/"+page+"_list.jsp"
几乎和Jenish的答案一样,但更短一点。
容易
$('<img src=>')[0].src
生成一个带有空src-name的img会迫使浏览器自己计算base-url,不管你是否有/index.html或其他什么。
格式为“hostname/pathname/search”
url是:
var url = window.location.hostname + window.location.pathname + window.location.hash
为了你的案子
window.location.hostname = "stackoverflow.com"
window.location.pathname ="/questions/25203124/how-to-get-base-url-with-jquery-or-javascript"
window.location.hash = ""
baseurl = hostname = window。location。hostname
把它放在你的标题中,这样当你需要它的时候就可以使用了。
var base_url = "<?php echo base_url();?>";
您将得到http://localhost:81/your-path-file或http://localhost/your-path-file。