简体   繁体   English

使用JavaScript重定向到指定页面

[英]Redirect to a specified page using JavaScript

Is there any way through which i can have a javascript on a page to redirect any url that's present on the page to some specific site. 有什么方法可以使页面上的JavaScript将页面上存在的所有URL重定向到某个特定站点。

For example on a HTML page i have say 10 urls present. 例如,在HTML页面上,我说有10个网址。 Can i add a javascript to the HTML page so that if anyone clicks on any url on that page, it gets redirected to the a specified page. 我可以向HTML页面添加JavaScript吗,以便任何人单击该页面上的任何url时,它将被重定向到指定的页面。

Thanks. 谢谢。

EDIT:: 编辑::

My scenario is i have some 13k links on a page and i do highlighting of terms on the page, even if any link is also clicked on the page, the word gets highlighted on that page. 我的情况是,我在页面上有一些13k链接,并且在页面上也突出显示了术语,即使在页面上也单击了任何链接,单词都会在该页面上突出显示。 In order to do that i process each url and add some more info to it to go thought my server perl script which does the job of highlighting. 为了做到这一点,我处理了每个URL并向其中添加了更多信息,以为我的服务器perl脚本完成了突出显示工作。 But now due to large number of links on page, it takes time to process the page and page is rendered after a long time. 但是,由于页面上的链接数量众多,处理页面需要花费时间,并且页面经过很长时间后才呈现。 So i want to have a javascript which can pass any link by adding info to my perl script on server. 所以我想拥有一个可以通过向服务器上的perl脚本添加信息来传递任何链接的javascript。

I tried doing it server side my breaking page into pieces and processing in parallel but not much improvement. 我尝试在服务器端将其分解成几部分并并行处理,但并没有太大改进。

Any other solution or suggestions are welcomed. 任何其他解决方案或建议均受到欢迎。 Appreciate your help in this regard. 感谢您在这方面的帮助。

You can use preventDefault in the click event handler to prevent the default behavior(open the link), and use location.href to redirect to a new page. 您可以在click事件处理程序中使用preventDefault来防止出现默认行为(打开链接),并使用location.href重定向到新页面。

if you're using jQuery: 如果您使用的是jQuery:

$(".links").click(function(event){
    event.preventDefault();
    location.href = "http://google.com";
});

You can do this with the following jQuery block: 您可以使用以下jQuery块执行此操作:

$(document).ready(function () {
    $('#urlId').live('click', function (e) {
        e.preventDefault(); //Stops the link from opening
        window.location.href = "/specifiedPage"; // Changes the location of the page
    });
});

You can create a "protective glass" div in front of everything and handle the click event on that div. 您可以在所有内容的前面创建一个“保护玻璃” div并处理该div上的click事件。 This has the advantage of not touching the page so after removing the div anything can go back to normal. 这样做的好处是不触摸页面,因此在删除div之后,所有内容都可以恢复正常。

Only be sure to put a non-fully-transparent color on the div background because I've found that Internet Explorer ignores events if the div is fully transparent. 仅确保将非完全透明的颜色放在div背景上,因为我发现如果div是完全透明的,则Internet Explorer会忽略事件。

Something like rgba(0,0,0,0.001) is enough. rgba(0,0,0,0.001)类的东西就足够了。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM