在 window.open 函数中添加 URL 前缀 jQuery

发布于 2025-01-05 05:06:48 字数 1188 浏览 4 评论 0原文

我有这个 HTML:

<a href="#" onclick="window.open('TrackPackage.asp?ID=4', '', 'settings');">Track Your Package »</a>

此站点上的某人能够为我提供一个脚本,以在 URL 中添加域前缀 http://www.example.com/ 这是脚本:

$(document).ready(function(){
$('a[onclick^="window.open(\'TrackPackage.asp"]').attr('onClick', $('a[onclick^="window.open(\'TrackPackage.asp"]').attr('onClick').replace("window.open('", "window.open('http://www.example.com/"));
});

但是,我有有点麻烦:
第一个问题是元素有多个实例。这是一个小提琴: http://jsfiddle.net/VMmZx/
按照预期,一个锚点使用 ID=4 签名,另一个锚点使用 ID=5 签名,而是都使用 ID=4 签名>。
这个想法是,每个 window.open 函数都应该以 http://www.example.com 为前缀,但是 URL 的其余部分应该保持不变。 ..

我遇到的第二个问题是,当页面上不存在该元素时,jQuery 的其余部分将失败...
这是另一个小提琴: http://jsfiddle.net/VPf32/
应该获取类 foo,但由于页面上不存在该元素,因此 jQuery 不会执行。
由于 JavaScript 包含在 ASP.NET 服务器的 HTML 模板中,这可能会产生很多问题。

我希望我已经说清楚了,您可以帮助我。 谢谢。

I have this HTML:

<a href="#" onclick="window.open('TrackPackage.asp?ID=4', '', 'settings');">Track Your Package »</a>

Somebody on this site was able to provide me with a script to prefix the URL with the domain http://www.example.com/ Here's the script:

$(document).ready(function(){
$('a[onclick^="window.open(\'TrackPackage.asp"]').attr('onClick', $('a[onclick^="window.open(\'TrackPackage.asp"]').attr('onClick').replace("window.open('", "window.open('http://www.example.com/"));
});

However, I am having a little trouble with this:
The first issue is where there is multiple instances of the element. Here's a fiddle: http://jsfiddle.net/VMmZx/
Instead of one anchor being signed with ID=4 and the other with ID=5 as intended, they're both being signed with ID=4.
The idea is, each window.open function should be prefixed with http://www.example.com however, the remainder of the URL should remain intact...

The second problem I'm encountering is when the element does not exist on a page, the remainder of the jQuery fails...
Here's another fiddle: http://jsfiddle.net/VPf32/
The <a> should get the class foo, but since the element does not exist on the page, the jQuery does not execute.
Since the JavaScript is being included in the HTML template of the ASP.NET server, this can create many problems.

I hope I've been clear and you can help me. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零度℉ 2025-01-12 05:06:48

您可以使用 .each() 迭代每个匹配元素并更改它们单独:

$('a[onclick^="window.open(\'TrackPackage.asp"]').each(function(index, element) {
    element = $(element);
    element.attr('onclick', element.attr('onclick').replace(/open\('/, 'open(\'http://www.example.com/'));
});​

但是,我认为使用 href#onclick 打开窗口的链接并不具有语义性。如果可能的话,尝试将标记更改为:

<a href="TrackPackage.asp?ID=4" target="_blank">Track Your Package »</a>

现在,如果有人好奇它将引导他们去哪里,当您将鼠标悬停在状态栏中时,浏览器可以在状态栏中显示一些有用的内容。

如果您需要进一步调整行为,请添加一个类并绑定 click 事件。当它们单击时,阻止默认操作并自行打开窗口,就像您之前所做的那样。

You can use .each() to iterate over each matching element and change them individually:

$('a[onclick^="window.open(\'TrackPackage.asp"]').each(function(index, element) {
    element = $(element);
    element.attr('onclick', element.attr('onclick').replace(/open\('/, 'open(\'http://www.example.com/'));
});​

However, I don't think using links with a href of # and an onclick opening a window is as semantic as it could be. If possible, try changing the markup to this:

<a href="TrackPackage.asp?ID=4" target="_blank">Track Your Package »</a>

Now if someone is curious where it will lead them, the browser can show something useful in the status bar when you hover over it.

If you need to adjust the behavior further, add a class and bind for the click event. When they click, prevent the default action and open the window yourself, as you did before.

一口甜 2025-01-12 05:06:48

你为什么要这样进行内联点击?我只会输出如下链接:

<a href="/path/to/file.html" target="_blank">Link Text</a>

然后:

$('a[target=_blank]').click(function(){
   var prefix = 'http://domain.com';
   window.open(prefix + $(this).attr('href'));
});

Why are you doing the click even inline like that? I would just output the links like:

<a href="/path/to/file.html" target="_blank">Link Text</a>

And then:

$('a[target=_blank]').click(function(){
   var prefix = 'http://domain.com';
   window.open(prefix + $(this).attr('href'));
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文