1

I am working on a project for a client who would like their own dynamic latest news feed embedded on to the right hand side of our site. It's a fairly custom module with videos and links, not just RSS or Twitter.

The client has uploaded this content on to their servers as a page specially for us to grab, and have provided us with the URL.

I could embed the content using an iFrame, but they haven't got target="_parent" in their links, so if a link is clicked the new content is loaded within the iframe which isn't what we want. We want it to open in a new tab. We can't change their content or ask them to for 2 weeks unfortunately - which will be too late! To make it even more annoying, we're not on the same domain.

Our system is build on asp.net which I have no knowledge of (I'm more PHP!). It's also worth mentioning that I am a front-end developer in this role and can't access or modify any asp.net code.

I have tried using jQuery .load() without success. Being a bit of a newbie I am unsure of the correct way to go about this, or if it's even possible at all using front-end technologies?

It seems that similar questions to mine are all old and dated so it would be good to get the latest and best approach.

Thanks.

4

3 回答 3

0

好吧,如果你用 拉入他们的内容.load(),它有一个完成时的回调函数。只需循环浏览<a>该部分中的所有链接并将其更改target=为在新选项卡中打开!

css: target-new: tab ! important(不幸的是,这仅与 css3 兼容)

于 2012-08-14T13:40:47.323 回答
0

使用jQuery .load()您需要提取页面上的所有 JS 代码并使其针对此部分,因为它只会加载 DOM 元素。这有点棘手,但在改变了一些东西和路径之后,你就可以让它工作了。

另一种可能更简单的方法,但只有当您在同一个域上时它才会起作用:

$("#iFrame").contents().find("a").on("click",function(e)
{
    e.preventDefault();
    // then use the href to virtualy create a link click event :-)
});
于 2012-08-14T13:44:48.677 回答
0

如果您可以确保<head>正在加载的页面中包含:

<base target="_blank" />

然后所有链接将在新窗口中打开。如果您无法确保页面中包含该内容<head>,则可以选择通过服务器端 HTTP 请求下载页面数据,并在将页面嵌入您的框中之前手动添加该标签。如果你不想做任何服务器端的工作,你还不如在客户端做。这是如何完成的简化版本:

$.get("http://exampleurl.com/", function (data) {
    var pos = data.indexOf("<head>");
    if(pos == -1) {
        //pretty impossible
        return;
    }
    data = data.substr(0, pos + 5) + '<base target="_blank" />' + data.substr(pos + 6, data.length - (pos + 6));
    //display data
});
于 2012-08-14T13:43:25.520 回答