如何获取javascript中的基本url [英] how to get the base url in javascript

查看:160
本文介绍了如何获取javascript中的基本url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 CodeIgniter 建立网站,我有各种资源,可以使用 base_url 帮助函数如下



< link rel =stylesheettype =text / csshref ='。base_url('assets / css / themes / default.css')''id =style_color/> p>

产生(即www.mysite.com)



< link rel = stylesheettype =text / csshref =http://www.mysite.com/assets/css/themes/default.cssid =style_color/>



然后,我可以使用javascript来将此资源与另一个资源互换。



$('#style_color ').attr(href,assets / css / themes /+ color_ +.css);



是的,它会尝试加载资源,而不使用由php生成的绝对路径,所以我的解决方案是添加一个虚拟标签在每一个页面与php像这样



< div id =base_urlclass ='。base_url()。'>< / div>



然后将javascript行修改为



$('#style_color')。attr(href,$('base_url' ).attr(class)+assets / css / themes /+ color_ +.css);



工作,但它不看起来优雅,所以,我会感谢任何帮助,如何可以从JavaScript或任何其他解决方案中生成此基本URL),感谢:)

< hr>

我偏好使用仅限Javascript的解决方案,由于我使用的是 CodeIgniter ,因此 document.base_url 变量与从协议 index.php 似乎方便



document.base_url = base_url('index.php');


$ b
$ b function base_url(segment){
//获取段
pathArray = window.location.pathname.split('/');
//找到段所在的位置
indexOfSegment = pathArray.indexOf(segment);
// make base_url是原点加上段的路径
return window.location.origin + pathArray.slice(0,indexOfSegment).join('/')+'/';
}


解决方案

h1>

您可以使用 window.location

在JavaScript中轻松访问当前网址

您可以通过 locations 对象访问该网址的段。例如:

  //本文:
// http://stackoverflow.com/questions/21246818/ how-to-get-the-base-url-in-javascript

var base_url = window.location.origin;
//http://stackoverflow.com

var host = window.location.host;
// stackoverflow.com

var pathArray = window.location.pathname.split('/');
// [,questions,21246818,how-to-get-the-base-url-in-javascript]

在Chrome开发工具中,您只需在控制台中输入 window.location






进一步阅读可在此Stack Overflow线程


I am building a website with CodeIgniter, I have various resources that I load with the base_url helper function like this

<link rel="stylesheet" type="text/css" href="'.base_url('assets/css/themes/default.css').'" id="style_color"/>

which produces (i.e. www.mysite.com)

<link rel="stylesheet" type="text/css" href="http://www.mysite.com/assets/css/themes/default.css" id="style_color"/>

I can then swap this resource with another in javascript like this

$('#style_color').attr("href", "assets/css/themes/" + color_ + ".css");

what happens is that it will try to load the resource without using the absolute path generated by php, so my solution was adding a dummy tag in every page with php like this

<div id="base_url" class="'.base_url().'"></div>

I then modified the javascript line to

$('#style_color').attr("href", $('#base_url').attr("class") + "assets/css/themes/" + color_ + ".css");

it does work but it doesn't look elegant at all, so, I would appreciate any help on how to maybe generate this base url from within javascript or any other solution, thanks :)


I preferred a Javascript only solution and since I am using CodeIgniter, a document.base_url variable with the segments of the url from the protocol to the index.php seemed handy

document.base_url = base_url('index.php');

with the function base_url() being

function base_url(segment){
   // get the segments
   pathArray = window.location.pathname.split( '/' );
   // find where the segment is located
   indexOfSegment = pathArray.indexOf(segment);
   // make base_url be the origin plus the path to the segment
   return window.location.origin + pathArray.slice(0,indexOfSegment).join('/') + '/';
}

解决方案

Base URL in JavaScript

You can access the current url quite easily in JavaScript with window.location

You have access to the segments of that URL via this locations object. For example:

// This article:
// http://stackoverflow.com/questions/21246818/how-to-get-the-base-url-in-javascript

var base_url = window.location.origin;
// "http://stackoverflow.com"

var host = window.location.host;
// stackoverflow.com

var pathArray = window.location.pathname.split( '/' );
// ["", "questions", "21246818", "how-to-get-the-base-url-in-javascript"]

In Chrome Dev Tools, you can simply enter window.location in your console and it will return all of the available properties.


Further reading is available on this Stack Overflow thread

这篇关于如何获取javascript中的基本url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆