从链接的href属性获取完整URI [英] Get the full URI from the href property of a link

查看:145
本文介绍了从链接的href属性获取完整URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在某些方面得到确认。

I would like to have a confirmation on some point.

我的目标是在阅读时始终获得相同的字符串(在我的情况下是URI)来自链接的href属性。示例:

My goal is to always get the same string (which is the URI in my case) while reading the href property from a link. Example:

< a href =test.htm/> ,其中base_url = http://domain.name/

<a href="test.htm" /> with base_url = http://domain.name/

< ; a href =../ test.htm/> with base_url = http:/ /domain.name/domain/

<a href="../test.htm" /> with base_url = http://domain.name/domain/

< a href =http://domain.name/test.htm /> with base_url =来自 http://domain.name/ 的任何文件夹

<a href="http://domain.name/test.htm" /> with base_url = any folder from http://domain.name/

我需要从以上3种情况(或任何其他情况)获得 http://domain.name/test.htm 相同的字符串)。

I need to get http://domain.name/test.htm from the 3 situations above (or any other identical string).

经过一些测试后,似乎 my_a_dom_node.href 总是返回完全限定的URI,包括 http://domaine.name ,这应该可以满足我的需求。

After some tests, it appears that my_a_dom_node.href always return the full-qualified URI, including the http://domaine.name, which should be okay for what I want.

jQuery有不同的行为, $(my_a_dom_node).attr('href')返回内容(文本)出现在HTML中。所以我的诀窍是使用 $(my_a_dom_node).get(0).href 来获取完整的URI。

jQuery has a different behaviour and $(my_a_dom_node).attr('href') returns the content (text) that appears inside the HTML. So my trick is to use $(my_a_dom_node).get(0).href to get the full URI.

问题是:我能依靠吗?

推荐答案

是的,你可以信赖!

当人们使用简单的javascript(没有jQuery)时,许多人问你要求的相反,他们想要得到href属性中写的真实url而不是完整的url,在这种情况下他们过去只是这样做:

Once when people was using simple javascript (no jQuery) many asked the opposite of what you are asking, they wanted to get the real url as written in the href attribute and not the full one, in such case they used to simply do:

my_a_dom_node.getAttribute('href', 2); //works both IE/FF

然后是jQuery帮助人们不要浪费时间寻找他们需要这样的代码,jQuery总是返回href属性中写的真实url。

Then it came jQuery that helped people not to waste their time in finding out they would need such code and jQuery always returns the real url as written in the href attribute.

有趣的是,现在有人问如何获取完整的URL,因为jQuery返回了href属性中写的那个。

It's funny that now someone is asking how to get the full url because jQuery returns the one written in the href attribute.

这篇关于从链接的href属性获取完整URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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