Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不带www的Javascript location.host_Javascript - Fatal编程技术网

不带www的Javascript location.host

不带www的Javascript location.host,javascript,Javascript,我有一个php文件,用来分享我的用户在Facebook和Twitter上的帖子。例如,如果我点击Twitter图标,我会看到一个屏幕,其中自动写入指向我网站的超链接。这很好,但我喜欢删除自动显示在Twitter屏幕上的url的www.部分。我猜这与location.host有关,所以我在谷歌上搜索了一下,但我找不到这个问题的正确答案 下面是我的代码示例: function e(f,i,an) { with(document) { w

我有一个php文件,用来分享我的用户在Facebook和Twitter上的帖子。例如,如果我点击Twitter图标,我会看到一个屏幕,其中自动写入指向我网站的超链接。这很好,但我喜欢删除自动显示在Twitter屏幕上的url的
www.
部分。我猜这与location.host有关,所以我在谷歌上搜索了一下,但我找不到这个问题的正确答案

下面是我的代码示例:

function e(f,i,an)
{
        with(document)
        {
                write('<div style="width:100%"><table id=m'+i+' style="visibility:hidden"><tr>');
                write('<td id="vst' + i + '" style="white-space:nowrap">&nbsp;</td>');
                write('<td hr(this.childNodes[0])" title="share on facebook"><a href="javascript:od(\'http://www.facebook.com/sharer.php?u=' + location.host + '/e/'+ i +'\',900,400)" class=icon><img src="images/facebook.png" target="_blank"></a></td>');
        write('<td hr(this.childNodes[0])" title="share on twitter"><a href="javascript:od(\'http://twitter.com/home?status=' + location.host + '/e/'+ i +' - %20read!\',800,600)" class=icon><img src="images/twitter.png" target="_blank"></a></td>');
                write('<td class=ei><a name="cid'+i+'"></a><a href="e/'+i+'">( #'+i+' )</a></td>'); 

                  document.write('<td>&nbsp;</td>');
函数e(f,i,an)
{
附(文件)
{
写(“”);
写(“”);
写(“”);
写(“”);
文件。写(“”);

我的第二个问题是:如何在Twitter屏幕上添加自定义标题:例如,我的网站名,而不必使用location.host和正在共享的主题名的标题?

对于第一个问题,您可以修改主机:

location.host.replace('www.','')

编辑:解决问题

由于再次被否决,并且在第一条评论中看到大量的赞成票,我将尝试解决除包含
www
www
子域之外的子域的问题

对于这个解决方案,仍然远离regex,主要是因为一般来说,维护regex比较困难,而且有很多开发人员根本不接触regex

var cleaned_host;
if(location.host.indexOf('www.') === 0){
    cleaned_host = location.host.replace('www.','');
}
// do something with `cleaned_host`
…或者更简洁地说

location.host.indexOf('www.') && location.host || location.host.replace('www.', '');
// evaluates to hostname with starting `www.` removed
location.host.replace('http://www.“,”)

或者(如果要保留http://)

location.host.replace('http://www.','http://')


它确保您仅在www刚开始时替换它。

如果您只想获取第二级和顶级域,而不是任何子域,这将有助于您:

var url = location.host; // e.g. "www.example.com"
return url.split(".").slice(-2).join("."); // "example.com"
这也适用于其他子域,甚至适用于三个以上的leveldomain。

删除所有各种www.matches
如果您需要删除www.w.ww.ww2.等,下面的代码可能会很方便

Javascript PHP 声明坚持不使用正则表达式,这意味着它不包括评论中描述的某些情况

但是这里有一个不需要正则表达式的解决方案,应该涵盖所有内容。我还使用了
hostname
而不是
host

功能域(域){
var subdomain='www',subdomain_len=subdomain.length;
if(domain.substr(subdomain,subdomain_len)==子域)
域=域。子字符串(子域)
返回域;
}
console.log(域名(location.hostname));//如果需要,删除www
console.log(域名('www.foobar.com');//www
log(域名('www.foobar.com');//不剥离www

console.log(域名('prefix.www.foobar.com');//不剥离www
甚至可以使
location.host.replace(/^www\./,'')
这样
www.
只从主机名的开头删除。但除非您的主机名恰巧是这样,否则这不会有任何区别。-1发生这种情况时,我见过像WW.domain.tld这样的域(带有4个w)。此外,域本身可能以www结尾,比如www.thisismyww.org,在这种情况下会把事情搞砸。我坚持我的答案,它使用字符串替换,这比正则表达式更有效。别误会,我希望正则表达式可以扩展为图灵完全,但它并不总是像字符串函数那样好。如果这个家伙在写lib在很多未知的情况下都会用到正则表达式,我可能会考虑使用正则表达式,或者只在第一个字符上替换字符串,但我怀疑这在这种情况下是必要的。好答案@BillyMoonI已经提交,它处理了上述评论,并且在可以避免的情况下保持了@BillyMoon's no regex的精神。-1:
location.host
只包含URL的主机名部分。它不包含方案,因此您的
.replace()
将永远不会匹配。谢谢您,GolezTrol。现在,我的最后一个问题仍然存在。我希望有人能帮我解决这个问题。
var searchPattern = "^w+\\d*\\." ;
var rx = new RegExp( searchPattern, "gim" ) ;
var replacePattern = "" ;
var replacedText = sourceText.replace( rx, replacePattern ) ;
preg_replace( '|(?mi-Us)^w+\\d*\\.|D', '', $sourceText, $limit, $count) ;