获取并替换 的 href 属性值使用原型标记
我的 tml 中有一个简单的链接(特定于 apache Tapestry):
<a href="www.google.com" class="info-value" target="new">www.google.com</a>
现在在浏览器上,如果我尝试单击该链接,实际上它会重定向到
http://localhost:8080/..../... ./www.google.com
相反,应该为该链接打开一个新选项卡。
所以我想的逻辑是:
1) Fire a javascript on page load
2) Get the href value of anchor tag
3) Append http:// at the start, if it doesn't contains it.
所以要做到这一点,实际上我想使用原型(javascript框架),而且我对此有点陌生......
我如何使用Prototype.js库编写函数?
I have a simple link inside my tml (apache tapestry specific) :
<a href="www.google.com" class="info-value" target="new">www.google.com</a>
Now on the browser if I am trying to click the link, actually it's redirecting to
http://localhost:8080/..../..../www.google.com
Instead of it should open a new tab for that link.
So the logic which I am thinking is :
1) Fire a javascript on page load
2) Get the href value of anchor tag
3) Append http:// at the start, if it doesn't contains it.
So to do this, actually I want to use prototype (javascript framework), and I am bit new to this...
How can I write the function using the Prototype.js library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有说明
href
的值来自何处。正如你所说,你需要在前面添加一个“http”。假设链接是动态呈现的,为什么不在服务器端执行此操作,可能会容易得多。在 tml:和 .java: 中,
这是比在客户端更好的方法,因为如果用户关闭了 javascript 会发生什么?
另一方面,如果它是 .tml 中的静态链接,只需写“http://www.google.com”即可!
编辑:根据您下面的评论:
上面只是一个示例。您可以向
activityDetails
添加另一个执行此操作的方法(例如getExternalLinkWithProtocol()
),或者提供与上述方法类似的包装方法。You don't say where the value for your
href
is coming from. As you say you need to prepend an "http". Assuming the link is dynamically rendered, why don't you just do this server-side, probably much easier. In tml:and in .java:
This is a much better approach than doing it client-side as what happens if the user has javascript turned off?
On the other hand, if it's a static link in your .tml, just write "http://www.google.com"!
Edit: In light of your comment below:
The above is just an example of what do do. You can either add another method to
activityDetails
which does this (e.ggetExternalLinkWithProtocol()
), or provide a wrapper method similar to the one above.没有理由在客户端执行此操作。只需将模板更改为:
www.google.com
如果它基于属性:
${hostname}
...调整以适合您的属性等。
No reason to do this on the client side. Simply change your template to:
<a href="http://www.google.com" class="info-value" target="new">www.google.com</a>
and if it's based on a property:
<a href="http://${hostname}" class="info-value" target="new">${hostname}</a>
... adjust to fit your properties, etc.