简体   繁体   English

.href.replace给我双重域名(http:// localhosthttp://mydomain.com)

[英].href.replace giving me double domain (http://localhosthttp://mydomain.com)

im working on a piece of code where im getting the html from another server through php with file_get_contents. 我正在处理一段代码,其中我使用file_get_contents通过php从另一台服务器获取html。

Then some of the links needs to get the original domain onto it so that they link to the proper places, for most of my links ive just been able to replace the whole line as they were static but with this one being dynamic i need to replace just a part of it. 然后某些链接需要将原始域放置到其上,以便它们链接到适当的位置,因为我的大多数链接ive都能够替换整行,因为它们是静态的,但是由于这是动态的,因此我需要替换只是一部分。

Ive done it this way: 我已经这样完成了:

<script>$(document).ready(function(){
  $(".view").each(function(){
        this.href = this.href.replace("/tourney/", "http://binarybeast.com/tourney/");
    })
});</script>

I however am getting the problem that when doing this im getting double domains even though no domain was made in the first link like this: 但是我遇到的问题是,即使在第一个链接中都没有这样的域,这样做时我还是会获得双重域:

<a class="view" href="http://localhosthttp://binarybeast.com/tourney/load_match/169049">View Details</a>

Original line: 原始行:

<a class="view" href="/tourney/load_match/169049">View Details</a>

Use attr("href") to access the attribute: 使用attr("href")访问属性:

this.href = $(this).attr("href").replace(...)

Or without jQuery the getAttribute function. 或者没有jQuery的getAttribute函数。

The href property will always return the absolute path. href属性将始终返回绝对路径。 Here's an example: 这是一个例子:

$("<a href='test'>")[0].href
//"http://stackoverflow.com/questions/15627830/href-replace-giving-me-double-domain-http-localhosthttp-mydomain-com/test"
$("<a href='test'>").attr("href")
//"test"

Try this: 尝试这个:

this.setAttribute("href",this.getAttribute("href").replace("/tourney/","http://.../"));

This accesses the attribute as you write it, not the property as parsed by the browser. 这将在您编写属性时访问该属性,而不是浏览器解析的属性。

Here's a slightly different way of looking at it: 这是一种稍微不同的查看方式:

$(document).ready(function(){
    $(".view").each(function(){
        var parts = this.href.split('/').slice(3);

        if (parts[0] == 'tourney') {
            this.href = '/' + parts.join('/');
        }
    });
});

http://jsfiddle.net/userdude/aAXYM/ http://jsfiddle.net/userdude/aAXYM/

This works because the urls in this.href will always be expanded, thus you can remove the first part (the domain is in parts[2] ), test, and reassemble. 之所以this.href是因为this.href的URL始终会被扩展,因此您可以删除第一部分(域位于parts[2] ),进行测试并重新组装。 If you want to add http://yoursite.com , just add that to the first part, as 'http://yoursite.com/' + or use: 如果要添加http://yoursite.com ,只需将其添加为第一部分,即'http://yoursite.com/' +或使用:

window.location.protocol + '//' + window.location.hostname + '/' + ... 

if it's appropriate. 如果合适的话。

The href property returns the full, absolute url. href属性返回完整的绝对URL。 Either change the replace-regex to /.*?\\/tourney\\// , or assign to the host property : 将replace-regex更改为/.*?\\/tourney\\//或分配给host属性

this.host = "binarybeast.com";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用PHP / Nginx将cookie域从无域迁移到.mydomain.com - Migrate cookie domain from no domain to .mydomain.com with PHP/Nginx 在Heroku上将app.mydomain.com作为mydomain.com/app运行 - Run app.mydomain.com as mydomain.com/app on Heroku Symfony 为 mydomain.com/parameter 创建路由 - Symfony create routing for mydomain.com/parameter 网址重写:将mydomain.com/index.php更改为mydomain.com/home - URL rewriting: Change mydomain.com/index.php to mydomain.com/home Laravel 4.1使用301永久重定向将所有请求从www.mydomain.com重定向到mydomain.com - Laravel 4.1 Redirect all requests from www.mydomain.com to mydomain.com with 301 permanent redirect 如何在Codeigniter中使用mobiledetect.net将mydomain.com重定向到m.mydomain.com? - How to redirect mydomain.com to m.mydomain.com with mobiledetect.net in codeigniter? PHP邮件未从mydomain.com发送到Google应用程序电子邮件myname@mydomain.com - PHP mail is not sending from mydomain.com to the google apps email myname@mydomain.com 使用PHP设置“ mydomain.com/username”的任何简单,整洁的方法吗? - Any easy, noncluttering way to set up “mydomain.com/username” using PHP? 如何使用zend框架获取像mydomain.com/username这样的动态URL - how to get dynamic URL like mydomain.com/username using zend framework 如何调试“ Waiting For mydomain.com” Web请求陷入等待状态 - How to debug “Waiting For mydomain.com” web-request getting stuck at pending state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM